1、添加商品到购物车
//添加商品(给用户01添加两个商品,默认数量为1) redisTemplate.boundHashOps("01").put("1001", "1"); redisTemplate.boundHashOps("01").put("1002", "1");
2、查询购物车所有商品
redis中的1001商品信息 redisTemplate.boundHashOps("1001").put("product_sub_title", "商品副标题(卖点)"); redisTemplate.boundHashOps("1001").put("price", "商品价格"); redisTemplate.boundHashOps("1001").put("goodName", "商品名");//查询01用户的购物车所有商品 List goodList = new ArrayList<Good>(); Map carlist = redisTemplate.boundHashOps("01").entries(); carlist.forEach((k,v)->{ Good good = new Good(); good.setGoodId(k.toString()); Map good1 = redisTemplate.boundHashOps(k).entries(); good1.forEach((k1,v1)->{ if("product_sub_title".equals(k1.toString())){ good.setProductSubTitle(v1.toString()); } if("price".equals(k1.toString())){ good.setPrice(v1.toString()); } if("goodName".equals(k1.toString())){ good.setGoodName(v1.toString()); } }); goodList.add(good); });return goodList;
此处可以优化,批量查询01用户下的所有商品信息,性能更好
2.0 返回01用户所有商品
[{"goodId":"1001","productSubTitle":"商品副标题(卖点)","price":"商品价格","goodName":"商品名"},{"goodId":"1002","productSubTitle":null,"price":null,"goodName":null}]
2.1商品实体
public class Good { private String goodId; private String productSubTitle; private String price; private String goodName; public String getGoodId() { return goodId; } public void setGoodId(String goodId) { this.goodId = goodId; } public String getProductSubTitle() { return productSubTitle; } public void setProductSubTitle(String productSubTitle) { this.productSubTitle = productSubTitle; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getGoodName() { return goodName; } public void setGoodName(String goodName) { this.goodName = goodName; } }
3、修改商品数量
//修改商品1002数量为8 redisTemplate.boundHashOps("01").put("1002", "8");