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.

94 lines
3.7 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.CommentMapper">
<!-- 定义评论表基础字段列表 -->
<sql id="Base_Column_List">
id,user_id,business_id,goods_id,time,content
</sql>
<!-- 查询所有评论(关联查询商家、商品和用户信息) -->
<select id="selectAll" resultType="com.example.entity.Comment">
select comment.*,
business.name as businessName, <!-- 商家名称 -->
goods.img as goodsImg, <!-- 商品图片 -->
goods.name as goodsName, <!-- 商品名称 -->
goods.price as goodsPrice, <!-- 商品价格 -->
goods.unit as goodUnit, <!-- 商品单位 -->
user.name as userName <!-- 用户昵称 -->
from comment
left join business on comment.business_id = business.id <!-- 关联商家表 -->
left join goods on comment.goods_id = goods.id <!-- 关联商品表 -->
left join user on comment.user_id = user.id <!-- 关联用户表 -->
<where>
<if test="id != null"> and id= #{id}</if>
<if test="userId != null"> and user_id = #{userId}</if>
<if test="businessId != null"> and comment.business_id = #{businessId}</if>
<if test="goodsId != null"> and goods_id = #{goodsId}</if>
<if test="time != null"> and time = #{time}</if>
<if test="content != null"> and content like concat('%', #{content}, '%') </if> <!-- 评论内容模糊搜索 -->
</where>
</select>
<!-- 根据ID查询单个评论 -->
<select id="selectById" resultType="com.example.entity.Comment">
select
<include refid="Base_Column_List" />
from comment
where id = #{id}
</select>
<!-- 根据ID删除评论 -->
<delete id="deleteById">
delete from comment
where id = #{id}
</delete>
<!-- 新增评论 -->
<insert id="insert" parameterType="com.example.entity.Comment" useGeneratedKeys="true">
insert into comment
<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="time != null">time,</if>
<if test="content != null">content,</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="time != null">#{time},</if>
<if test="content != null">#{content},</if>
</trim>
</insert>
<!-- 根据ID修改评论信息 -->
<update id="updateById" parameterType="com.example.entity.Comment">
update comment
<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="time != null">
time = #{time},
</if>
<if test="content != null">
content = #{content}, <!-- 评论内容 -->
</if>
</set>
where id = #{id}
</update>
</mapper>