MyBatis的9種動態(tài)標簽詳解
前言
MyBatis提供了9種動態(tài)SQL標簽:trim、where、set、foreach、if、choose、when、otherwise、bind;
其執(zhí)行原理為,使用OGNL從SQL參數(shù)對象中計算表達式的值,根據(jù)表達式的值動態(tài)拼接SQL,以此來完成動態(tài)SQL的功能。
動態(tài)標簽用法
1.if
If : 當參數(shù)滿足條件才會執(zhí)行某個條件
<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條件都不滿足時,則執(zhí)行otherwise中的SQL。類似于java的switch語句。
<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
<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>
當?shù)谝粋€if不滿或第一第二第三個if都不滿足,會出現(xiàn)以下情況
SELECT stu.name FROM tab_stu stu WHERE AND name = "小米" AND class ="1班”; SELECT stu.name FROM tab_stu stu WHERE;
這會導致查詢失敗。使用where標簽可以解決這個問題
<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”或”O(jiān)R”開頭的,where也會根據(jù)語法絕對是否需要保留。
4.set
set標簽用于解決動態(tài)更新語句存在的符號問題
<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標簽會動態(tài)前置SET關鍵字,同時也會消除無關的逗號,因為用了條件語句后,可能就會在生成的賦值語句的后面留下逗號。
5.trim
trim:trim標簽可實現(xiàn)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判斷時去掉多余的逗號)
<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>
<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:對集合進行遍歷
<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注解指定,該參數(shù)為必選(java入?yún)?,相對?{listName})
item:表示本次迭代獲取的元素,若collection為List、Set或數(shù)組,則表示其中元素;若collection為Map,則代表key-value的value,該參數(shù)為必選
index:在List、Set和數(shù)組中,index表示當前迭代的位置,在Map中,index指元素的key,該參數(shù)是可選項
open:表示該語句以什么開始,最常使用的是左括弧”(”,MyBatis會將該字符拼接到foreach標簽包裹的SQL語句之前,并且只拼接一次,該參數(shù)是可選項
close:表示該語句以什么結束,最常使用的是右括弧”)”,MyBatis會將該字符拼接到foreach標簽包裹的SQL語句末尾,該參數(shù)是可選項
separator:MyBatis會在每次迭代后給SQL語句添加上separator屬性指定的字符,該參數(shù)是可選項
7.bind
bind:bind標簽可以從OGNL(對象圖導航語言)表達式中創(chuàng)建一個變量并將其綁定到上下文
Mybatis中使用Mysql的模糊查詢字符串拼接(like) 中也涉及到bind的使用
<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種動態(tài)標簽詳解的文章就介紹到這了,更多相關MyBatis動態(tài)標簽內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java封裝數(shù)組實現(xiàn)包含、搜索和刪除元素操作詳解
這篇文章主要介紹了Java封裝數(shù)組實現(xiàn)包含、搜索和刪除元素操作,結合實例形式分析了java針對數(shù)組元素的查找、刪除、判斷等相關操作封裝與使用技巧,需要的朋友可以參考下2020-03-03解決Spring Boot和Feign中使用Java 8時間日期API(LocalDate等)的序列化問題
這篇文章主要介紹了解決Spring Boot和Feign中使用Java 8時間日期API(LocalDate等)的序列化問題,需要的朋友可以參考下2018-03-03解決 Spring RestTemplate post傳遞參數(shù)時報錯問題
本文詳解說明了RestTemplate post傳遞參數(shù)時報錯的問題及其原由,需要的朋友可以參考下2020-02-02SpringBoot過濾器與攔截器深入分析實現(xiàn)方法
大家應該都曉得實現(xiàn)過濾器需要實現(xiàn) javax.servlet.Filter 接口,而攔截器會在處理指定請求之前和之后進行相關操作,配置攔截器需要兩步,本文通過實例代碼給大家介紹SpringBoot 過濾器和攔截器的相關知識,感興趣的朋友一起看看吧2022-11-11重試框架Guava-Retry和spring-Retry的使用示例
spring-retry 和 guava-retry 工具都是線程安全的重試,能夠支持并發(fā)業(yè)務場景的重試邏輯正確性,本文主要介紹了重試框架Guava-Retry和spring-Retry的使用示例,感興趣的可以一下2023-09-09