• SpringBoot项目--电脑商城【显示勾选的购物车数据】


    1.持久层[Mapper]

    1.规划SQL语句

    用户在购物车列表页中通过随机勾选相关的商品,在点击“结算”按钮后,跳转到结算页面,在这个页面中需要展示用户在上个页面所勾选的购物车对应的数据,列表的展示,而展示的内容还是在于购物车的表,两个页面需要用户勾选多个值传递给下一个

    1. select
    2. cid, #日后勾选购物车商品模块需要用到cid来确定勾选的是购物车表的哪一条数据
    3. uid, #感觉没必要,因为uid可以从session中拿的呀,难道是为
    4. #了后面提交购物车订单时判断提交的商品的uid和登录的uid是否一致?
    5. pid, #日购提交订单模块需要用到pid来确定购买的是商品表的哪件商
    6. #品,然后对商品表的该商品的库存,销售热度等信息进行修改
    7. t_cart.price, #两个表都有该字段,需要指定获取的是哪个数据表的
    8. t_cart.num, #两个表都有该字段且含义不同,需要指定获取的是哪个数据表的
    9. title,
    10. t_product.price as realPrice, #为了在购物车列表页展示两个价格的差值
    11. image
    12. from t_cart
    13. left join t_product on t_cart.pid = t_product.id #把t_cart作为主表(老师说现在处理的是购物车表的数据所以让其为主表,我不明白)
    14. where
    15. cid in (?,?,?)
    16. order by
    17. t_cart.created_time desc #进行排序使最新加入购物车的在最上面

    2. 接口和抽象方法

    1. /**
    2. * 用于:显示勾选购物车数据
    3. * 根据购物车的id将用户选中的商品传递给“结算”页面
    4. * @param cid
    5. * @return
    6. */
    7. List findVOByCid(Integer cid);

    3.配置映射

    1. <select id="findVOByCid" resultMap="CartEntityMap">
    2. SELECT cid, uid, pid,
    3. t_cart.price, t_cart.num,
    4. t_product.title, t_product.image,
    5. t_product.price AS realprice
    6. FROM t_cart
    7. LEFT JOIN t_product
    8. ON t_cart.pid = t_product.id
    9. WHERE cid in (
    10. /*
    11. collection:是什么类型
    12. */
    13. <foreach collection="array" separator="," item="cid">
    14. #{cid}
    15. foreach>
    16. )
    17. ORDER BY t_cart.created_time DESC
    18. select>

    4.单元测试

    1. @Test
    2. public void findVOByCid(){
    3. Integer[] cids={1,2,3,45,6,9};
    4. System.out.println(cartMapper.findVOByCid(cids));
    5. }

    2.业务层[Service]

    1.异常规划

    因为是查询语句,所以没有异常处理

    2.设计业务层接口抽象方法

    1. /**
    2. * 将用户在购物车页面中勾选中的数据传递给"结算“页面
    3. * @param uid
    4. * @param cids
    5. * @return
    6. */
    7. List getVOByCid(Integer uid,
    8. Integer[] cids);

    3.完成抽象方法的设计

    1. /**
    2. * 将用户在购物车页面中勾选中的数据传递给"结算“页面
    3. *
    4. * @param uid
    5. * @param cids
    6. * @return
    7. */
    8. @Override
    9. public List getVOByCid(Integer uid, Integer[] cids) {
    10. List list = cartMapper.findVOByCid(cids);
    11. //循环遍历获取到的数据,查看该数据是否与目前传入的uid是一致的
    12. Iterator it = list.iterator();
    13. while (it.hasNext()){//如果循环没有遍历结束
    14. //因为it一开始是指向下标为0,所以一上来就要next,才可以指向下一个
    15. CartVO cartVO = it.next();
    16. //进行判断用户对应是否正确
    17. if (cartVO.getUid().equals(uid)) {//表示当前数据不属于当前的用户
    18. //从集合中移除这个元素
    19. list.remove(cartVO);
    20. }
    21. }
    22. return list;
    23. }

    3.控制层{Controller]

    1.请求设计

    • /cart/list
    • Integeer cids,HttpSession session
    • POST
    • JsonResult>

    2.完成请求处理

    1. /**
    2. * 将用户在购物车选中的商品通过”结算“传递到结算页面
    3. * @param cids
    4. * @param session
    5. * @return
    6. */
    7. @PostMapping("/list")
    8. public JsonResult> getVOByCid(Integer[] cids,
    9. HttpSession session){
    10. List data = cartService.getVOByCid(getuidFromSession(session), cids);
    11. return new JsonResult<>(OK,data);
    12. }

    4.前端页面

    1.将cart.html页面的”结算“按钮属性更改为”type=submit"

    2.注释调用orderConfirm.html中的js代码

    
    

    3.orderConfirm.html页面中添加自动加载从上一个页面中传递过来的cids数据,再起请求ajax,中进行填充当前页面的某一个区域中

    1. function showCartList() {
    2. $("#cart-list").empty();
    3. /*
    4. * location.search.substr:是获取url中的数据
    5. * 1:表示查找?之前
    6. * 0:表示查找?之后【请求参数】
    7. *
    8. * 这里要传data是因为后端接口需要传入参数,所以这里才要写
    9. * */
    10. $.ajax({
    11. url: "/cart/list",
    12. data: location.search.substr(1),
    13. type: "GET",
    14. dataType: "JSON",
    15. success: function(json) {
    16. let list = json.data;
    17. console.log("count=" + list.length);
    18. let allCount = 0;
    19. let allPrice = 0;
    20. for (let i = 0; i < list.length; i++) {
    21. console.log(list[i].title);
    22. let tr = ''
    23. + ''
    24. + '#{title}'
    25. + #{realPrice}'
    26. + '#{num}'
    27. + #{totalPrice}'
    28. + '';
    29. tr = tr.replace(/#{cid}/g, list[i].cid);
    30. tr = tr.replace(/#{image}/g, list[i].image);
    31. tr = tr.replace(/#{title}/g, list[i].title);
    32. tr = tr.replace(/#{realPrice}/g, list[i].realPrice);
    33. tr = tr.replace(/#{num}/g, list[i].num);
    34. tr = tr.replace(/#{totalPrice}/g, list[i].realPrice * list[i].num);
    35. $("#cart-list").append(tr);
    36. allCount += list[i].num;
    37. allPrice += list[i].realPrice * list[i].num;
    38. }
    39. $("#all-count").html(allCount);
    40. $("#all-price").html(allPrice);
    41. },
    42. error: function() {
    43. alert("购物车数据加载未知异常 + 原因:" + xhr.status);
    44. }
    45. });
    46. }
  • 相关阅读:
    ArcGIS_修改文本样式
    我们的爱恨情仇:人性-关系-危机-和谐的科学研究
    【Node.js】-闲聊:前端框架发展史
    Kafka - 3.x 图解Broker总体工作流程
    含文档+PPT+源码等]精品基于Nodejs实现的学生宿舍管理系统[包运行成功]Nodejs毕业设计计算机项目源码
    [多线程] | 实例演示三种创建多线程的方式,初识线程同步以及解决线程安全问题(超卖)
    react+antd引入并修改样式,配置别名路径
    【C++】初窥C++
    推荐系统的下一步?阿里时空聚合GNN,效果吊打LightGCN!
    node+vue3+mysql前后分离开发范式——实现视频文件上传并渲染
  • 原文地址:https://blog.csdn.net/m0_63077733/article/details/132830780