前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >购物车模块

购物车模块

作者头像
爱撒谎的男孩
发布2018-05-25 16:27:50
1.4K0
发布2018-05-25 16:27:50
举报

购物车模块

## 建表

  • 创建购物车的表
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;

显示购物车

定义值对象(XXXVo)

  • 当我们需要查询多张表的数据的时候,我们此时仅仅使用一个实体类来接收肯定是不行的,我们需要定义一个值对象来接收查询的多张表数据
  • 实现多表连接查询的结果接收
/**
 * 购物车的值对象
 * 用于接收多表连接查询的结果
 * @author chenjiabing
 */
public class CartVo implements 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);
	 -->
	 <select id="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>

批量删除购物车中的商品

持久层

  • 根据传入的id批量删除商品,用sql语句如下: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);
 	批量删除
  -->
  <delete id="deleteCartById" parameterType="java.lang.Integer">
  		delete from t_cart 
  		where
  		id in
  		<!-- 
  			遍历数组ids
  			collection:需要遍历的数组
  			item: 数组中的每一个值
  			open : 开始的内容
  			close: 结束的内容
  			separator :每个元素的分割符
  			最后拼接的就是  (id,id,id,id,id)
  		 -->
  		<foreach collection="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";
}

MySQL存储过程

创建存储过程

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);
	  	使用存储过程删除
	   -->
	   
	   	<delete id="deleteCartByIdProcdure">
	   		{call deleteCart(#{id})}
	   	</delete>

## 修改购物车的数量

持久层

创建存储过程
delimiter $$
create procedure updateNum(pid int,pnum int)
begin
	update t_cart set num=pnum where id=pid;
end $$
定义接口
写配置文件
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-05-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 购物车模块
    • 显示购物车
      • 定义值对象(XXXVo)
      • 持久层
    • 批量删除购物车中的商品
      • 持久层
      • 接口中定义方法
      • 配置文件中配置
      • 控制器层
    • MySQL存储过程
      • 创建存储过程
        • 删除存储过程
        • 调用
      • 使用存储过程删除一行数据
        • 持久层
    相关产品与服务
    云数据库 MySQL
    腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档