用户在浏览商品的过程中,如果遇到心仪的商品往往会将其添加到购物车中,以下是一个示例展示了在获取前端传过来的商品信息之后后端如何执行添加购物车操作
1.新建数据库购物车表并关联对应的外键
我们知道,每一个用户只对应一个购物车,而每个购物车里可以有多个商品,这时候就需要对购物车表cart_list添加两个外键,使其与商品表good_list和用户表login_user1表关联起来,以便执行相应操作
在这里我们关联了good_list表里的good_list_id字段作为商品的唯一标识符,关联login_user1表里的user_account字段作为用户的唯一标识符
good_list表和login_user1表如下
这里我们手动添加了数据以便后续的测试
2.创建相关实体类
@Data
@Entity
public class CartList {
@Id
public Integer cartListId;
public Integer goodListId;
public Integer goodListCount;
public Integer userAccount;
public String addTime;
}
@Data
@Entity
public class GoodList {
@Id
private int goodListId; //商品编号
private String goodListName; //商品名称
private double goodListPrice; //商品单价
private String goodListPicture;
private String goodListBriefIntroduction;
private Integer categoryId;
}
3.创建CartMapper.xml
在resources目录下新建mappers包,在mappers包下新建CartMapper.xml,将数据库表字段与实体类字段对应
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.software.PetShop.mapper.CartMapper">
<resultMap id="cartResult" type="com.software.PetShop.entity.CartList">
<id column="cart_list_id" property="cartListId"/>
<id property="goodListId" column="good_list_id"/>
<id property="goodListCount" column="good_list_count"/>
<id property="addTime" column="add_time"/>
<!-- 添加userAccount属性的外键关联映射 -->
<id property="userAccount" column="user_account" />
</resultMap>
</mapper>
4.配置mybatis-config.xml
<mappers>
<mapper resource="mappers/CartMapper.xml"></mapper>
</mappers>
5.新建CartController,创建addCart()方法
在这里首先接收前端传过来的用户,商品以及商品数量信息,根据用户id和商品id来判断购物车的数据库中是否已经存在这样一个商品,如果商品之前就已经存在,就执行更新操作更新原商品的数量,如果商品不存在,那就添加购物车操作,向数据库表中插入相关的数据,最后返回执行状态
/**
* 添加购物车
**/
@PostMapping("/addCart")
public String addCart(@RequestBody String json){
//使用mybatis作为数据库连接池
SqlSession sqlSession = getSqlSession();
try {
JSONObject jsonObject = new JSONObject(json);
//获取前端传递的参数类型,获取用户,商品种类,数量
int goodListId = (int) jsonObject.get("goodId");
int goodListCount = (int) jsonObject.get("goodCount");
int userAccount = (int) jsonObject.get("userAccount");
System.out.println(goodListId);
JSONObject result = new JSONObject();
CartMapper cartMapper = sqlSession.getMapper(CartMapper.class);
//根据用户id和商品的id查询数据库中是否已经存在了这样一个商品
CartList answer =
cartMapper.findByGoodListIdAndUserAccount(goodListId,userAccount);
Random random = new Random();
int randomId = random.nextInt(); // 生成一个随机整数
//如果有就更新购物车里商品的数量
if (answer!=null){
cartMapper.updateCartList(goodListCount,goodListId,userAccount);
sqlSession.commit();
}else {
//没有就新增新的商品到购物车列表
answer = new CartList();
int cartListId = randomId;
String addTime = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss").format(new Date());
cartMapper.addNewCartList(cartListId,
userAccount,goodListId,goodListCount,addTime);
sqlSession.commit();
}
if (answer!=null){
result.put("status","ok");
}else{
result.put("status","no");
}
System.out.println(result);
return result.toString();
} finally {
sqlSession.close();
}
}
6.在CartMapper接口中实现对应方法
public interface CartMapper extends BaseMapper<CartList> {
//根据商品id和用户账号获取购物车列表
CartList findByGoodListIdAndUserAccount(int goodListId,int userAccount);
//添加新的购物车,插入新的数据
int addNewCartList(int cartListId, int userAccount, int goodListId,int
goodListCount,String addTime);
}
7.在CartMapper.xml中实现sql语句
这里是根据获取到的商品id和用户账号来判断这一商品之前是否已经存在于购物车表中,因此需要多表联查
<!--通过good_id和userAccount查询列表-->
<select id="findByGoodListIdAndUserAccount" resultMap="cartResult">
select c.*
from cart_list c
inner join good_list g on c.good_list_id = g.good_list_id
inner join login_user1 u on c.user_account = u.user_account
where c.good_list_id = #{goodListId} and c.user_account = #{userAccount}
</select>
<!--之前存在就更新数量-->
<update id="updateCartList" parameterType="com.software.PetShop.entity.CartList">
update cart_list set good_list_count = #{goodListCount} where good_list_id = #
{goodListId} and user_account = #{userAccount}
</update>
<!--购物车里没有添加过该商品,则添加该商品-->
<insert id="addNewCartList" parameterType="com.software.PetShop.entity.CartList" >
insert into
cart_list(cart_list_id,user_account,good_list_id,good_list_count,add_time)
values(#{cartListId},#{userAccount},#{goodListId},#{goodListCount},#{addTime})
</insert>
8.测试
这里我们手动设置一个新的数据测试数据执行添加购物车操作,可以看出数据库中成功插入了商品数据
再改变原有测试数据的商品数量,可以看出数据库表成功执行了更新操作