Java项目实战 - 展示商品详情页

发布服务

zheyi-rest实现:商品基本信息和描述的显示,并添加缓存。

Dao

无。

Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@Service
public class ItemServiceImpl implements ItemService {

@Value("${REDIS_ITEM_KEY}")
private String REDIS_ITEM_KEY;
@Value("${REDIS_ITEM_EXPIRE}")
private Integer REDIS_ITEM_EXPIRE;


@Autowired
private TbItemMapper itemMapper;

@Autowired
private TbItemDescMapper itemDescMapper;

@Autowired
private RedisUtils redisUtils;

@Override
public TaotaoResult getItemBasicInfo(long itemId) {

//get cache
try {
String cache = redisUtils.get(REDIS_ITEM_KEY + ":" + itemId + ":basic");
if (!StringUtils.isBlank(cache)) {
TbItem item = JsonUtils.jsonToPojo(cache, TbItem.class);
return TaotaoResult.ok(item);
}
} catch (Exception e) {
e.printStackTrace();
}


TbItem item = itemMapper.selectByPrimaryKey(itemId);

//add cache
try {
redisUtils.set(REDIS_ITEM_KEY + ":" + itemId + ":basic", JsonUtils.objectToJson(item));
redisUtils.expire(REDIS_ITEM_KEY + ":" + itemId + ":basic", REDIS_ITEM_EXPIRE);
} catch (Exception e) {
e.printStackTrace();
}
return TaotaoResult.ok(item);
}

@Override
public TaotaoResult getItemDesc(long itemId) {

//get cache
try {
String cache = redisUtils.get(REDIS_ITEM_KEY + ":" + itemId + ":desc");
if (!StringUtils.isBlank(cache)) {

TbItemDesc itemDesc = JsonUtils.jsonToPojo(cache, TbItemDesc.class);
return TaotaoResult.ok(itemDesc);
}
} catch (Exception e) {
// TODO: handle exception
}

TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);

//add cache
try {
redisUtils.set(REDIS_ITEM_KEY + ":" + itemId + ":desc", JsonUtils.objectToJson(itemDesc));
redisUtils.expire(REDIS_ITEM_KEY + ":" + itemId + ":desc", REDIS_ITEM_EXPIRE);
} catch (Exception e) {
e.printStackTrace();
}
return TaotaoResult.ok(itemDesc);
}

}

Portal调用

zheyi-portal实现:当用户访问商品详情页面时,需要加载商品信息。

Dao

需要添加POJO:

1
2
3
4
5
6
7
8
9
10
11
public class ItemInfo extends TbItem{

public String[] getImages() {
String image = getImage();
if (image != null) {
String[] images = image.split(",");
return images;
}
return null;
}
}

Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@Service
public class ItemServiceImpl implements ItemService {

@Value("${REST_BASE_URL}")
private String REST_BASE_URL;
@Value("${ITEM_INFO}")
private String ITEM_INFO;
@Value("${ITEM_DESC_URL}")
private String ITEM_DESC_URL;

@Override
public ItemInfo getItemById(Long itemId) {
try {
String itemJson = HttpClientUtil.doGet(REST_BASE_URL + ITEM_INFO + itemId);
if (!StringUtils.isBlank(itemJson)) {
TaotaoResult result = TaotaoResult.formatToPojo(itemJson, ItemInfo.class);
if (result.getStatus() == 200) {
ItemInfo itemInfo = (ItemInfo) result.getData();
return itemInfo;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

@Override
public String getItemDescById(Long itemId) {
try {
//查询商品描述
String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_DESC_URL + itemId);
//转换成java对象
TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, TbItemDesc.class);
if (taotaoResult.getStatus() == 200) {
TbItemDesc itemDesc = (TbItemDesc) taotaoResult.getData();
//取商品描述信息
String result = itemDesc.getItemDesc();
return result;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Controller
public class ItemController {

@Autowired
private ItemService itemService;

@RequestMapping("/item/{itemId}")
public String getItem(@PathVariable Long itemId, Model model){
ItemInfo itemInfo = itemService.getItemById(itemId);

model.addAttribute("item", itemInfo);

return "item";
}

@RequestMapping(value="/item/desc/{itemId}", produces=MediaType.TEXT_HTML_VALUE+";charset=utf-8")
@ResponseBody
public String getItemDesc(@PathVariable Long itemId) {
String string = itemService.getItemDescById(itemId);
return string;
}
}

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!