<?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.sky.mapper.DishMapper"> <insert id="insert" useGeneratedKeys="true" keyProperty="id"> insert into dish (name, category_id, price, image, description, create_time, update_time, create_user, update_user, status) values (#{name}, #{categoryId}, #{price}, #{image}, #{description}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser}, #{status}) </insert> //分页查询 <select id="pageQuery" resultType="com.sky.vo.DishVO"> select d.* , c.name as categoryName from dish d left outer join category c on d.category_id = c.id <where> <if test="name != null"> and d.name like concat('%',#{name},'%') </if> <if test="categoryId != null"> and d.category_id = #{categoryId} </if> <if test="status != null"> and d.status = #{status} </if> </where> order by d.create_time desc </select> <update id="update"> update dish <set> <if test="name != null">name = #{name},</if> <if test="categoryId != null">category_id = #{categoryId},</if> <if test="price != null">price = #{price},</if> <if test="image != null">image = #{image},</if> <if test="description != null">description = #{description},</if> <if test="status != null">status = #{status},</if> <if test="updateTime != null">update_time = #{updateTime},</if> <if test="updateUser != null">update_user = #{updateUser},</if> </set> where id = #{id} </update> <select id="list" resultType="Dish" parameterType="Dish"> select * from dish <where> <if test="name != null">
时间: 2025-07-14 12:52:55 浏览: 18
<think>好的,我现在需要帮助用户展示MyBatis映射文件中关于Dish表的SQL操作示例,包括插入、更新、查询和分页的配置。首先,我需要回忆MyBatis的XML映射文件的基本结构,以及如何编写这些SQL语句。
用户提到了Dish表,所以我需要假设这个表的结构。通常,Dish表可能包含的字段如id(主键)、name(菜品名称)、price(价格)、category_id(分类ID)、create_time(创建时间)、update_time(更新时间)等。基于这些字段,我可以设计对应的SQL操作。
首先,插入操作。在MyBatis中,插入语句使用<insert>标签,并且通常需要指定parameterType,这里应该是Dish实体类的全限定名。使用#{}来引用实体类的属性。需要注意的是,如果数据库支持自动生成主键(比如自增ID),可以使用useGeneratedKeys和keyProperty来返回生成的主键值到实体对象中。
接下来是更新操作。更新语句使用<update>标签,同样需要parameterType。在设置字段时,使用<set>标签配合<if>标签可以实现动态更新,只更新非空的字段,避免全字段更新可能带来的问题。
然后是查询操作。基本的查询所有数据比较简单,但用户可能也需要根据条件查询,所以可以展示一个根据ID查询单个记录的示例,以及一个条件查询的例子,使用<where>和<if>标签来动态拼接条件。这样可以灵活应对不同的查询需求。
关于分页查询,用户提到了两种方式:一种是直接在SQL中使用LIMIT和OFFSET,这在XML中可以直接写,参数通过方法传入;另一种是使用PageHelper插件,通过拦截器实现分页,这样在XML中不需要写分页的SQL,而是由插件自动处理。需要引用用户提供的引用[2]和[3]中的内容来说明这两种方法。
另外,用户提供的引用[4]中提到XML中的特殊符号如大于小于号需要转义,比如使用>和<,这点在编写条件查询时需要注意,尤其是时间范围筛选的情况。例如,查询创建时间在某个区间内的记录,就需要用到>=和<=,这时候要正确转义。
在组织这些内容时,需要确保每个SQL操作都有对应的XML配置示例,并且注释清楚每个部分的作用。同时,要引用用户提供的参考资料来支持说明,比如分页的两种实现方式对应的引用[2]和[3],以及特殊符号处理的引用[4]。
可能还需要考虑的地方包括:参数的使用是否正确,比如#{}和${}的区别,防止SQL注入;结果映射是否正确,是否使用了resultType或者resultMap;以及动态SQL标签的正确使用,如<if>、<where>、<set>等,来增强SQL的灵活性。
最后,生成相关问题,需要围绕MyBatis的XML配置、动态SQL、分页实现、特殊符号处理等方面提问,帮助用户进一步深入了解相关内容。</think>### MyBatis DishMapper XML配置示例
以下展示针对`Dish`表的XML映射文件配置,包含插入、更新、查询及分页操作。
(假设`Dish`表字段:`id`(主键), `name`, `price`, `category_id`, `create_time`, `update_time`)
---
#### 1. 插入操作
```xml
<insert id="insertDish" parameterType="com.example.model.Dish" useGeneratedKeys="true" keyProperty="id">
INSERT INTO dish (name, price, category_id, create_time)
VALUES (#{name}, #{price}, #{categoryId}, #{createTime})
</insert>
```
- **说明**:`useGeneratedKeys`返回自增主键到实体对象[^1]。
---
#### 2. 更新操作
```xml
<update id="updateDish" parameterType="com.example.model.Dish">
UPDATE dish
<set>
<if test="name != null">name = #{name},</if>
<if test="price != null">price = #{price},</if>
<if test="categoryId != null">category_id = #{categoryId},</if>
update_time = NOW()
</set>
WHERE id = #{id}
</update>
```
- **说明**:`<set>`动态处理字段更新,避免全字段覆盖[^1]。
---
#### 3. 查询操作
**根据ID查询**
```xml
<select id="getDishById" resultType="com.example.model.Dish">
SELECT * FROM dish WHERE id = #{id}
</select>
```
**条件查询(动态SQL)**
```xml
<select id="getDishList" resultType="com.example.model.Dish">
SELECT * FROM dish
<where>
<if test="name != null">AND name LIKE CONCAT('%', #{name}, '%')</if>
<if test="categoryId != null">AND category_id = #{categoryId}</if>
<if test="minPrice != null">AND price >= #{minPrice}</if> <!-- 转义大于号 -->
<if test="maxPrice != null">AND price <= #{maxPrice}</if> <!-- 转义小于号 -->
</where>
</select>
```
- **说明**:`<where>`自动去除多余`AND`,`>`和`<`用于转义符号[^4]。
---
#### 4. 分页查询
**方式一:手动分页(LIMIT/OFFSET)**
```xml
<select id="getDishByPage" resultType="com.example.model.Dish">
SELECT * FROM dish LIMIT #{pageSize} OFFSET #{offset}
</select>
```
- **说明**:通过参数计算偏移量实现分页[^2]。
**方式二:PageHelper插件分页**
```xml
<se
阅读全文
相关推荐








