首页 > 其他分享 >springboot整合项目-商城项目展示购物车商品数量增加or减少功能

springboot整合项目-商城项目展示购物车商品数量增加or减少功能

时间:2022-11-09 14:02:47浏览次数:38  
标签:username springboot cid 项目 param 购物车 num Integer uid

增加or减少购物车商品数量

持久层

1.sql规划

1.增加之前先判断是否存在这个数据

select * from t_cart where cid = #{cid}

增加购物车数量,就是update操作

update  t_cart set num=#{num},modified_time =? modified_user =? where cid = #{cid}

2.接口与抽象方法

  /**
     * 根据cid查找此条数据是否存在
     * @param cid
     * @return
     */
   Cart findByCid(Integer cid);

业务层

1.规划异常

1.更新时异常 updateException
2.找不到此条数据 CartNotFoundException

2.接口与方法的设计

 /**
     * 增加购物车里面的商品数量
     * @param cid
     * @param uid
     * @param username
     * @return
     */
  Integer updateNumByCid(Integer cid,Integer uid,String username);

  /**
   * 减少购物车里面的商品数量
   * @param cid
   * @param uid
   * @param username
   * @return
   */
  Integer reduceNum(Integer cid,Integer uid,String username);
 /**
     * 增加购物车里的数量
     * @param cid
     * @param uid
     * @param username
     * @return
     */
    @Override
    public Integer updateNumByCid(Integer cid, Integer uid, String username) {
        //先查找此数据是否存在
        Cart result = cartMapper.findByCid(cid);
        if (result == null) {
            throw new CartNotFoundException("此商品找不到的异常");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("访问非法");
        }

        int num = result.getNum()+1;
        Integer integer = cartMapper.updateNumerByCid(cid, num, username, new Date());
        if (integer !=1){
            throw new UpdateException("更新时产生了位置的异常");
        }
        return num;
    }

    /**
     * 减少购物车的数量
     * @param cid
     * @param uid
     * @param username
     * @return
     */
    @Override
    public Integer reduceNum(Integer cid, Integer uid, String username) {
        //先查找此数据是否存在
        Cart result = cartMapper.findByCid(cid);
        if (result == null) {
            throw new CartNotFoundException("此商品找不到的异常");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("访问非法");
        }

        int num = result.getNum()-1;
        Integer integer = cartMapper.updateNumerByCid(cid, num, username, new Date());
        if (integer !=1){
            throw new UpdateException("更新时产生了位置的异常");
        }
        return num;
    }

测试


控制层

1.异常处理

else if (e instanceof CartNotFoundException) {
            result.setState(4007);
        }

2.请求设计

1.增加

/{cid}/num/add

/post

/HttpSession session Integer cid

/JsonResult

2.减少

/{cid}/num/reduce

/post

/HttpSession session Integer cid

/JsonResult

3.方法以及实现的逻辑

   /**
     * 增加购物车某个商品的数量
     * @param cid
     * @param session
     * @return
     */
    @RequestMapping("{cid}/num/add")
    public JsonResult<Integer> addNum(@PathVariable("cid") Integer cid, HttpSession session) {
        // 从Session中获取uid和username
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        // 调用业务对象执行增加数量
        Integer data = cartService.updateNumByCid(cid, uid, username);
        // 返回成功
        return new JsonResult<Integer>(OK, data);
    }

    /**
     * 减少某个商品的数量
     * @param cid
     * @param session
     * @return
     */
    @RequestMapping("{cid}/num/reduce")
    public JsonResult<Integer> reduceNum(@PathVariable("cid") Integer cid, HttpSession session) {
        // 从Session中获取uid和username
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        // 调用业务对象执行增加数量
        Integer data = cartService.reduceNum(cid,uid,username);
        // 返回成功
        return new JsonResult<Integer>(OK, data);
    }

前端页面

1.

function reduceNum(cid) {
				$.ajax({
					url: "/carts/" + cid + "/num/reduce",
					type: "POST",
					dataType: "JSON",
					success: function(json) {
						if (json.state == 200) {
							// showCartList();
							$("#num-" - cid).val(json.data);
							let price = $("#price-" - cid).html();
							let totalPrice = price * json.data;
							$("#total-price-" - cid).html(totalPrice);
							showCartList();
						} else {
							alert("增加商品数量失败!" + json.message);
						}
					},
					error: function(xhr) {
						alert("您的登录信息已经过期,请重新登录!HTTP响应码:" + xhr.status);
						location.href = "login.html";
					}
				});
			};
			function addNum(cid) {
				$.ajax({
					url: "/carts/" + cid + "/num/add",
					type: "POST",
					dataType: "JSON",
					success: function(json) {
						if (json.state == 200) {
							// showCartList();
							$("#num-" + cid).val(json.data);
							let price = $("#price-" + cid).html();
							let totalPrice = price * json.data;
							$("#total-price-" +cid).html(totalPrice);
						} else {
							alert("增加商品数量失败!" + json.message);
						}
					},
					error: function(xhr) {
						alert("您的登录信息已经过期,请重新登录!HTTP响应码:" + xhr.status);
						location.href = "login.html";
					}
				});
			};

标签:username,springboot,cid,项目,param,购物车,num,Integer,uid
From: https://www.cnblogs.com/wiseleer/p/16873381.html

相关文章

  • springboot项目整合-商城项目实现删除收货地址
    删除收货地址1.持久层1.1规划sql语句1.在删除之前需要判断该数据是否存在,判断该条数据的归属是否是当前的用户,不需要重复开发2.执行删除收货地址deletefromt_addre......
  • springboot项目整合-商城项目实现热销商品功能
    商品热销排行持久层1.sql语句SELECT*FROMt_productWHEREstatus=1ORDERBYpriorityDESCLIMIT0,42.接口和方法/***查询热销商品的前四名*@re......
  • springboot项目-商城项目实现购物车功能
    购物车模块加入购物车持久层1.规划持久层的语句1.向购物车表中插入数据insertintot_cart(aid除外)values(值列表)2.当当前的商品已经在购物车中存在,则直接更新......
  • springboot整合项目-商城项目展示购物车功能
    展示购物车列表1.持久层1.规划sql语句#多表查询如果字段不重复则不需要要声明字段属于那种表selectcid,uid,pid,t_cart.price,t_cart.num,t_product.title,t_product.......
  • 我服了!SpringBoot升级后这服务我一个星期都没跑起来!(上)
    最近由于各方面的原因在准备升级SpringCloud和SpringBoot,经过一系列前置的调研和分析,决定把SpringBoot相关版本从2.1.6升级到2.7.5,SpringCloud相关版本从Green......
  • threejs三维地图大屏项目分享
    这是最近公司的一个项目。客户的需求是基于总公司和子公司的数据,开发一个数据展示大屏。大屏两边都是一些图表展示数据,中间部分是一个三维中国地图,点击中国地图的某个省份......
  • 尚硅谷java入门b站零基础 异常处理 +多线程+部分项目三 2022.3.26
    380如何自定义异常/**如何自定义异常类?*1.继承于现有的异常结构:RuntimeException、Exception*2.提供全局常量:serialVersionUID*3.提供重载的构造器**/publicc......
  • 9601项目
    改编译文件时,去英文文件修改对应的参数然后再点击工具,更新编译,然后发布 然后把编译生成的qm文件考到文件夹中    然后再构建就OK了 ......
  • 如果springboot项目缺少test模块
    操作步骤1.选择projectstructure2.选中缺少测试模块的服务,点src目录下右键点NewFlder新建test3.讲该文件夹标记为test模块4.接下来就可以建包建测试类了......
  • 使用github管理iOS分布式项目开发
    使用github管理iOS分布式项目开发 在我们iOS项目开发过程中,我们的团队成员都是分散的、分布式,这个项目管理带来了挑战。Git是一个能够进行版本管理的软件,它是cvs和svn的未......