動態 SQL 是 MyBatis 的強大特性之一。 使用動態 SQL 並非一件易事,MyBatis 顯著地提升了這一特性的易用性。
1 if
使用動態 SQL 最常見情景是根據條件包含 where 子句的一部分。比如:
<select id="findActiveBlogWithTitleLike"
resultType="Blog">
SELECT * FROM BLOG
WHERE state = 'ACTIVE'
<if test="title != null">
AND title like #{title}
</if>
</select>
如果希望通過 “title” 和 “author” 兩個參數進行可選搜索該怎麼辦呢? 只需要加入另一個條件即可。
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = 'ACTIVE'
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</select>
2 choose (when, otherwise)
有時候,我們不想使用所有的條件,而只是想從多個條件中選擇一個使用。針對這種情況,MyBatis 提供了 choose 元素,它有點像 Java 中的 switch 語句。
<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>
3 trim (where, set)
現在回到之前的 “if” 示例,這次我們將 “state = ‘ACTIVE’” 設置成動態條件,看看會發生什麼。
<select id="findActiveBlogLike"
resultType="Blog">
select B.id, B.title, A.authorid, A.username
from Blog B left outer join Author A on B.author_id = A.authorid
WHERE
<if test="state != null">
B.state = #{state}
</if>
<if test="title != null">
AND B.title like #{title}
</if>
<if test="author != null and author.username != null">
AND A.username like #{author.username }
</if>
</select>
如果沒有匹配的條件會怎麼樣?最終這條 SQL 會變成這樣:
SELECT * FROM BLOG WHERE
這會導致查詢失敗。 如果匹配的只是第二個條件又會怎樣?這條 SQL 會是這樣:
SELECT * FROM BLOG WHERE AND title like ‘someTitle’
這個查詢也會失敗。
MyBatis 有一個簡單且適合大多數場景的解決辦法。 而這,只需要一處簡單的改動:
<select id="findActiveBlogLike"
resultType="Blog">
select B.id, B.title, A.authorid, A.username
from Blog B left outer join Author A on B.author_id = A.authorid
<where>
<if test="state != null">
B.state = #{state}
</if>
<if test="title != null">
AND B.title like #{title}
</if>
<if test="author != null and author.username != null">
AND A.username like #{author.username }
</if>
</where>
</select>
如果 where 元素與你期望的不太一樣,你也可以通過自定義 trim 元素來定製 where 元素的功能。比如,和 where 元素等價的自定義 trim 元素爲:
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
用於動態更新語句的類似解決方案叫做 set。 set 元素可以用於動態包含需要更新的列,忽略其它不更新的列。比如:
<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>
來看看與 set 元素等價的自定義 trim 元素吧:
<trim prefix="SET" suffixOverrides=",">
...
</trim>
屬性 |
描述 |
prefix |
在trim標籤內SQL語句加上前綴 |
suffix |
在trim標籤內SQL語句加上後綴 |
prefixOverrides |
指定去除多餘的前綴內容 |
suffixOverrides |
指定去除多餘的後綴內容 |
4 foreach
動態 SQL 的另一個常見使用場景是對集合進行遍歷。比如:
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>