• 近期将进行后台系统升级,如有访问不畅,请稍后再试!
  • 极客文库-知识库上线!
  • 极客文库小编@勤劳的小蚂蚁,为您推荐每日资讯,欢迎关注!
  • 每日更新优质编程文章!
  • 更多功能模块开发中。。。

从零到企业级SSM电商项目实战教程(三十三)更新,删除购物车商品功能开发

文章目录[隐藏]

CartController.java

@RequestMapping("update.do")
@ResponseBody
public 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);
}

@RequestMapping("delete_product.do")
@ResponseBody
public 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);
}

ICartService.java

ServerResponse<CartVo> update(Integer userId,Integer productId,Integer count);

ServerResponse<CartVo> deleteProduct(Integer userId,String productIds);

CartServiceImpl.java

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.selectCartByUserIdProductId(userId,productId);
    if(cart != null){
        cart.setQuantity(count);
    }
    cartMapper.updateByPrimaryKeySelective(cart);
    CartVo cartVo = this.getCartVoLimit(userId);
    return ServerResponse.createBySuccess(cartVo);
}

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);
    CartVo cartVo = this.getCartVoLimit(userId);
    return ServerResponse.createBySuccess(cartVo);
}

CartMapper.java

int deleteByUserIdProductIds(@Param("userId") Integer userId,@Param("productIdList")List<String> productIdList);

CartMapper.xml

<delete id="deleteByUserIdProductIds" parameterType="map">
  delete from mmall_cart
  where 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>

喜欢 (0)
[247507792@qq.com]
分享 (0)
Geekerstar
关于作者:
本站技术支持

欢迎 注册账号 登录 发表评论!

  • 精品技术教程
  • 编程资源分享
  • 问答交流社区
  • 极客文库知识库

客服QQ


QQ:2248886839


工作时间:09:00-23:00