- A+
MyBatis提供了9種動(dòng)態(tài)SQL標(biāo)簽:trim、where、set、foreach、if、choose、when、otherwise、bind;其執(zhí)行原理為,使用OGNL從SQL參數(shù)對(duì)象中計(jì)算表達(dá)式的值,根據(jù)表達(dá)式的值動(dòng)態(tài)拼接SQL,以此來完成動(dòng)態(tài)SQL的功能。
動(dòng)態(tài)標(biāo)簽用法
1.if
If : 當(dāng)參數(shù)滿足條件才會(huì)執(zhí)行某個(gè)條件
|
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標(biāo)簽是按順序判斷其內(nèi)部when標(biāo)簽中的test條件是否成立,如果有一個(gè)成立,則choose結(jié)束;如果所有的when條件都不滿足時(shí),則執(zhí)行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> |
當(dāng)?shù)谝粋€(gè)if不滿或第一第二第三個(gè)if都不滿足,會(huì)出現(xiàn)以下情況
|
1
2
|
SELECT stu.name FROM tab_stu stu WHERE AND name = "小米" AND class ="1班”;SELECT stu.name FROM tab_stu stu WHERE; |
這會(huì)導(dǎo)致查詢失敗。使用where標(biāo)簽可以解決這個(gè)問題
|
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標(biāo)簽會(huì)在只有一個(gè)以上的if條件滿足的情況下才去插入WHERE關(guān)鍵字,而且,若最后的內(nèi)容是”AND”或”O(jiān)R”開頭的,where也會(huì)根據(jù)語法絕對(duì)是否需要保留。
4.set
set標(biāo)簽用于解決動(dòng)態(tài)更新語句存在的符號(hào)問題
|
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標(biāo)簽會(huì)動(dòng)態(tài)前置SET關(guān)鍵字,同時(shí)也會(huì)消除無關(guān)的逗號(hào),因?yàn)橛昧藯l件語句后,可能就會(huì)在生成的賦值語句的后面留下逗號(hào)。
5.trim
trim:trim標(biāo)簽可實(shí)現(xiàn)where/set標(biāo)簽的功能
Trim標(biāo)簽有4個(gè)屬性,分別為prefix、suffix、prefixOverrides、suffixOverrides
prefix:表示在trim標(biāo)簽包裹的SQL前添加指定內(nèi)容
suffix:表示在trim標(biāo)簽包裹的SQL末尾添加指定內(nèi)容
prefixOverrides:表示去掉(覆蓋)trim標(biāo)簽包裹的SQL指定首部內(nèi)容,去掉多個(gè)內(nèi)容寫法為and |or(中間空格不能省略)(一般用于if判斷時(shí)去掉多余的AND |OR)
suffixOverrides:表示去掉(覆蓋)trim標(biāo)簽包裹的SQL指定尾部內(nèi)容(一般用于update語句if判斷時(shí)去掉多余的逗號(hào))
|
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:對(duì)集合進(jìn)行遍歷
|
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標(biāo)簽的各個(gè)屬性:
collection:迭代集合的名稱,可以使用@Param注解指定,該參數(shù)為必選(java入?yún)?,相?duì)于#{listName})
item:表示本次迭代獲取的元素,若collection為List、Set或數(shù)組,則表示其中元素;若collection為Map,則代表key-value的value,該參數(shù)為必選
index:在List、Set和數(shù)組中,index表示當(dāng)前迭代的位置,在Map中,index指元素的key,該參數(shù)是可選項(xiàng)
open:表示該語句以什么開始,最常使用的是左括弧”(”,MyBatis會(huì)將該字符拼接到foreach標(biāo)簽包裹的SQL語句之前,并且只拼接一次,該參數(shù)是可選項(xiàng)
close:表示該語句以什么結(jié)束,最常使用的是右括弧”)”,MyBatis會(huì)將該字符拼接到foreach標(biāo)簽包裹的SQL語句末尾,該參數(shù)是可選項(xiàng)
separator:MyBatis會(huì)在每次迭代后給SQL語句添加上separator屬性指定的字符,該參數(shù)是可選項(xiàng)
7.bind
bind:bind標(biāo)簽可以從OGNL(對(duì)象圖導(dǎo)航語言)表達(dá)式中創(chuàng)建一個(gè)變量并將其綁定到上下文
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> |
到此這篇關(guān)于MyBatis的9種動(dòng)態(tài)標(biāo)簽詳解的文章就介紹到這了

