https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040
第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第五天】
第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第六天】
第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第七天】(redis缓存)
第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第八天】(solr服务器搭建、搜索功能实现)
第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第九天】(商品详情页面实现)
1 课程计划
今天的内容:
1、在taotao-portal工程中调用taotao-search工程发布的服务。实现商品搜索功能。
2、点击商品的图片,打开商品详情页面
a) 商品基本信息
b) 延迟加载商品详情。延迟一秒加载使用ajax
c) 商品的规格参数。按需加载,当用户点击商品规格参数tab页,加载ajax。
2.3 Service层
接收两个参数1、查询条件2、页码。调用taotao-search的搜索服务。接收返回的json数据,把json转换成java对象返回SearchResult对象。
@Service public class SearchServiceImpl implements SearchService { @Value("${SEARCH_BASE_URL}") private String SEARCH_BASE_URL; @Override public SearchResult search(String queryString, int page) { // 调用taotao-search的服务 // 查询参数 Map<String, String> param = new HashMap<>(); param.put("q", queryString); param.put("page", page + ""); try { // 调用服务得到JSON格式 String json = HttpClientUtil.doGet(SEARCH_BASE_URL, param); // 把字符串转换成java对象 TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, SearchResult.class); if (taotaoResult.getStatus() == 200) { SearchResult result = (SearchResult) taotaoResult.getData(); return result; } } catch (Exception e) { e.printStackTrace(); } // 程序正常执行走try{}代码块,必然会return rsult; // 此处的return null;只是为了这个方法返回值类型检查不报错 return null; } }
2.4 Controller层
功能:接收请求的参数查询条件和页码。调用Service查询商品列表得到SearchResult对象。
需要把
Query:回显的查询条件
totalPages:总页数
itemList:商品列表
Page:当前页码
传递到页面。返回一个逻辑视图search字符串。
//首页商品搜索Controller @Controller public class PortalSearchController { @Autowired private SearchService searchService; @RequestMapping("/search") public String search(@RequestParam("q")String queryString, @RequestParam(defaultValue="1")Integer page, Model model) { if (queryString != null) { try { //防止字符串乱码 queryString = new String(queryString.getBytes("iso8859-1"), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } SearchResult searchResult = searchService.search(queryString, page); //向页面传递参数 model.addAttribute("query", queryString); model.addAttribute("totalPages", searchResult.getPageCount()); model.addAttribute("itemList", searchResult.getItemList()); model.addAttribute("page", page); return "search"; } }
2.5 存在的问题
搜索结果中图片展示不出来,image字段中存储的图片是多张,使用逗号分隔。
修改方法:
Pojo:
package com.taotao.common.pojo; public class Item { private String id; private String title; private String sell_point; private long price; private String image; private String category_name; private String item_des; public String[] getImages() { if(image != null) { String[] images = image.split(","); return images; } return null; }
3 商品详情页面展示
3.1 需求分析
需要在taotao-portal中调用taotao-rest发布的服务,查询商品详情。
1、商品的基本信息
2、商品的描述
3、商品的规格
当用户请求商品详情页面时,只需要把商品基本信息展示出来,为了快速响应用户。商品的描述可以延迟加载,延迟一秒钟加载。商品的规格参数按需加载,当用户点击商品规格参数的标签页此时再加载。
3.2 服务发布
需要在taotao-rest工程中发布服务
1、取商品基本信息的服务
2、取商品描述的服务
3、取商品规格的服务
需要把商品信息添加到缓存中。设置商品的过期时间,过期时间为一天。需要缓存同步。
3.2.1 取商品基本信息
Dao层
查询的表tb_item
Service层
接收商品id,根据商品id查询商品基本信息。返回一个商品的pojo,使用taotaoResult包装返回。
/** * 商品信息管理Service * * @author kangy * */ @Service public class ItemServiceImpl implements ItemService { @Autowired private TbItemMapper itemMapper; @Override public TaotaoResult getItemBaseInfo(long itemId) { // 根据商品ID查询商品信息 TbItem item = itemMapper.selectByPrimaryKey(itemId); // 使用TaoTaoresult包装一下 return TaotaoResult.ok(item); } }
Controller层
接收商品id调用Service查询商品信息,返回商品对象,使用TaotaoResult包装。
Url:/rest/item/info/{itemId}
@RestController @RequestMapping("/item") public class ItemController { @Autowired private ItemService itemService; @RequestMapping("/info/{itemId}") public TaotaoResult getItemBaseInfo(@PathVariable Long itemId) { TaotaoResult result = itemService.getItemBaseInfo(itemId); return result; } }
添加缓存逻辑
Redis的hash类型中的key是不能设置过期时间。如果还需要对key进行分类可以使用折中的方案。
Key的命名方式:
Itheima:javaee16:01=刘备
Itheima:javaee16:02=张飞
商品key的定义:
基本信息:
REDIS_ITEM_KEY:商品id:base=json
描述:
REDIS_ITEM_KEY:商品id:desc=json
规格参数:
REDIS_ITEM_KEY:商品id:param=json
/** * 商品信息管理Service * * @author kangy * */ @Service public class ItemServiceImpl implements ItemService { @Autowired private TbItemMapper itemMapper; @Value("${REDIS_ITEM_KEY}") private String REDIS_ITEM_KEY; @Value("${REDIS_ITEM_EXPIRE}") private Integer REDIS_ITEM_EXPIRE; @Autowired private JedisClient jedisClient; @Override public TaotaoResult getItemBaseInfo(long itemId) { try { // 添加缓存逻辑 // 从缓存中取商品信息,商品id对应的信息 String json = jedisClient.get(REDIS_ITEM_KEY + ":" + itemId + ":base"); // 判断是否有值 // commons.lang3.StringUtils if (!StringUtils.isBlank(json)) { // 把json转换成java对象 TbItem item = JsonUtils.jsonToPojo(json, TbItem.class); return TaotaoResult.ok(item); } } catch (Exception e) { e.printStackTrace(); } // 根据商品ID查询商品信息 TbItem item = itemMapper.selectByPrimaryKey(itemId); // 使用TaoTaoresult包装一下 try { // 把商品信息写入缓存 jedisClient.set(REDIS_ITEM_KEY + ":" + itemId + ":base", JsonUtils.objectToJson(item)); // 设置key的有效期 jedisClient.expire(REDIS_ITEM_KEY + ":" + itemId + ":base", REDIS_ITEM_EXPIRE); } catch (Exception e) { e.printStackTrace(); } return TaotaoResult.ok(item); } }
3.2.2 取商品描述信息
根据商品id取商品描述信息。单表查询tb_item_desc。
Dao层
使用逆向工程
Service层
接收商品id根据商品id查询商品描述。返回商品描述的pojo。使用TaotaoResult包装。
需要添加缓存逻辑。
==========================================================
参考资料:
end