一、购物车记录列表接口:
根据一个ID的集合,查询购物车记录,实现方式有两种:
1.动态SQL
<select id="searchShoppingCartById" resultMap="ShopCartMap">
SELECT * FROM shopping_cart WHERE cart_id IN
<foreach collection="list" item="cid" separator="," open="(" close=")">
#{cid}
</foreach>
</select>
2.tkMapper条件查询
critera.andIn("cartId",ids);
Service接口:
@Override
public ResultVO listShoppingCartsByCids(List<Integer> cids) {
Example example = new Example(ShoppingCart.class);
Example.Criteria criteria = example.createCriteria();
criteria.andIn("cartId",cids);
List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByExample(example);
return null;
}
这样查询出来的购物车中没有商品图片、商品名称,单价等信息,不满足需求。
二、数据库实现
1.ShoppingCartMapper:
public List<ShoppingCartVO> selectShopcartByCids(List<Integer> cids);
2.映射配置(动态sql):
<select id="selectShopcartByCids" resultMap="ShoppingCartVOMap">
SELECT c.cart_id,
c.product_id,
c.sku_id,
c.user_id,
c.cart_num,
c.cart_time,
c.product_price,
c.sku_props,
p.product_name,
i.url,
s.original_price,
s.sell_price,
s.sku_name
FROM shopping_cart c
INNER JOIN product p
INNER JOIN product_img i
INNER JOIN product_sku s
ON c.product_id=p.product_id
AND i.item_id=p.product_id
AND c.sku_id=s.sku_id
WHERE i.is_main=1 and c.cart_id IN
<foreach collection="cids" item="cid" separator="," open="(" close=")">
#{cid}
</foreach>
</select>
3.单元测试
@Test
public void testShopCart(){
//List<CategoryVO> categoryVOS = categoryMapper.selectFirstLevelCategories();
List<Integer> cids = new ArrayList<>();
cids.add(1);
cids.add(9);
cids.add(15);
List<ShoppingCartVO> list = shoppingCartMapper.selectShopcartByCids(cids);
System.out.println(list);
}
三、接口实现
ShoppingCartMapper:
public List<ShoppingCartVO> selectShopcartByCids(String cids);
listShoppingCartsByCids:
@Override
public ResultVO listShoppingCartsByCids(String cids) {
//使用tkMapper查询出来的购物车中没有商品图片、商品名称,单价等信息,不满足需求。
// Example example = new Example(ShoppingCart.class);
// Example.Criteria criteria = example.createCriteria();
// criteria.andIn("cartId",cids);
//
// List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByExample(example);
String[] arr = cids.split(",");
List<Integer> cartIds = new ArrayList<>();
for (int i = 0; i<arr.length;i++){
cartIds.add(Integer.parseInt(arr[i]));
}
List<ShoppingCartVO> list = shoppingCartMapper.selectShopcartByCids(cartIds);
ResultVO resultVO = new ResultVO(ResStatus.OK, "success", list);
return resultVO;
}
四、控制层实现:
ShopcartController:
@GetMapping("/listbycids")
@ApiImplicitParam(dataType = "String",name = "cids",value = "选择的购物车记录的id",required = true)
public ResultVO listByCids(String cids, @RequestHeader("token")String token){
ResultVO resultVO = shoppingCartService.listShoppingCartsByCids(cids);
return resultVO;
}
测试:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/128093.html