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.

111 lines
4.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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.GoodsMapper">
<!-- 定义商品表基础字段列表 -->
<sql id="Base_Column_List">
id,name,description,img,price,unit,count,type_id,business_id
</sql>
<!-- 查询所有商品(关联查询分类和商家信息) -->
<select id="selectAll" resultType="com.example.entity.Goods">
select goods.*,
type.name as typeName, <!-- 分类名称 -->
business.name as businessName <!-- 商家名称 -->
from goods
left join type on goods.type_id = type.id <!-- 关联分类表 -->
left join business on goods.business_id = business.id <!-- 关联商家表 -->
<where>
<if test="id != null"> and id= #{id}</if>
<if test="name != null"> and goods.name like concat('%', #{name}, '%')</if> <!-- 商品名称模糊搜索 -->
<if test="description != null"> and description= #{description}</if>
<if test="img != null"> and img= #{img}</if>
<if test="price != null"> and price= #{price}</if>
<if test="unit != null"> and unit= #{unit}</if>
<if test="count != null"> and count= #{count}</if> <!-- 库存数量 -->
<if test="typeId != null"> and type_id= #{typeId}</if> <!-- 分类ID -->
<if test="businessId != null"> and business_id= #{businessId}</if> <!-- 商家ID -->
</where>
order by id desc <!-- 按ID降序排列新的在前 -->
</select>
<!-- 根据ID查询单个商品关联查询分类和商家信息 -->
<select id="selectById" resultType="com.example.entity.Goods">
select goods.*,
business.name as businessName, <!-- 商家名称 -->
type.name as typeName <!-- 分类名称 -->
from goods
left join business on goods.business_id = business.id <!-- 关联商家表 -->
left join type on goods.type_id = type.id <!-- 关联分类表 -->
where goods.id = #{id}
</select>
<!-- 根据ID删除商品 -->
<delete id="deleteById">
delete from goods
where id = #{id}
</delete>
<!-- 新增商品 -->
<insert id="insert" parameterType="com.example.entity.Goods" useGeneratedKeys="true">
insert into goods
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="name != null">name,</if>
<if test="description != null">description,</if>
<if test="img != null">img,</if>
<if test="price != null">price,</if>
<if test="unit != null">unit,</if>
<if test="count != null">count,</if>
<if test="typeId != null">type_id,</if>
<if test="businessId != null">business_id,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="name != null">#{name},</if>
<if test="description != null">#{description},</if>
<if test="img != null">#{img},</if>
<if test="price != null">#{price},</if>
<if test="unit != null">#{unit},</if>
<if test="count != null">#{count},</if>
<if test="typeId != null">#{typeId},</if>
<if test="businessId != null">#{businessId},</if>
</trim>
</insert>
<!-- 根据ID修改商品信息 -->
<update id="updateById" parameterType="com.example.entity.Goods">
update goods
<set>
<if test="name != null">
name = #{name}, <!-- 商品名称 -->
</if>
<if test="description != null">
description = #{description}, <!-- 商品描述 -->
</if>
<if test="img != null">
img = #{img}, <!-- 商品图片 -->
</if>
<if test="price != null">
price = #{price}, <!-- 商品价格 -->
</if>
<if test="unit != null">
unit = #{unit}, <!-- 商品单位 -->
</if>
<if test="count != null">
count = #{count}, <!-- 库存数量 -->
</if>
<if test="typeId != null">
type_id = #{typeId}, <!-- 分类ID -->
</if>
<if test="businessId != null">
business_id = #{businessId}, <!-- 商家ID -->
</if>
</set>
where id = #{id}
</update>
</mapper>