if

一般配合 where 或者 set 使用

定义

1
2
3
<if test="逻辑表达式">
test 为true时拼接的语句
</if>

例子01:如果title列不为空则拼接 AND title like #{title}语句

1
2
3
4
5
6
<select id="findActiveBlogWithTitleLike" resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
</select>

例子02:作者不为空 且 作者名不为空时拼接 AND author_name like #{author.name}语句

使用了 逻辑运算符 and

1
2
3
4
5
6
<select id="findActiveBlogLike" resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</select>

choose,when,otherwise

定义:从上到下,如果when的test为true 就执行拼接然后跳过,如果都为false,那么拼接语句3

1
2
3
4
5
6
7
8
9
10
11
<choose>
<when test="条件1">
语句1
</when>
<when test="条件2">
语句2
</when>
<otherwise>
语句3
</otherwise>
</choose>

例子:传入了 “title” 就按 “title” 查找,传入了 “author” 就按 “author” 查找,若两者都没有传入,就返回标记为 featured 的 BLOG

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<select id="findActiveBlogLike" resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>

where

特征:

  • where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。
  • 若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。

定义:

1
2
3
4
5
6
7
8
9
10
11
<where>
<if test="state != null">
state = #{state}
</if>
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</where>

set

特征:

  • 用于动态更新语句,即update语句
  • set 元素可以用于动态包含需要更新的列,忽略其它不更新的列
  • set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号

例子:更新作者信息,如果传入参数不为空则更新

1
2
3
4
5
6
7
8
9
10
<update id="updateAuthorIfNecessary">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
</update>

trim

特征:

  • 去除sql语句中多余的and关键字,逗号,
  • 给sql语句前拼接 “where“、“set“以及“values(“ 等前缀,
  • 添加“)“等后缀
  • 可用于选择性插入、更新、删除或者条件查询等操作。

属性:

1 2
prefix 给sql语句拼接的前缀
prefix 给sql语句拼接的后缀
prefixOverrides 去除sql语句前面的关键字或者字符,该关键字或者字符由prefixOverrides属性指定,假设该属性指定为”AND”,当sql语句的开头为”AND”,trim标签将会去除该”AND”
suffixOverrides 去除sql语句后面的关键字或者字符,该关键字或者字符由suffixOverrides属性指定

例子:模仿了 where 中的例子,相当于模拟了where标签的效果

1
2
3
4
5
6
7
8
9
10
11
<trim prefix="WHERE" prefixOverrides="AND">
<if test="state != null">
state = #{state}
</if>
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</trim>

foreach

特征:

  • 它允许你指定一个集合,声明可以在元素体内使用的 集合项(item)和 索引(index)变量。
  • 它也允许你指定 开头结尾 的字符串 以及集合项迭代之间的 分隔符。这个元素也不会错误地添加多余的分隔符

属性:

  • collection: 需做foreach(遍历)的对象,作为入参时 list、array对象时
    • collection 属性值 分别默认用 “list”、”array” 代替,Map对象没有默认的属性值(需用注解指定)。
    • 但是,在作为入参时可以使用 @Param(“keyName”) 注解 来设置自定义collection属性值,设置keyName后,list、array会失效;
  • item: 集合元素迭代时的别名称,该参数为必选项;
  • index: 在list、array中,index为元素的序号索引。但是在Map中,index为遍历元素的key值,该参数为可选项;
  • open: 遍历集合时的开始符号,通常与close=”)”搭配使用。使用场景IN(),values()时,该参数为可选项;
  • separator: 元素之间的分隔符,类比在IN()的时候,separator=”,”,最终所有遍历的元素将会以设定的(,)逗号符号隔开,该参数为可选项;
  • close: 遍历集合时的结束符号,通常与open=”(“搭配使用,该参数为可选项;

例子:效果为 select * from post p where ID in ( item1 , item2 , item3 )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
<where>
<foreach
item="item"
index="index"
collection="list"
open="ID in ("
separator=","
close=")"
nullable="true"
>
#{item}
</foreach>
</where>
</select>

详解:https://blog.csdn.net/m0_37965811/article/details/117635299

多数据库支持

如果配置了 databaseIdProvider,你就可以在动态代码中使用名为 “_databaseId” 的变量来为不同的数据库构建特定的语句。比如下面的例子:

1
2
3
4
5
6
7
8
9
10
11
<insert id="insert">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
<if test="_databaseId == 'oracle'">
select seq_users.nextval from dual
</if>
<if test="_databaseId == 'db2'">
select nextval for seq_users from sysibm.sysdummy1"
</if>
</selectKey>
insert into users values (#{id}, #{name})
</insert>