如需转载,请根据 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 许可,附上本文作者及链接。
本文作者: 执笔成念
作者昵称: zbcn
本文链接: https://1363653611.github.io/zbcn.github.io/2019/10/10/myBatis_03%E5%8A%A8%E6%80%81sql/
动态sql
if
1 | <select id="findActiveBlogWithTitleLike" |
2 | resultType="Blog"> |
3 | SELECT * FROM BLOG |
4 | WHERE state = ‘ACTIVE’ |
5 | <if test="title != null"> |
6 | AND title like #{title} |
7 | </if> |
8 | </select> |
choose \ when \ otherwise
1 | <select id="findActiveBlogLike" |
2 | resultType="Blog"> |
3 | SELECT * FROM BLOG WHERE state = ‘ACTIVE’ |
4 | <choose> |
5 | <when test="title != null"> |
6 | AND title like #{title} |
7 | </when> |
8 | <when test="author != null and author.name != null"> |
9 | AND author_name like #{author.name} |
10 | </when> |
11 | <otherwise> |
12 | AND featured = 1 |
13 | </otherwise> |
14 | </choose> |
15 | </select> |
trim \ where \ set
- where
1 | <!--where --> |
2 | <select id="findActiveBlogLike" |
3 | resultType="Blog"> |
4 | SELECT * FROM BLOG |
5 | <where> |
6 | <if test="state != null"> |
7 | state = #{state} |
8 | </if> |
9 | <if test="title != null"> |
10 | AND title like #{title} |
11 | </if> |
12 | <if test="author != null and author.name != null"> |
13 | AND author_name like #{author.name} |
14 | </if> |
15 | </where> |
16 | </select> |
17 | <!--我们可以通过自定义 trim 元素来定制 where 元素的功能。比如,和 where 元素等价的自定义 trim 元素为: |
18 | prefixOverrides 属性会忽略通过管道分隔的文本序列(注意此例中的空格也是必要的)。它的作用是移除所有指定在 prefixOverrides 属性中的内容,并且插入 prefix 属性中指定的内容。 |
19 | --> |
20 | <trim prefix="WHERE" prefixOverrides="AND |OR "> |
21 | ... |
22 | </trim> |
- trim
- set
1<update id="updateAuthorIfNecessary">2update Author3<set>4<if test="username != null">username=#{username},</if>5<if test="password != null">password=#{password},</if>6<if test="email != null">email=#{email},</if>7<if test="bio != null">bio=#{bio}</if>8</set>9where id=#{id}10</update>11<!--对 set 元素等价的自定义 trim 元素的代码-->12<trim prefix="SET" suffixOverrides=",">13...14</trim>
foreach :
你可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象传递给 foreach 作为集合参数。当使用可迭代对象或者数组时,index 是当前迭代的次数,item 的值是本次迭代获取的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值
1 | <select id="selectPostIn" resultType="domain.blog.Post"> |
2 | SELECT * |
3 | FROM POST P |
4 | WHERE ID in |
5 | <foreach item="item" index="index" collection="list" |
6 | open="(" separator="," close=")"> |
7 | #{item} |
8 | </foreach> |
9 | </select> |
bind
1 | <select id="selectBlogsLike" resultType="Blog"> |
2 | <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" /> |
3 | SELECT * FROM BLOG |
4 | WHERE title LIKE #{pattern} |
5 | </select> |
多数据库支持
1 | <insert id="insert"> |
2 | <selectKey keyProperty="id" resultType="int" order="BEFORE"> |
3 | <if test="_databaseId == 'oracle'"> |
4 | select seq_users.nextval from dual |
5 | </if> |
6 | <if test="_databaseId == 'db2'"> |
7 | select nextval for seq_users from sysibm.sysdummy1" |
8 | </if> |
9 | </selectKey> |
10 | insert into users values (#{id}, #{name}) |
11 | </insert> |
带注解的映射器接口类中使用动态 SQL
script
1 | ({"<script>", |
2 | "update Author", |
3 | " <set>", |
4 | " <if test='username != null'>username=#{username},</if>", |
5 | " <if test='password != null'>password=#{password},</if>", |
6 | " <if test='email != null'>email=#{email},</if>", |
7 | " <if test='bio != null'>bio=#{bio}</if>", |
8 | " </set>", |
9 | "where id=#{id}", |
10 | "</script>"}) |
11 | void updateAuthorValues(Author author); |