## 建表
create table t_cart(id int primary key auto_increment, //主键 goods_id varchar(200), //商品的id uid int, //用户的id num int, //商品的数量 created_user varchar(50), created_time datetime, modified_user varchar(50), modified_time datetime )default charset=utf8;
/** * 购物车的值对象 * 用于接收多表连接查询的结果 * @author chenjiabing */ public class CartVoimplements Serializable{ private static final long serialVersionUID = 8904622535687816912L; private Integer id; //主键 购物车表中的主键 private String goodsId; //商品的id private Integer uid; //用户id private String image; //图片地址 private String title; //商品标题 private Integer price; //商品价格 private Integer num; //数量 public Integer getId(){ return id; } public void setId(Integer id){ this.id = id; } public String getGoodsId(){ return goodsId; } public void setGoodsId(String goodsId){ this.goodsId = goodsId; } public Integer getUid(){ return uid; } public void setUid(Integer uid){ this.uid = uid; } public String getImage(){ return image; } public void setImage(String image){ this.image = image; } public String getTitle(){ return title; } public void setTitle(String title){ this.title = title; } public Integer getPrice(){ return price; } public void setPrice(Integer price){ this.price = price; } public Integer getNum(){ return num; } public void setNum(Integer num){ this.num = num; } @Override public String toString(){ return "CartVo [id=" + id + ", goodsId=" + goodsId + ", uid=" + uid + ", image=" + image + ", title=" + title + ", price=" + price + ", num=" + num + "]"; } }
CartMapper.java
/** * 根据uid查询购物车中的所有商品 * @param uid 用户id * @return 查询的结果 */ List<CartVo>selectCartByUid(Integer uid);
CartMapper.xml
<!-- List<CartVo> selectCartByUid(Integer uid); --> <selectid="selectCartByUid"resultType="cn.tedu.store.bean.CartVo"> select c.id as id, c.uid as uid, goods_id as goodsId, image, title, c.num as num, price from t_cart c,t_goods g where c.uid=#{uid} and c.goods_id=g.id </select>
delete from t_cart where id in (1,2,3,4,4)
,因此参数应该是一个数组 @Param()
来指定 /** * 根据id删除购物车中的商品 * @param ids */ void deleteCartById(@Param("ids")Integer[] ids);
<forEach>
遍历数组中的元素 <!-- void deleteCartById(@Param("ids")Integer[] ids); 批量删除 --> <deleteid="deleteCartById"parameterType="java.lang.Integer"> delete from t_cart where id in <!-- 遍历数组ids collection:需要遍历的数组 item: 数组中的每一个值 open : 开始的内容 close: 结束的内容 separator :每个元素的分割符 最后拼接的就是 (id,id,id,id,id) --> <foreachcollection="ids"item="id"open="("separator=","close=")"> #{id} </foreach> </delete>
id
/** * 批量删除商品 * @param Itemids 数组,其中全是id * @return */ @RequestMapping("/moveCartBatch.do") public String moveCartBatch(Integer[] Itemids){ System.out.println(Itemids); cartService.moveCartById(Itemids); return "redirect:../cart/showCart.do"; }
delimiter $$ create procedure deleteCart(pid int) begin delete from t_cart where id=pid; end $$
drop procedure 存储过程名称 比如: drop procedure deleteCart;
call deleteCart(5);
CartMapper.java
中定义方法 /** * 调用存储过程 deleteCart(pid int)删除数据 * @param id */ void deleteCartByIdProcdure(Integer id);
CartMapper.xml
中定义节点 <!-- void deleteCartByIdProcdure(Integer id); 使用存储过程删除 --> <deleteid="deleteCartByIdProcdure"> {call deleteCart(#{id})} </delete>
## 修改购物车的数量