You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.2 KiB

<?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.example.mapper.CartMapper">
<!-- 定义购物车表基础字段列表 -->
<sql id="Base_Column_List">
id,user_id,business_id,goods_id,num
</sql>
<!-- 查询所有购物车商品(关联查询商家和商品信息) -->
<select id="selectAll" resultType="com.example.entity.Cart">
select cart.*,
business.name as businessName, <!-- 商家名称 -->
goods.img as goodsImg, <!-- 商品图片 -->
goods.name as goodsName, <!-- 商品名称 -->
goods.price as goodsPrice, <!-- 商品价格 -->
goods.unit as goodUnit <!-- 商品单位 -->
from cart
left join business on cart.business_id = business.id <!-- 关联商家表 -->
left join goods on cart.goods_id = goods.id <!-- 关联商品表 -->
<where>
<if test="id != null"> and id= #{id}</if>
<if test="userId != null"> and user_id = #{userId}</if>
<if test="businessId != null"> and business_id = #{businessId}</if>
<if test="goodsId != null"> and goods_id = #{goodsId}</if>
<if test="num != null"> and num = #{num}</if>
</where>
</select>
<!-- 根据ID查询单个购物车商品 -->
<select id="selectById" resultType="com.example.entity.Cart">
select
<include refid="Base_Column_List" />
from cart
where id = #{id}
</select>
<!-- 根据ID删除购物车商品 -->
<delete id="deleteById">
delete from cart
where id = #{id}
</delete>
<!-- 新增购物车商品 -->
<insert id="insert" parameterType="com.example.entity.Cart" useGeneratedKeys="true">
insert into cart
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="userId != null">user_id,</if>
<if test="businessId != null">business_id,</if>
<if test="goodsId != null">goods_id,</if>
<if test="num != null">num,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="userId != null">#{userId},</if>
<if test="businessId != null">#{businessId},</if>
<if test="goodsId != null">#{goodsId},</if>
<if test="num != null">#{num},</if>
</trim>
</insert>
<!-- 根据ID修改购物车商品信息 -->
<update id="updateById" parameterType="com.example.entity.Cart">
update cart
<set>
<if test="userId != null">
user_id = #{userId},
</if>
<if test="businessId != null">
business_id = #{businessId},
</if>
<if test="goodsId != null">
goods_id = #{goodsId},
</if>
<if test="num != null">
num = #{num}, <!-- 商品数量 -->
</if>
</set>
where id = #{id}
</update>
</mapper>