分析一下代码的意思<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.AdminMapper"> <select id="selectAll" resultType="com.example.entity.Admin"> select * from `admin` <where> <if test="name != null"> and name like concat('%', #{name}, '%')</if> </where> order by id desc </select> <delete id="deleteById"> delete from `admin` where id = #{id} </delete> <insert id="insert" parameterType="com.example.entity.Admin" useGeneratedKeys="true"> insert into `admin` <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null">id,</if> <if test="username != null">username,</if> <if test="password != null">password,</if> <if test="name != null">name,</if> <if test="phone != null">phone,</if> <if test="email != null">email
时间: 2025-06-19 18:00:36 浏览: 7
### MyBatis Mapper XML 中 SQL 映射代码的含义
#### 1. `select` 标签的作用
`<select>` 标签主要用于定义查询操作。它通过指定的 ID 和参数映射到对应的 SQL 查询语句,并返回结果数据。其核心属性包括:
- **id**: 唯一标识符,供接口方法调用。
- **parameterType**: 输入参数类型。
- **resultType** 或 **resultMap**: 定义返回的结果类型。
示例:
```xml
<select id="getEmpByIdNames" parameterType="map" resultType="com.mybatis.employee.Employee">
SELECT * FROM table_employee WHERE id = #{param1} AND names = #{param2}
</select>
```
上述代码表示基于两个参数 (`id`, `names`) 进行员工表的查询操作[^3]。
---
#### 2. `insert` 标签的作用
`<insert>` 标签用于执行插入操作。它可以支持简单的插入逻辑,也可以配合动态 SQL 实现复杂场景下的插入需求。如果数据库支持 RETURNING 子句,则可以通过 `<select>` 来实现插入并获取返回值的操作[^2]。
示例:
```xml
<insert id="insertEmployee" parameterType="com.mybatis.employee.Employee">
INSERT INTO employees (name, age, department_id) VALUES (#{name}, #{age}, #{departmentId})
</insert>
```
对于某些特殊数据库(如 PostgreSQL),可以利用如下方式完成插入后立即返回数据:
```xml
<select id="insertAndGetAuthor" resultType="domain.blog.Author" affectData="true" flushCache="true">
INSERT INTO Author (username, password, email, bio)
VALUES (#{username}, #{password}, #{email}, #{bio})
RETURNING id, username, password, email, bio
</select>
```
---
#### 3. `update` 标签的作用
`<update>` 标签负责更新数据库记录。通常会涉及多个字段的修改,因此常与动态 SQL 结合使用以提高灵活性。
示例:
```xml
<update id="updateEmployee" parameterType="com.mybatis.employee.Employee">
UPDATE employees SET name=#{name}, age=#{age} WHERE id=#{id}
</update>
```
当需要有条件地设置字段时,可借助 `trim` 标签优化 SQL 语法[^1]:
```xml
<update id="dynamicUpdateEmployee" parameterType="java.util.Map">
UPDATE employees
<set>
<if test="name != null">name=#{name},</if>
<if test="age != null">age=#{age},</if>
</set>
WHERE id=#{id}
</update>
```
---
#### 4. `delete` 标签的作用
`<delete>` 标签用于删除特定条件下的记录。它的功能相对简单,但也可能结合动态 SQL 提高适应性。
示例:
```xml
<delete id="deleteEmployeeById" parameterType="int">
DELETE FROM employees WHERE id=#{id}
</delete>
```
---
#### 动态 SQL 的用法
MyBatis 支持多种动态 SQL 标签,常见的有以下几个:
- **`if`**: 判断条件是否成立。
- **`choose/when/otherwise`**: 类似于 Java 的 switch-case 语句。
- **`foreach`**: 遍历集合或数组。
- **`trim`**: 处理多余的关键字(如 `AND`、`,` 等)。
##### 示例:`trim` 使用案例
假设有一个复杂的更新语句,仅需更新部分字段而非全部字段,此时可通过 `trim` 标签简化 SQL 构造过程:
```xml
<update id="updateUserDynamic" parameterType="hashmap">
UPDATE users
<trim prefix="SET" suffixOverrides=",">
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
</trim>
WHERE id=#{id}
</update>
```
此代码片段中,`suffixOverrides=","` 表明移除最后多余的逗号;而 `prefix="SET"` 添加了必要的前缀[^1]。
---
#### 总结
MyBatis 的 Mapper XML 文件提供了强大的 SQL 映射能力,能够灵活应对各种 CRUD 场景。无论是基础的增删改查还是高级的动态 SQL 应用,都能满足实际开发中的多样化需求。
---
问题
阅读全文
相关推荐











