十六、增加购物车商品数量
1 持久层
-
规划SQL语句
//更新t_cart表num值(加入购物车功能已有) //判断商品信息是否存在 select * from t_cart where cid=#{cid}
-
设计接口和抽象方法
Cart findCartByCid(Integer cid);
-
编写xml映射文件
<select id="findCartByCid" resultMap="cart"> select * from t_cart where cid=#{cid} </select>
2 业务层
-
设计接口和抽象方法
Integer addNum(Integer cid,Integer uid,String username);
-
实现接口和抽象方法
@Override public Integer addNum(Integer cid, Integer uid, String username) { Cart cart = cartMapper.findCartByCid(cid); if (cart == null){ throw new CartNotFoundException("购物车商品信息不存在"); } Integer num = cart.getNum()+1; Integer integer = cartMapper.updateCart(cid, num, username, new Date()); if (integer != 1){ throw new UpdateException("增加购物车商品数量时发生异常"); } return num; }
3 控制层
-
设计请求
/url/{ cid}/num/add post Integer cid,HttpSession session JsonResult<Integer>
-
实现请求
@RequestMapping("{cid}/add/num")
public JsonResult<Integer> addNum(Integer cid,HttpSession session){
Integer data = cartService.addNum(cid, getUidFromSession(session), getUsernameFromSession(session));
return new JsonResult<>(OK,data);
}
十七、购物车结算
1 持久层
-
规划SQL语句
select cid,uid,pid, t_cart.price,t_cart.num, t_product.title,t_product.image, t_product.price as realPrice from t_cart left join t_product on t_cart.pid=t_product.id where cid in (?,?,?) order by t_cart.craeted_time desc
-
设计接口和抽象方法
List<CartVO> findOVByCid(Integer[] cid);
-
编写xml映射
<select id="findOVByCid" resultType="com.cy.store.vo.CartVO"> select cid, uid, pid, t_cart.price, t_cart.num, t_product.title, t_product.image, t_product.price as realPrice from t_cart left join t_product on t_cart.pid=t_product.id where cid in ( <foreach collection="array" item="cid" separator=","> #{cid} </foreach> ) order by t_cart.created_time desc </select>
2 业务层
-
设计接口和抽象方法
List<CartVO> getOVByCid(Integer uid,Integer[] cid);
-
实现接口和方法
@Override public List<CartVO> getOVByCid(Integer uid, Integer[] cid) { List<CartVO> list = cartMapper.findOVByCid(cid); Iterator<CartVO> it = list.iterator(); while (it.hasNext()){ CartVO cartVO = it.next(); if (!cartVO.getUid().equals(uid)){ list.remove(cartVO); } } return list; }
3 控制层
-
设计请求
/carts/list POST Integer[] cid,HttpSession session JsonResult<List<CartOV>>
-
实现请求
@RequestMapping("list") public JsonResult<List<CartVO>> getCartList(Integer[] cid, HttpSession session){ List<CartVO> data = cartService.getOVByCid(getUidFromSession(session), cid); return new JsonResult<>(OK,data); }
4 前端页面
//orderConfirm.html
<script type="text/javascript">
$(document).ready(function (