更新购物车,即修改购物车中的每个商品的参数,

1、接口编写:

1、更新购物车:

*Controller:

//    更新商品到购物车@RequestMapping("update.do")@ResponseBodypublic ServerResponse<CartVo> update(HttpSession session, Integer count, Integer productId){User user =(User) session.getAttribute(Const.CURRENT_USER);if(user == null){return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());}return iCartService.update(user.getId(),productId,count);}

*Service:

    //更新商品到购物车ServerResponse<CartVo> update(Integer userId,Integer productId,Integer count);

*ServiceImpl:

 //    更新商品到购物车public ServerResponse<CartVo> update(Integer userId, Integer productId, Integer count) {if (productId == null || count == null) {return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());}Cart cart = cartMapper.selectCatByUserIdProductId(userId, productId);if (cart != null) {cart.setQuantity(count);}cartMapper.updateByPrimaryKeySelective(cart);return this.list(userId);}

selectCatByUserIdProductId方法:

*Mapper:

//根据用户Id和产品Id去查购物车Cart selectCatByUserIdProductId(@Param("userId") Integer userId, @Param("productId") Integer productId);

*Mappler.xml:

<!--根据用户Id和产品Id来查询购物车--><select id="selectCatByUserIdProductId" resultMap="BaseResultMap" parameterType="map">select<include refid="Base_Column_List"/>from mmall_cartwhere user_id=#{userId}and product_id=#{productId}</select>
2、删除商品:

*Controller:

    //    移除购物车某个产品@RequestMapping("delete_product.do")@ResponseBodypublic ServerResponse<CartVo> deleteProduct(HttpSession session, String productIds){User user =(User) session.getAttribute(Const.CURRENT_USER);if(user == null){return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());}return iCartService.deleteProduct(user.getId(),productIds);}

*Service:

    //删除购物车中的商品ServerResponse<CartVo> deleteProduct(Integer userId,String productIds);

*ServiceImpl:

//删除购物车中的商品public ServerResponse<CartVo> deleteProduct(Integer userId, String productIds) {List<String> productList = Splitter.on(",").splitToList(productIds);if (CollectionUtils.isEmpty(productList)) {return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());}cartMapper.deleteByUserIdProductIds(userId, productList);return this.list(userId);}

deleteByUserIdProductIds方法:
*Mapper:

    //删除购物车中的商品int deleteByUserIdProductIds(@Param("userId") Integer userId,@Param("productIdList")List<String> productIdList);

*Mappler.xml:

  <delete id="deleteByUserIdProductIds" parameterType="map">delete from mmall_cartwhere user_id = #{userId}<if test="productIdList != null">and product_id in<foreach collection="productIdList" item="item" index="index" open="(" separator="," close=")">#{item}</foreach></if></delete>
其中上面的list是我们封装的一个返回购物车列表信息的接口,,

*Controller:

    //查询购物车中商品@RequestMapping("list.do")@ResponseBodypublic ServerResponse<CartVo> list(HttpSession session){User user =(User) session.getAttribute(Const.CURRENT_USER);if(user == null){return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());}return iCartService.list(user.getId());}

*Service:

    //查询购物车中商品ServerResponse<CartVo> list(Integer userId);

*ServiceImpl:

    //查询购物车中商品public ServerResponse<CartVo> list(Integer userId) {CartVo cartVo = this.getCartVoLimit(userId);return ServerResponse.createBySuccess(cartVo);}

重点看一下getCartVoLimit这个方法:

//封装的一个根据用户Id来获取购物车信息的方法private CartVo getCartVoLimit(Integer userId) {CartVo cartVo = new CartVo();List<Cart> cartList = cartMapper.selectCartByUserId(userId);List<CartProductVo> cartProductVoList = Lists.newArrayList();//设置购物车初始总价BigDecimal cartTotalPrice = new BigDecimal("0.0");if (CollectionUtils.isNotEmpty(cartList)) {for (Cart cartItem : cartList) {CartProductVo cartProductVo = new CartProductVo();cartProductVo.setId(cartItem.getId());cartProductVo.setUserId(userId);cartProductVo.setProductId(cartItem.getProductId());Product product = productMapper.selectByPrimaryKey(cartItem.getProductId());if (product != null) {cartProductVo.setProductMainImage(product.getMainImage());cartProductVo.setProductName(product.getName());cartProductVo.setProductSubtitle(product.getSubtitle());cartProductVo.setProductStatus(product.getStatus());cartProductVo.setProductPrice(product.getPrice());cartProductVo.setProductStock(product.getStock());//判断库存int buyLimitCount =0;if (product.getStock()>=cartItem.getQuantity()) {//库存充足的时候buyLimitCount = cartItem.getQuantity();cartProductVo.setLimitQuantity(Const.Cart.LIMIT_MUM_SUCCESS);} else {//库存的不足的时候buyLimitCount = product.getStock();cartProductVo.setLimitQuantity(Const.Cart.LIMIT_MUM_FAIL);//购物车中更新有效库存Cart cartForQuantity = new Cart();cartForQuantity.setId(cartItem.getId());cartForQuantity.setQuantity(buyLimitCount);cartMapper.updateByPrimaryKeySelective(cartForQuantity);}cartProductVo.setQuantity(buyLimitCount);//计算总价cartProductVo.setProductTotalPrice(BigDecimalUtil.mul(product.getPrice().doubleValue(), cartProductVo.getQuantity()));cartProductVo.setProductChecked(cartItem.getChecked());}if (cartItem.getChecked() == Const.Cart.CHECKED) {//如果已经勾选,增加到整个购物车总价中if(cartProductVo.getProductTotalPrice() == null){}cartTotalPrice = BigDecimalUtil.add(cartTotalPrice.doubleValue(), cartProductVo.getProductTotalPrice().doubleValue());}cartProductVoList.add(cartProductVo);}}cartVo.setCartTotalPrice(cartTotalPrice);cartVo.setCartProductVoList(cartProductVoList);cartVo.setAllChecked(this.getAllCheckedStatus(userId));cartVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix"));return cartVo;}

上面封装的getAllCheckedStatus方法:

    //封装的购物车中商品的是否选中状态private boolean getAllCheckedStatus(Integer userId) {if (userId == null) {return false;}return cartMapper.selectCartProductCheckedStatusByUserId(userId) == 0;}

selectCartProductCheckedStatusByUserId:
cartMapper:

    //根据用户Id查询商品是否被选中int selectCartProductCheckedStatusByUserId(Integer userId);

cartMapper.xml:

  <select id="selectCartProductCheckedStatusByUserId" resultType="int" parameterType="int" >select count(1) from mmall_cart where checked = 0 and user_id = #{userId}</select>

上面方法中使用的CartVo:

package com.mmall.vo;import java.math.BigDecimal;
import java.util.List;public class CartVo {private List<CartProductVo> cartProductVoList;private BigDecimal cartTotalPrice;private Boolean allChecked; //是否都勾选private String imageHost;public List<CartProductVo> getCartProductVoList() {return cartProductVoList;}public void setCartProductVoList(List<CartProductVo> cartProductVoList) {this.cartProductVoList = cartProductVoList;}public BigDecimal getCartTotalPrice() {return cartTotalPrice;}public void setCartTotalPrice(BigDecimal cartTotalPrice) {this.cartTotalPrice = cartTotalPrice;}public Boolean getAllChecked() {return allChecked;}public void setAllChecked(Boolean allChecked) {this.allChecked = allChecked;}public String getImageHost() {return imageHost;}public void setImageHost(String imageHost) {this.imageHost = imageHost;}
}

ProductVo:

package com.mmall.vo;import java.math.BigDecimal;public class CartProductVo {//结合了产品和购物车的一个抽象对象private Integer Id;private Integer userId;private Integer productId;private Integer quantity;//购物车中该商品的数量private String productName;private String productSubtitle;private String productMainImage;private BigDecimal productPrice;private Integer productStatus;private BigDecimal productTotalPrice;private Integer productStock;private Integer productChecked; //该产品是否勾选private String limitQuantity; //限制数量的一个返回结果public Integer getId() {return Id;}public void setId(Integer id) {Id = id;}public Integer getUserId() {return userId;}public void setUserId(Integer userId) {this.userId = userId;}public Integer getProductId() {return productId;}public void setProductId(Integer productId) {this.productId = productId;}public Integer getQuantity() {return quantity;}public void setQuantity(Integer quantity) {this.quantity = quantity;}public String getProductName() {return productName;}public void setProductName(String productName) {this.productName = productName;}public String getProductSubtitle() {return productSubtitle;}public void setProductSubtitle(String productSubtitle) {this.productSubtitle = productSubtitle;}public String getProductMainImage() {return productMainImage;}public void setProductMainImage(String productMainImage) {this.productMainImage = productMainImage;}public BigDecimal getProductPrice() {return productPrice;}public void setProductPrice(BigDecimal productPrice) {this.productPrice = productPrice;}public Integer getProductStatus() {return productStatus;}public void setProductStatus(Integer productStatus) {this.productStatus = productStatus;}public BigDecimal getProductTotalPrice() {return productTotalPrice;}public void setProductTotalPrice(BigDecimal productTotalPrice) {this.productTotalPrice = productTotalPrice;}public Integer getProductStock() {return productStock;}public void setProductStock(Integer productStock) {this.productStock = productStock;}public Integer getProductChecked() {return productChecked;}public void setProductChecked(Integer productChecked) {this.productChecked = productChecked;}public String getLimitQuantity() {return limitQuantity;}public void setLimitQuantity(String limitQuantity) {this.limitQuantity = limitQuantity;}
}

2、接口测试:

1、用户购物车信息接口测试:
image.png

2、更新商品数量到购物车接口测试:
image.png

3、移除购物车某个产品接口测试:我们可以传入商品信息id单个或多个进行删除~
image.png

20、【购物车模块】——更新、删除、查询购物车功能开发相关推荐

  1. 《小米商城》--购物车单条数据删除、购物车数量修改、清空购物车、查看地址功能、添加地址

    在购物车页面,有清空购物车方法以及按钮, 在controler里写出delete方法,现获取请求参数cid,然后传入参数cid调用deleteCartByCid方法,然后跳转到购物车展示功能 然后调用 ...

  2. FL Studio水果编曲20.8.4更新内容及新增功能介绍

    FL Studio 20.8.4与其他大多数音乐制作软件不同,该软件通过一个直观的工作窗口,只需一次点击即可进行音轨编辑.缩混工作,当然如果有需要您也可以把调音台窗口及编曲窗口分开,在一个紧凑的调音台 ...

  3. php购物车内物品删除,求助 购物车 用session删除 列表的一条

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 执行代码: include("data.php"); include("fuc.php"); session_st ...

  4. 【效能平台】接口模块——获取列表数据、查看详情数据、增加以及更新项目接口、删除接口相关功能开发(六)

    今日状态:充充实实 打卡学习 星期一 星期二 星期三 星期四 星期五 星期六 星期日 成功 暂无 暂无 暂无 暂无 暂无 暂无 一.获取接口列表数据 二.获取接口详情数据和更新数据 三.增加接口数据

  5. 企业级项目分享:购物车模块( 二) 21-06-09

    购物车模块( 二) 目录 购物车模块( 二) 前言 查看购物车 1.分析 2.接口 3.后端实现 3.1步骤一:修改CartService,添加 queryCartList 方法,从redis查询的购 ...

  6. mmall商城购物车模块总结

    购物车模块的设计思想 购物车的实现方式有很多,但是最常见的就三种:Cookie,Session,数据库.三种方法各有优劣,适合的场景各不相同.Cookie方法:通过把购物车中的商品数据写入Cookie ...

  7. EasyUI项目之门户(添加查询购物车与清空购物车)

    目标效果: 目标: 1,添加查询购物车 2,清空购物车 一,添加查询购物车 三种实现方法 0.1 session 保存购物车信息到session服务端 0.2 cookie保存购物车到本地(效率更高  ...

  8. 企业级项目分享:购物车模块(一)2021-06-08

    购物车模块 目录 购物车模块 前言 1.搭建购物车服务 1.1步骤一:创建changgou4-service-cart 项目 1.2步骤二:修改pom.xml文件,添加坐标 1.3步骤三:创建yml文 ...

  9. Java面向对象编程-模拟购物车模块

    总体架构 需求: 模拟购物车模块功能,需要实现添加商品到购物车中去,同是需要提供修改商品的购买数量,结算商品价格等功能 分析: ① 购物车中的每个商品都是一个对象,需要定义一个商品类 ② 购物车本身也 ...

  10. mmall购物车模块

    mmall购物车模块 cart数据库表设计 门户_购物车接口 业务需求 查询购物车list 方法复用getCartVoLimit cart数据库表设计 CREATE TABLE `mmall_cart ...

最新文章

  1. 《算法竞赛进阶指南》打卡-基本算法-AcWing 97. 约数之和:递归、快速幂
  2. 嘉实多RO150合成齿轮油
  3. POS机移动刷卡机自适应网站源码 dedecms织梦模板
  4. avue 文字点击 弹窗_目前最好用的文字转语音、视频配音方法,一键合成,智能黑科技...
  5. php100视频教程下载(全集),下载地址链接(整理后包涵解压密码)
  6. 深度解密HTTP通信细节
  7. unzip命令常用参数
  8. 计算机运行命令jar,jar文件打开教程
  9. 儿童机器人编程语言_儿童编程机器人
  10. 如何在高共模电压下测量小差分电压
  11. 阿里天猫小镇的实质就是为了圈地!
  12. 基于宏指令下的威纶通配方功能(RW位控制)
  13. 初为人师[/size]
  14. Is the American Dream Really Dead?
  15. 小学计算机课题研究方案,课题研究方案范文《小学信息技术课堂有效教学的探索》...
  16. 练习2-7 编写一个函数invert(x,p,n),该函数返回对x执行下列操作后的结果:将x从第p位开始的n个(二进制)位求反(即1变成0,0变成1),x的其余各位保持不变。
  17. mysql数据库BKA算法详解
  18. Android中的ShareSDK学习
  19. rg1 蓝光危害rg0_蓝光危害检测标准IEC/TR 62778
  20. 《游戏设计艺术(第2版)》——学习笔记(7)第7章 游戏始于一个创意

热门文章

  1. pytorch之tensor按索引赋值,三种方法!
  2. 教你怎么在vi和vim上查找字符串
  3. c语言程序设计现代方法(2th)第12章答案(自己胡乱编写的答案,持续更新)
  4. 猜想:汇编指令push和pop对sp的处理顺序缘由
  5. Master公式(计算递归复杂度)
  6. [BUUCTF-pwn]——pwn1_sctf_2016
  7. 华为驳斥鸿蒙六月上线,终于来了!华为鸿蒙6月初将正式上线手机
  8. centos 编译nginx php mariadb,centos7安装nginx+mariadb+php-fpm
  9. 多线程测试工具groboutils的使用
  10. 线程、进程、程序区别