• 千峰商城-springboot项目搭建-78-购物车结算-查询购物车记录接口


    一、购物车记录列表接口:
    根据一个ID的集合,查询购物车记录,实现方式有两种:
    1.动态SQL
    1. <select id="searchShoppingCartById" resultMap="ShopCartMap">
    2. SELECT * FROM shopping_cart WHERE cart_id IN
    3. <foreach collection="list" item="cid" separator="," open="(" close=")">
    4. #{cid}
    5. </foreach>
    6. elect>

     2.tkMapper条件查询

    critera.andIn("cartId",ids);
    

    Service接口:

    1. @Override
    2. public ResultVO listShoppingCartsByCids(List cids) {
    3. Example example = new Example(ShoppingCart.class);
    4. Example.Criteria criteria = example.createCriteria();
    5. criteria.andIn("cartId",cids);
    6. List shoppingCarts = shoppingCartMapper.selectByExample(example);
    7. return null;
    8. }

    这样查询出来的购物车中没有商品图片、商品名称,单价等信息,不满足需求。

    二、数据库实现

    1.ShoppingCartMapper:

        public List<ShoppingCartVO> selectShopcartByCids(List<Integer> cids);
    

    2.映射配置(动态sql):

    1. <select id="selectShopcartByCids" resultMap="ShoppingCartVOMap">
    2. SELECT c.cart_id,
    3. c.product_id,
    4. c.sku_id,
    5. c.user_id,
    6. c.cart_num,
    7. c.cart_time,
    8. c.product_price,
    9. c.sku_props,
    10. p.product_name,
    11. i.url,
    12. s.original_price,
    13. s.sell_price,
    14. s.sku_name
    15. FROM shopping_cart c
    16. INNER JOIN product p
    17. INNER JOIN product_img i
    18. INNER JOIN product_sku s
    19. ON c.product_id=p.product_id
    20. AND i.item_id=p.product_id
    21. AND c.sku_id=s.sku_id
    22. WHERE i.is_main=1 and c.cart_id IN
    23. <foreach collection="cids" item="cid" separator="," open="(" close=")">
    24. #{cid}
    25. </foreach>
    26. elect>

     3.单元测试

    1. @Test
    2. public void testShopCart(){
    3. //List categoryVOS = categoryMapper.selectFirstLevelCategories();
    4. List cids = new ArrayList<>();
    5. cids.add(1);
    6. cids.add(9);
    7. cids.add(15);
    8. List list = shoppingCartMapper.selectShopcartByCids(cids);
    9. System.out.println(list);
    10. }

     三、接口实现

    ShoppingCartMapper:
    
     public List 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 shoppingCarts = shoppingCartMapper.selectByExample(example); String[] arr = cids.split(","); List cartIds = new ArrayList<>(); for (int i = 0; i){ cartIds.add(Integer.parseInt(arr[i])); } List list = shoppingCartMapper.selectShopcartByCids(cartIds); ResultVO resultVO = new ResultVO(ResStatus.OK, "success", list); return resultVO; }
    
     
    

     四、控制层实现:

    ShopcartController:
    
    1. @GetMapping("/listbycids")
    2. @ApiImplicitParam(dataType = "String",name = "cids",value = "选择的购物车记录的id",required = true)
    3. public ResultVO listByCids(String cids, @RequestHeader("token")String token){
    4. ResultVO resultVO = shoppingCartService.listShoppingCartsByCids(cids);
    5. return resultVO;
    6. }

     测试:

  • 相关阅读:
    操作系统(3)进程管理(中)数据一致性
    Map中key和value值是否可以为null或空字符串?
    使用 matlab 的 Robotics ToolBox 完成5自由雄克机械臂的建模与运动学仿真,并计算8个点的运动轨迹,绘制运动动画
    【第47篇】BoT-SORT:强大的关联多行人跟踪
    SpringBoot整合MybatisPlus多线程下切换数据源的设计方案
    Vue+element搭建后台管理系统-二、安装插件
    BGP联盟实验
    如何用 Sysbench 测试 TiDB
    计算机视觉所需要的数学基础
    前端Vue拖拽功能
  • 原文地址:https://blog.csdn.net/LYly_B/article/details/126011427