- A+
MyBatis提供了9種動態SQL標簽:trim、where、set、foreach、if、choose、when、otherwise、bind;其執行原理為,使用OGNL從SQL參數對象中計算表達式的值,根據表達式的值動態拼接SQL,以此來完成動態SQL的功能。
動態標簽用法
1.if
If : 當參數滿足條件才會執行某個條件
1
2
3
4
5
6
|
< select id= "findName" resultType= "String" > ????????? SELECT stu. name FROM tab_stu stu WHERE age = 20 ????????? <if test= "name != null" > ???????????????? AND name like #{ name } ???????? </if> </ select > |
2.choose、when、otherwise
choose、when、otherwise : choose標簽是按順序判斷其內部when標簽中的test條件是否成立,如果有一個成立,則choose結束;如果所有的when條件都不滿足時,則執行otherwise中的SQL。類似于java的switch語句。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< select id= "findName" resultType= "String" > ????????? SELECT stu. name FROM tab_stu stu WHERE age = #{age} ???? <choose> ????????????? < when test= "name != null" > ???????????????????? AND name like #{ name } ???????????? </ when > ???????????? < when test= "class != null" > ???????????????????? AND class like #{class} ???????????? </ when > ???????????? <otherwise> ???????????????????? AND class = 1 ???????????? </otherwise> ????? </choose> </ select > |
3.where
1
2
3
4
5
6
7
8
9
10
11
12
|
< select id= "findName" resultType= "String" > ????????? SELECT stu. name FROM tab_stu stu WHERE ???????? <if test= "age != null" > ???????????? age = #{age} ???????? </if> ???????? <if test= "name!= null" > ???????????? AND name = #{ name } ???????? </if> ???????? <if test= "class!= null" > ???????????? AND class = #{class} ???????? </if> </ select > |
當第一個if不滿或第一第二第三個if都不滿足,會出現以下情況
1
2
|
SELECT stu. name FROM tab_stu stu WHERE AND name = "小米" AND class ="1班”; SELECT stu. name FROM tab_stu stu WHERE ; |
這會導致查詢失敗。使用where標簽可以解決這個問題
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< select id= "findName" resultType= "String" > ????????? SELECT stu. name FROM tab_stu stu ???????? < where > ???????????? <if test= "age != null" > ???????????????? age = #{age} ???????????? </if> ????????? <if test= "name!= null" > ???????????????? AND name = #{ name } ???????? </if> ???????? <if test= "class!= null" > ???????????????? AND class = #{class} ???????? </if> ???? </ where > </ select > |
where標簽會在只有一個以上的if條件滿足的情況下才去插入WHERE關鍵字,而且,若最后的內容是”AND”或”OR”開頭的,where也會根據語法絕對是否需要保留。
4.set
set標簽用于解決動態更新語句存在的符號問題
1
2
3
4
5
6
7
8
9
|
< update id= "updateStu" > ???????? Update tab_stu ???????? < set > ???????????????? <if test= "name != null" > name =#{ name },</if> ???????????????? <if test= "age != null" > age=#{age},</if> ???????????????? <if test= "class != null" > class=#{class},</if> ???????????????? <if test= "subject != null" > subject=#{subject}</if> ???????? </ set > </ update > |
set標簽會動態前置SET關鍵字,同時也會消除無關的逗號,因為用了條件語句后,可能就會在生成的賦值語句的后面留下逗號。
5.trim
trim:trim標簽可實現where/set標簽的功能
Trim標簽有4個屬性,分別為prefix、suffix、prefixOverrides、suffixOverrides
prefix:表示在trim標簽包裹的SQL前添加指定內容
suffix:表示在trim標簽包裹的SQL末尾添加指定內容
prefixOverrides:表示去掉(覆蓋)trim標簽包裹的SQL指定首部內容,去掉多個內容寫法為and |or(中間空格不能省略)(一般用于if判斷時去掉多余的AND |OR)
suffixOverrides:表示去掉(覆蓋)trim標簽包裹的SQL指定尾部內容(一般用于update語句if判斷時去掉多余的逗號)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< select id= "findName" resultType= "String" > ????????? SELECT stu. name FROM tab_stu stu ???????? <trim prefix= "where" prefixOverrides= "and |or" > ???????????????? <if test= "age != null" > ???????????????????????? age = #{age} ???????????????? </if> ???????????????? <if test= "name!= null" > ???????????????????????? AND name = #{ name } ???????????????? </if> ???????????????? <if test= "class!= null" > ???????????????????????? OR class = #{class} ???????????????? </if> ???????? </trim> </ select > |
1
2
3
4
5
6
7
8
9
|
< update id=”updateStu”> ???????????? Update tab_stu ???????????? <trim prefix= "set" subfix= "where id=#{id}" suffixOverrides= "," > ???????????????? <if test= "name != null" > name =#{ name },</if> ???????????????? <if test= "age != null" > age=#{age},</if> ???????????????? <if test= "class != null" > class=#{class},</if> ???????????????? <if test= "subject != null" > subject=#{subject}</if> ???????????? </trim> </ update > |
6.foreach
foreach:對集合進行遍歷
1
2
3
4
5
6
|
< select id= "findName" resultType= "String" > ???????? SELECT stu. name FROM tab_stu stu where id in ???????? <foreach item=”item” index =” index ” collection=”listName” open =”(” separator=”,” close =”)”> ???????????????? #{item} ???????? </foreach> </ select > |
下面是foreach標簽的各個屬性:
collection:迭代集合的名稱,可以使用@Param注解指定,該參數為必選(java入參,相對于#{listName})
item:表示本次迭代獲取的元素,若collection為List、Set或數組,則表示其中元素;若collection為Map,則代表key-value的value,該參數為必選
index:在List、Set和數組中,index表示當前迭代的位置,在Map中,index指元素的key,該參數是可選項
open:表示該語句以什么開始,最常使用的是左括弧”(”,MyBatis會將該字符拼接到foreach標簽包裹的SQL語句之前,并且只拼接一次,該參數是可選項
close:表示該語句以什么結束,最常使用的是右括弧”)”,MyBatis會將該字符拼接到foreach標簽包裹的SQL語句末尾,該參數是可選項
separator:MyBatis會在每次迭代后給SQL語句添加上separator屬性指定的字符,該參數是可選項
7.bind
bind:bind標簽可以從OGNL(對象圖導航語言)表達式中創建一個變量并將其綁定到上下文
Mybatis中使用Mysql的模糊查詢字符串拼接(like) 中也涉及到bind的使用
1
2
3
4
5
6
7
8
9
|
< select id= "findName" resultType= "String" > ????????? SELECT stu. name FROM tab_stu stu ???????? < where > ???????????????? <if test= "name!= null" > ???????????????????????? <bind name = "stuName" value= "'%'+stuName+'%'" > ???????????????????????? name like #{stuName} ???????????????? </if> ???????? </ where > </ select > |
到此這篇關于MyBatis的9種動態標簽詳解的文章就介紹到這了