1、Mybatis動態SQL簡介java
mybatis 的動態sql語句是基於OGNL表達式的,能夠方便的在 sql 語句中實現某些邏輯.sql
整體說來mybatis 動態SQL 語句主要有如下幾類:數組
1. if 語句 (簡單的條件判斷)mybatis
2. choose (when,otherwize) ,至關於java 語言中的 switch ,與 jstl 中的choose 很相似.app
3. trim (對包含的內容加上 prefix,或者 suffix 等,前綴,後綴)框架
4. where (主要是用來簡化sql語句中where條件判斷的,能智能的處理 and or ,沒必要擔憂多餘致使語法錯誤)ide
5. set (主要用於更新時)spa
6. foreach (在實現 mybatis in 語句查詢時特別有用)code
下面分別介紹這幾種處理方式對象
2、if
這條語句的意思很是簡單,若是你提供了title參數,那麼就要知足title=#{title},
一樣若是你提供了Content和Owner的時候,它們也須要知足相應的條件,
以後就是返回知足這些條件的全部Blog。
這是很是有用的一個功能,以往咱們使用其餘類型框架或者直接使用JDBC的時候,若是咱們要達到一樣的選擇效果的時候,
咱們就須要拼SQL語句,這是極其麻煩的,比起來,上述的動態SQL就要簡單多了
<select id="dynamicIfTest" parameterType="Blog" resultType="Blog"> select * from t_blog where 1 = 1 <if test="title != null"> and title = #{title} </if> <if test="content != null"> and content = #{content} </if> <if test="owner != null"> and owner = #{owner} </if> </select>
3、choose(when,otherwise)
when元素表示當when中的條件知足的時候就輸出其中的內容,跟JAVA中的switch效果差很少的是按照條件的順序,
當when中有條件知足的時候,就會跳出choose,
即全部的when和otherwise條件中,只有一個會輸出,
當全部的條件都不知足的時候就輸出otherwise中的內容。
因此上述語句的意思很是簡單, 當title!=null的時候就輸出and titlte = #{title},再也不往下判斷條件,
當title=null 且content!=null的時候就輸出and content = #{content},
當全部條件都不知足的時候就輸出otherwise中的內容。
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog"> select * from t_blog where 1 = 1 <choose> <when test="title != null"> and title = #{title} </when> <when test="content != null"> and content = #{content} </when> <otherwise> and owner = "owner1" </otherwise> </choose> </select>
4、trim
trim元素的主要功能是能夠在本身包含的內容前加上某些前綴,也能夠在其後加上某些後綴,與之對應的屬性是prefix和suffix;
能夠把包含內容的首部某些內容覆蓋,即忽略,也能夠把尾部的某些內容覆蓋,對應的屬性是prefixOverrides和suffixOverrides;
正由於trim有這樣的功能,因此咱們也能夠很是簡單的利用trim來代替where元素的功能
<select id="dynamicTrimTest" parameterType="Blog" resultType="Blog"> select * from t_blog <trim prefix="where" prefixOverrides="and |or"> <if test="title != null"> title = #{title} </if> <if test="content != null"> and content = #{content} </if> <if test="owner != null"> or owner = #{owner} </if> </trim></select>
5、where
where元素的做用是會在寫入where元素的地方輸出一個where,
另一個好處是你不須要考慮where元素裏面的條件輸出是什麼樣子的,MyBatis會智能的幫你處理,
若是全部的條件都不知足那麼MyBatis就會查出全部的記錄,
若是輸出後是and 開頭的,MyBatis會把第一個and忽略,固然若是是or開頭的,MyBatis也會把它忽略;
此外,在where元素中你不須要考慮空格的問題,MyBatis會智能的幫你加上。
像上述例子中,若是title=null, 而content != null,那麼輸出的整個語句會是select * from t_blog where content = #{content},
而不是select * from t_blog where and content = #{content},由於MyBatis會智能的把首個and 或 or 給忽略。
<select id="dynamicWhereTest" parameterType="Blog" resultType="Blog"> select * from t_blog <where> <if test="title != null"> title = #{title} </if> <if test="content != null"> and content = #{content} </if> <if test="owner != null"> and owner = #{owner} </if> </where></select>
6、set
set元素主要是用在更新操做的時候,它的主要功能和where元素實際上是差很少的,
主要是在包含的語句前輸出一個set,而後若是包含的語句是以逗號結束的話將會把該逗號忽略,
若是set包含的內容爲空的話則會出錯。
有了set元素咱們就能夠動態的更新那些修改了的字段。
<update id="dynamicSetTest" parameterType="Blog"> update t_blog <set> <if test="title != null"> title = #{title}, </if> <if test="content != null"> content = #{content}, </if> <if test="owner != null"> owner = #{owner} </if>
</set> where id = #{id} </update>
7、foreach
foreach的主要用在構建in條件中,它能夠在SQL語句中進行迭代一個集合。
foreach元素的屬性主要有item,index,collection,open,separator,close。
item表示集合中每個元素進行迭代時的別名,
index指定一個名字,用於表示在迭代過程當中,每次迭代到的位置,
open表示該語句以什麼開始,
separator表示在每次進行迭代之間以什麼符號做爲分隔符,
close表示以什麼結束,
在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,
可是在不一樣狀況下,該屬性的值是不同的,主要有一下3種狀況:
若是傳入的是單參數且參數類型是一個List的時候,collection屬性值爲list
若是傳入的是單參數且參數類型是一個array數組的時候,collection的屬性值爲array
若是傳入的參數是多個的時候,咱們就須要把它們封裝成一個Map了,固然單參數也能夠封裝成map,
實際上若是你在傳入參數的時候,在MyBatis裏面也是會把它封裝成一個Map的,map的key就是參數名,
因此這個時候collection屬性值就是傳入的List或array對象在本身封裝的map裏面的key
Foreach-List:
對應的mapper裏面的方法
public List<Blog> dynamicForeachTest(List<Integer> ids);
<select id="dynamicForeachTest" resultType="Blog"> select * from t_blog where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
Foreach-數組:
對應的mapper方法
public List<Blog> dynamicForeach2Test(int[] ids);
<select id="dynamicForeach2Test" resultType="Blog"> select * from t_blog where id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach></select>
另外批量新增數據也會用到foreach,用法相似,這裏就再也不贅述了