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


評論