商品详情页面展示——淘淘商城(二十二)

商品信息服务发布

需求

  • 提供三个接口,分别提供商品基本信息、商品描述、商品参数,并设置缓存时间为1天。
  • 在taotao-rest中开发。
  • 商品基本信息:
    • 请求url:/item/base/{itemId}
    • 请求参数:url中的商品id
    • 响应信息:TaoTaoResult,包含商品基本信息。
  • 商品描述:
    • 请求url:/item/desc/{itemId}
    • 请求参数:url中的商品id
    • 响应信息:TaoTaoResult,包含商品描述。
  • 商品基本信息:
    • 请求url:/item/param/{itemId}
    • 请求参数:url中的商品id
    • 响应信息:TaoTaoResult,包含商品参数。

Service层

  • 在redis.properties中设置redis相关参数。
    • 商品基本信息在redis中缓存的key格式为:REDIS_ITEM_KEY:商品id:ITEM_BASE_KEY。
    • 这种以 : 分隔的key能够有层次的表示redis中的数据,并且弥补hash类型不能设置国企时间的缺陷。
      9.rediskey.png
    • 商品描述和参数的key格式类似。
#商品的key
REDIS_ITEM_KEY=item
#商品基础信息的key
ITEM_BASE_KEY=base
#商品描述的key
ITEM_DESC_KEY=desc
#商品参数的key
ITEM_PARAM_KEY=param

#商品过期时间
REDIS_ITEM_EXPIRE=86400
  • 创建ItemService和对应实现类,根据id获取商品基本信息、描述、参数,并使用TaoTaoResult包装。
    • 注意缓存的读取和写入,以及过期时间的设置。
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemMapper itemMapper;
    @Autowired
    private TbItemDescMapper itemDescMapper;
    @Autowired
    private TbItemParamItemMapper itemParamItemMapper;
    @Autowired
    private RedisDao redisDao;

    @Value("${REDIS_ITEM_KEY}")
    private String REDIS_ITEM_KEY;
    @Value("${ITEM_BASE_KEY}")
    private String ITEM_BASE_KEY;
    @Value("${ITEM_DESC_KEY}")
    private String ITEM_DESC_KEY;
    @Value("${ITEM_PARAM_KEY}")
    private String ITEM_PARAM_KEY;


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


    @Override
    public TaotaoResult getItemBaseInfo(long itemId) {
        //该商品redis中商品基本信息对应的key
        String key = REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_BASE_KEY;

        //缓存中读取
        try {
            String json = redisDao.get(key);
            if (!StringUtils.isBlank(json)) {
                TbItem item = JsonUtils.jsonToPojo(json, TbItem.class);
                return TaotaoResult.ok(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //查询基本信息
        TbItem item = itemMapper.selectByPrimaryKey(itemId);

        //写入缓存
        try {
            redisDao.set(key, JsonUtils.objectToJson(item));
            redisDao.expire(key, REDIS_ITEM_EXPIRE);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return TaotaoResult.ok(item);
    }

    @Override
    public TaotaoResult getItemDesc(long itemId) {
        //该商品redis中商品描述对应的key
        String key = REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_DESC_KEY;

        //读取缓存
        try {
            String json = redisDao.get(key);
            if (!StringUtils.isBlank(json)) {
                TbItemDesc itemDesc = JsonUtils.jsonToPojo(json, TbItemDesc.class);
                return TaotaoResult.ok(itemDesc);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //查询基本信息
        TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);

        //写入缓存
        try {
            redisDao.set(key, JsonUtils.objectToJson(itemDesc));
            redisDao.expire(key, REDIS_ITEM_EXPIRE);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return TaotaoResult.ok(itemDesc);
    }

    @Override
    public TaotaoResult getItemParam(long itemId) {
        //该商品redis中商品参数对应的key
        String key = REDIS_ITEM_KEY + ":" + itemId + ":" + ITEM_PARAM_KEY;

        //读取缓存
        try {
            String json = redisDao.get(key);
            if (!StringUtils.isBlank(json)) {
                TbItemParamItem itemParamItem = JsonUtils.jsonToPojo(json, TbItemParamItem.class);
                return TaotaoResult.ok(itemParamItem);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //查询基本信息
        TbItemParamItem itemParamItem = null;

        TbItemParamItemExample example = new TbItemParamItemExample();
        Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemParamItem> list = itemParamItemMapper.selectByExampleWithBLOBs(example);
        if (list != null && list.size() > 0) {
            itemParamItem = list.get(0);
        }

        //写入缓存
        try {
            redisDao.set(key, JsonUtils.objectToJson(itemParamItem));
            redisDao.expire(key, REDIS_ITEM_EXPIRE);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return TaotaoResult.ok(itemParamItem);
    }

}

Controller层

  • 创建ItemController,提供根据id访问基本信息、描述、参数的接口,返回包含它们的json。
@Controller
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @RequestMapping("/base/{itemId}")
    @ResponseBody
    public TaotaoResult getItemBaseInfo(@PathVariable Long itemId) {
        TaotaoResult result = itemService.getItemBaseInfo(itemId);
        return result;
    }

    @RequestMapping("/desc/{itemId}")
    @ResponseBody
    public TaotaoResult getItemDesc(@PathVariable Long itemId) {
        TaotaoResult result = itemService.getItemDesc(itemId);
        return result;
    }

    @RequestMapping("/param/{itemId}")
    @ResponseBody
    public TaotaoResult getItemParam(@PathVariable Long itemId) {
        TaotaoResult result = itemService.getItemParam(itemId);
        return result;
    }

}

调用服务展示商品信息

需求

分析

  • 搜索商品之后,点击链接,可展示商品详情页面,初始时包含商品基本信息。
    9.searchresult.png
  • item.jsp中,基本信息直接由jsp加载。
    9.base.png
  • 商品描述和参数由ajax加载。
    • 商品参数通过绑定点击事件,按需加载。
    • 商品描述是用定时器,延时1s加载。
      9.paramdesc.png
      9.paramdescadd.png

总结

  • 在taotao-portal中开发。
  • 商品基本信息:
    • 请求url:/item/{itemId}
    • 请求参数:商品id
    • 响应参数:商品详情页视图item.jsp,包含商品基本信息。
  • 商品描述:
    • 请求url:/item/desc/{itemId}
    • 请求参数:商品id
    • 响应参数:商品描述字符串。
  • 商品基本信息:
    • 请求url:/item/param/{itemId}
    • 请求参数:商品id
    • 响应参数:商品参数字符串(包含html片段)。

Service层

  • rest.properties中配置服务层需要调用的url。
#商品基本信息url
ITEM_BASE_URL=/item/base/
#商品描述url
ITEM_DESC_URL=/item/desc/
#商品参数url
ITEM_PARAM_URL=/item/param/
  • 创建ItemService和对应实现类,根据商品id,调用rest服务,组织数据返回商品信息、描述、参数。
@Service
public class ItemServiceImpl implements ItemService {

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

    @Override
    public Item getItemBase(long itemId) {
        try {
            String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_BASE_URL + itemId);
            if (!StringUtils.isBlank(json)) {
                TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, TbItem.class);
                if (taotaoResult.getStatus() == 200) {
                    TbItem tbItem = (TbItem) taotaoResult.getData();

                    Item item = new Item();
                    item.setImage(tbItem.getImage());
                    item.setId(String.valueOf(tbItem.getId()));
                    item.setPrice(tbItem.getPrice());
                    item.setSellPoint(tbItem.getSellPoint());
                    item.setTitle(tbItem.getTitle());

                    return item;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }

    @Override
    public String getItemDesc(Long itemId) {
        try {
            String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_DESC_URL + itemId);
            if (!StringUtils.isBlank(json)) {
                TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, TbItemDesc.class);
                if (taotaoResult.getStatus() == 200) {
                    TbItemDesc itemDesc = (TbItemDesc) taotaoResult.getData();
                    String desc = itemDesc.getItemDesc();
                    return desc;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    @Override
    public String getItemParam(Long itemId) {
        try {
            String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_PARAM_URL + itemId);
            if (!StringUtils.isBlank(json)) {
                TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, TbItemParamItem.class);
                if (taotaoResult.getStatus() == 200) {
                    TbItemParamItem itemParamItem = (TbItemParamItem) taotaoResult.getData();
                    String paramData = itemParamItem.getParamData();

                    //生成html
                    List<Map> jsonList = JsonUtils.jsonToList(paramData, Map.class);
                    StringBuffer sb = new StringBuffer();
                    sb.append(
                            "<table cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" border=\"0\" class=\"Ptable\">\n");
                    sb.append("    <tbody>\n");
                    for (Map m1 : jsonList) {
                        sb.append("        <tr>\n");
                        sb.append("            <th class=\"tdTitle\" colspan=\"2\">" + m1.get("group") + "</th>\n");
                        sb.append("        </tr>\n");
                        List<Map> list2 = (List<Map>) m1.get("params");
                        for (Map m2 : list2) {
                            sb.append("        <tr>\n");
                            sb.append("            <td class=\"tdTitle\">" + m2.get("k") + "</td>\n");
                            sb.append("            <td>" + m2.get("v") + "</td>\n");
                            sb.append("        </tr>\n");
                        }
                    }
                    sb.append("    </tbody>\n");
                    sb.append("</table>");
                    //返回html片段
                    return sb.toString();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";

    }
}

Controller层

  • 创建ItemController,在item视图中赋予商品基本信息的对象,并提供描述和参数的查询接口。
@Controller
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @RequestMapping("/{itemId}")
    public String showItem(@PathVariable Long itemId, Model model) {
        Item item = itemService.getItemBase(itemId);
        model.addAttribute("item", item);
        return "item";
    }

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

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

运行工程

  • 启动portal,rest,search,solr,nginx,从搜索结果点击商品链接。
  • 商品基本信息。
    9.itembase.png
  • 商品描述。
    9.itemdesc.png
  • 商品参数。
    9.param.png

猜你喜欢

转载自blog.csdn.net/qq_40369829/article/details/79948164