Mybatis動態SQL的實現示例
場景
在實際應用開發過程中,我們往往需要寫複雜的 SQL 語句,需要拼接,而拼接SQL語句又稍微不注意,由於引號,空格等缺失可能都會導致錯誤。
Mybatis提供了動態SQL,也就是可以根據使用者提供的引數,動態決定查詢語句依賴的查詢條件或SQL語句的內容。
動態SQL標籤
if 和 where 標籤
<!--動態Sql : where / if--> <select id="dynamicSql" resultType="com.lks.domain.User"> select <include refid="tableAllkey"/> from users <where> <if test="id != null and id != 0"> AND id = #{id} </if> <if test="name != null and name != ''"> AND name = #{name} </if> <if test="county != null and county != ''"> AND county = #{county} </if> </where> </select>
一般開發列表業務的查詢條件時,如果有多個查詢條件,通常會使用 標籤來進行控制。 標籤可以自動的將第一個條件前面的邏輯運算子 (or,and) 去掉,正如程式碼中寫的,id 查詢條件前面是有“and”關鍵字的,但是在打印出來的 SQL 中卻沒有,這就是 的作用。列印SQL語句的使用可以在mybatis-config檔案中新增setting標籤:
<settings> <!-- 列印查詢語句 --> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings>
choose、when、otherwise 標籤
這三個標籤需要組合在一起使用,類似於 Java 中的 switch、case、default。只有一個條件生效,也就是隻執行滿足的條件 when,沒有滿足的條件就執行 otherwise,表示預設條件。
<!--動態Sql: choose、when、otherwise 標籤--> <select id="dynamicSql2" resultType="com.lks.domain.User"> select * from users <where> <choose> <when test="name != null and name != ''"> AND name = #{name} </when> <when test="county != null and county != ''"> AND county = #{county} </when> <otherwise> AND id = #{id} </otherwise> </choose> </where> </select>
在測試類中,即使同時新增name和county的值,最終的sql也只會新增第一個屬性值。
set 標籤
使用set標籤可以將動態的配置 SET 關鍵字,並剔除追加到條件末尾的任何不相關的逗號。使用 if+set 標籤修改後,在進行表單更新的操作中,哪個欄位中有值才去更新,如果某項為 null 則不進行更新,而是保持資料庫原值。
<!--動態Sql: set 標籤--> <update id="updateSet" parameterType="com.lks.domain.User"> update users <set> <if test="name != null and name != ''"> name = #{name},</if> <if test="county != null and county != ''"> county = #{county},</if> </set> where id = #{id} </update>
trim 標籤
trim 是一個格式化標籤,可以完成< set > 或者是 < where > 標記的功能。主要有4個引數:
① prefix:字首
② prefixOverrides:去掉第一個and或者是or
③ suffix:字尾
④ suffixOverrides:去掉最後一個逗號,也可以是其他的標記
<!--動態Sql: trim 標籤--> <select id="dynamicSqlTrim" resultType="com.lks.domain.User"> select * from users <trim prefix="where" suffix="order by age" prefixOverrides="and | or" suffixOverrides=","> <if test="name != null and name != ''"> AND name = #{name} </if> <if test="county != null and county != ''"> AND county = #{county} </if> </trim> </select>
foreach 標籤
foreach標籤主要有以下引數:
item :迴圈體中的具體物件。支援屬性的點路徑訪問,如item.age,item.info.details,在list和陣列中是其中的物件,在map中是value。
index :在list和陣列中,index是元素的序號,在map中,index是元素的key,該引數可選。
open :表示該語句以什麼開始
close :表示該語句以什麼結束
separator :表示元素之間的分隔符,例如在in()的時候,separator=","會自動在元素中間用“,“隔開,避免手動輸入逗號導致sql錯誤,如in(1,2,)這樣。該引數可選。
list批量插入
<!--動態Sql: foreach標籤,批量插入--> <insert id="dynamicSqlInsertList" useGeneratedKeys="true" keyProperty="id"> insert into users (name,age,county,date) values <foreach collection="list" item="user" separator="," > (#{user.name},#{user.age},#{user.county},#{user.date}) </foreach> </insert>
從結果可以看出,我們一下插入了兩條資料,每條資料之間使用“,”進行分割,separator="," 的作用就是如此。其中< foreach >標籤內部的屬性務必加上item.。
list集合引數
<!--動態Sql: foreach標籤,list引數查詢--> <select id="dynamicSqlSelectList" resultType="com.lks.domain.User"> SELECT * from users WHERE id in <foreach collection="list" item="id" open="(" close=")" separator="," > #{id} </foreach> </select>
可以看出我們的 SQL 語句新增了:( ?,? ) ,前後的括號由 open="(" close=")" 進行控制,用“?”佔位符佔位,並通過separator以:“,”隔開,內部兩個迴圈遍歷出的元素。array 集合與 list 的做法也是類似的:
<!--動態Sql: foreach標籤,array引數查詢--> <select id="dynamicSqlSelectArray" resultType="com.lks.domain.User"> select * from users WHERE id in <foreach collection="array" item="id" open="(" close=")" separator=","> #{id} </foreach> </select>
map引數
< map> 標籤需要結合MyBatis的引數註解 @Param()來使用,需要告訴Mybatis配置檔案中的collection="map"裡的map是一個引數:
<!--動態Sql: foreach標籤,map引數查詢--> <select id="dynamicSqlSelectMap" resultType="com.lks.bean.User"> select * from users WHERE <foreach collection="map" index="key" item="value" separator="="> ${key} = #{value} </foreach> </select>
需要主要${}和#{}的使用。
到此這篇關於Mybatis動態SQL的實現示例的文章就介紹到這了,更多相關Mybatis動態SQL內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!