推薦閱讀:
什么是動態SQL? 動態SQL有什么作用?
傳統的使用JDBC的方法,相信大家在組合復雜的的SQL語句的時候,需要去拼接,稍不注意哪怕少了個空格,都會導致錯誤。Mybatis的動態SQL功能正是為了解決這種問題, 其通過 if, choose, when, otherwise, trim, where, set, foreach標簽,可組合成非常靈活的SQL語句,從而提高開發人員的效率。
下面就去感受Mybatis動態SQL的魅力吧:
1. if: 你們能判斷,我也能判斷!
作為程序猿,誰不懂 if ! 在mybatis中也能用 if 啦:
1
2
3
4
5
6
7
|
<select id= "findUserById" resultType= "user" > select * from user where < if test= "id != null" > id=#{id} </ if > and deleteFlag= 0 ; </select> |
上面例子: 如果傳入的id 不為空, 那么才會SQL才拼接id = #{id}。 這個相信大家看一樣就能明白,不多說。
細心的人會發現一個問題:“你這不對啊! 要是你傳入的id為null, 那么你這最終的SQL語句不就成了 select * from user where and deleteFlag=0, 這語句有問題!”
是啊,這時候,mybatis的 where 標簽就該隆重登場啦:
2. where, 有了我,SQL語句拼接條件神馬的都是浮云!
咱們通過where改造一下上面的例子:
1
2
3
4
5
6
7
8
9
|
<select id= "findUserById" resultType= "user" > select * from user where <where> < if test= "id != null" > id=#{id} </ if > and deleteFlag= 0 ; </where> </select> |
有些人就要問了: “你這都是些什么玩意兒! 跟上面的相比, 不就是多了個where標簽嘛! 那這個還會不會出現 select * from user where and deleteFlag=0 ?”
的確,從表面上來看,就是多了個where標簽而已, 不過實質上, mybatis是對它做了處理,當它遇到AND或者OR這些,它知道怎么處理。其實我們可以通過 trim 標簽去自定義這種處理規則。
3. trim : 我的地盤,我做主!
上面的where標簽,其實用trim 可以表示如下:
1
2
3
|
<trim prefix= "WHERE" prefixOverrides= "AND |OR " > ... </trim> |
它的意思就是: 當WHERE后緊隨AND或則OR的時候,就去除AND或者OR。 除了WHERE以外, 其實還有一個比較經典的實現,那就是SET。
4. set: 信我,不出錯!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<update id= "updateUser" parameterType= "com.dy.entity.User" > update user set < if test= "name != null" > name = #{name}, </ if > < if test= "password != null" > password = #{password}, </ if > < if test= "age != null" > age = #{age} </ if > <where> < if test= "id != null" > id = #{id} </ if > and deleteFlag = 0 ; </where> </update> |
問題又來了: “如果我只有name不為null, 那么這SQL不就成了 update set name = #{name}, where ........ ? 你那name后面那逗號會導致出錯啊!”
是的,這時候,就可以用mybatis為我們提供的set 標簽了。下面是通過set標簽改造后:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<update id= "updateUser" parameterType= "com.dy.entity.User" > update user set <set> < if test= "name != null" >name = #{name},</ if > < if test= "password != null" >password = #{password},</ if > < if test= "age != null" >age = #{age},</ if > </set> <where> < if test= "id != null" > id = #{id} </ if > and deleteFlag = 0 ; </where> </update> |
這個用trim 可表示為:
1
2
3
|
<trim prefix= "SET" suffixOverrides= "," > ... </trim> |
WHERE是使用的 prefixOverrides(前綴), SET是使用的 suffixOverrides (后綴), 看明白了吧!
5. foreach: 你有for, 我有foreach, 不要以為就你才屌!
java中有for, 可通過for循環, 同樣, mybatis中有foreach, 可通過它實現循環,循環的對象當然主要是java容器和數組。
1
2
3
4
5
6
7
8
9
|
<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> |
將一個 List 實例或者數組作為參數對象傳給 MyBatis,當這么做的時候,MyBatis 會自動將它包裝在一個 Map 中并以名稱為鍵。List 實例將會以“list”作為鍵,而數組實例的鍵將是“array”。同樣, 當循環的對象為map的時候,index其實就是map的key。
6. choose: 我選擇了你,你選擇了我!
Java中有switch, mybatis有choose。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<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> |
以上例子中: 當title和author都不為null的時候, 那么選擇二選一(前者優先), 如果都為null, 那么就選擇 otherwise中的, 如果tilte和author只有一個不為null, 那么就選擇不為null的那個。
以上所述是小編給大家介紹的Mybatis入門教程(四)之mybatis動態sql,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!