Mybatis常用动态 SQL 相关标签
1. <if>
用于条件判断,当满足条件时执行对应的 SQL 片段。
示例:
<select id="findUser" resultType="User">
SELECT * FROM users
WHERE 1=1
<if test="name != null and name != ''">
AND name = #{name}
</if>
<if test="age != null and age != ''">
AND age = #{age}
</if>
</select>
上述示例中,如果name不为null,并且不为空时,则拼接条件name进行查询。即if条件判断为true时,才进行name拼接查询
2. <where>
自动处理 WHERE
子句中的逻辑,避免手动拼接 AND
或 OR
导致的 SQL 错误。
- 如果
<where>
内部没有任何内容,则不会生成WHERE
。 - 自动移除第一个条件前多余的
AND
或OR
。
示例:
<select id="findUser" resultType="User">
SELECT * FROM users
<where>
<if test="name != null and name != ''">
AND name = #{name}
</if>
<if test="age != null and age != ''">
AND age = #{age}
</if>
</where>
</select>
3. <set>
用于更新操作时动态生成 SET
子句,自动处理逗号分隔符。
- 如果
<set>
内部没有任何内容,则不会生成SET
。 - 自动移除最后一个逗号。
示例:
<update id="updateUser">
UPDATE users
<set>
<if test="name != null and name != ''">
AND name = #{name}
</if>
<if test="age != null and age != ''">
AND age = #{age}
</if>
</set>
WHERE id = #{id}
</update>
4. <choose>、<when>、<otherwise>
类似于 Java 中的 switch
或 if-else
语句,用于多条件分支判断。
示例:
<select id="findUser" resultType="User">
SELECT * FROM users
<where>
<choose>
<when test="name != null and name != ''">
name = #{name}
</when>
<when test="email != null and email != ''">
email = #{email}
</when>
<otherwise>
age > 0
</otherwise>
</choose>
</where>
</select>
上述示例中:
如果name不为空,则根据name查询
如果name不为空且email不为空,则根据email查询
如果name和email都为空,则查询age大于0的
5. <trim>
用于自定义动态 SQL 的前缀和后缀,可以灵活控制生成的 SQL。
prefix
:在SQL前添加内容。suffix
:在生成 SQL语句后添加内容。prefixOverrides
:移除指定的前缀(如多余的AND
或OR
)。suffixOverrides
:移除指定的后缀。
示例:
<select id="findUser" resultType="User">
SELECT * FROM users
<trim prefix="WHERE" prefixOverrides="AND |OR">
<if test="name != null and name != ''">
AND name = #{name}
</if>
<if test="age != null and age != ''">
AND age = #{age}
</if>
</trim>
</select>
6. <foreach>
用于遍历集合,常用于 IN
查询或批量插入。
collection
:指定要遍历的集合名称。item
:集合中的每个元素的别名。open
:遍历开始的字符。close
:遍历结束的字符。separator
:集合项之间的分隔符。
示例:
<select id="findUsersByIds" resultType="User">
SELECT * FROM users
WHERE id IN
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</select>
批量插入示例:
<insert id="batchInsert">
INSERT INTO users (name, age)
VALUES
<foreach collection="list" item="user" separator=",">
(#{user.name}, #{user.age})
</foreach>
</insert>
7. <bind>
用于在 SQL 中创建变量,通常结合其他标签使用,比如正则表达式匹配。
示例:
<select id="findUserByLike" resultType="User">
<bind name="pattern" value="'%' + _parameter + '%'" />
SELECT * FROM users
WHERE name LIKE #{pattern}
</select>
8. <sql>
和 <include>
用于定义可重用的 SQL 片段。
<sql>
:定义 SQL 片段。<include>
:引用已定义的 SQL 片段。
示例:
<sql id="userColumns">
id, name, age, email
</sql>
<select id="findUser" resultType="User">
SELECT <include refid="userColumns" /> FROM users
WHERE id = #{id}
</select>
总结
标签 | 功能描述 |
---|---|
<if> | 条件判断 |
<where> | 自动生成 WHERE 子句 |
<set> | 自动生成 SET 子句 |
<choose> | 多条件分支判断 |
<trim> | 自定义前缀和后缀 |
<foreach> | 遍历集合,适用于 IN 查询或批量操作 |
<bind> | 创建变量 |
<sql> | 定义可重用的 SQL 片段 |
<include> | 引用已定义的 SQL 片段 |
通过这些标签,MyBatis 可以灵活地生成动态 SQL,满足复杂的业务需求。