MyBatis中關于SQL的寫法總結
最近MyBatis使用較多,在這里簡單總結一下MyBatis的sql寫法
說簡單一點mybatis就是寫原?sql,官方都說了 mybatis 的動態(tài)sql語句是基于OGNL表達式的??梢苑奖愕脑?sql 語句中實現(xiàn)某些邏輯.
總體說來mybatis 動態(tài)SQL 語句主要有以下幾類:
if語句 (簡單的條件判斷)choose(when,otherwize) ,相當于java 語?中的 switch ,與 jstl 中的choose 很類似.trim(對包含的內容加上 prefix,或者 suffix 等,前綴,后綴)where(主要是?來簡化sql語句中where條件判斷的,能智能的處理 and or ,不必擔心多余導致語法誤)set(主要?于更新時)foreach(在實現(xiàn) mybatis in 語句查詢時特別有用)
一、MyBatis – if 語句
< 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>if語句十分好理解,只有提供title,content,owener,這三個參數,才會返回滿足這些條件的所有結果,這是一個非常有用的功能,如果使用JDBC就需要拼SQL語句,是非常麻煩的,相比較而言,MyBatis提供的這種動態(tài)Sql就非常的簡單
二、MyBatis --choose 語句
相當于java 語?中的 switch ,與 jstl 中的choose 很類似.
<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>when元素表?當when中的條件滿?的時候就輸出其中的內容,跟JAVA中的switch效果差不多的是按條件的順序,當when中有條件滿?的時候,就會跳出choose,即所有的when和otherwise條件中,只有個會輸出,當所有的我很條件都不滿?的時候就輸出otherwise中的內容。
所以上述語句的意思?常簡單,當title!=null的時候就輸出and titlte = #{title},不再往下判斷條件,當title為空且content!=null的時候就出and content = #{content},當所有條件都不滿?的時候就輸出otherwise中的內容。
三、MyBatis – trim
對包含的內容加上 prefix,或者 suffix 等,前綴,后綴
<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>后綴,與之對應的屬性是prefix和suffix;可以把包含內容的?部某些內容覆蓋,即忽略,也可以把尾的某些內容覆蓋,對應的屬性是prefixOverrides和suffixOverrides;正因為trim有這樣的功能,所以我們可以?常簡單的利?trim來代替where元素的功能。
四、MyBatis – where
主要是?來簡化sql語句中where條件判斷的,能智能的處理 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>where元素的作?是會在寫?where元素的地?輸出?個where,另外?個好處是你不需要考慮where素??的條件輸出是什么樣?的,MyBatis會智能的幫你處理,如果所有的條件都不滿?那么MyBatis就查出所有的記錄,如果輸出后是and 開頭的,MyBatis會把第?個and忽略,當然如果是or開頭的MyBatis也會把它忽略;此外,在where元素中你不需要考慮空格的問題,MyBatis會智能的幫你加上。
像上述?中,如果title=null, ?content != null,那么輸出的整個語句會是select * fromt_blog where content = {content},?不是select * from t_blog where and content = #{content},因為MyBatis會智能的把?個and 或 or 給忽略。
五、MyBatis – 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>set元素主要是?在更新操作的時候,它的主要功能和where元素其實是差不多的,主要是在包含的語前輸出?個set,然后如果包含的語句是以逗號結束的話將會把該逗號忽略,如果set包含的內容為空的話會出錯。
有了set元素我們就可以動態(tài)的更新那些修改了的字段。
六、MyBatis – foreach
在實現(xiàn) mybatis in 語句查詢時特別有?
<select id="queryById" resultMap="BaseReslutMap" >
?? ?select * FROM entity
?? ?where id ?in
?? ?<foreach collection="userids" item="userid" index="index" open="(" separator="," close=")">
?? ??? ?#{userid}
?? ?</foreach>
</select>foreach的主要?在構建in條件中,它可以在SQL語句中進?迭代?個集合。
foreach元素的屬性主要有item,index,collection,open,separator,close。
item表?集合中每?個元素進?迭代時的別名index指定?個名字,?于表?在迭代過程中,每次迭代到的位置,open表?該語句以什么開始,separator?在每次進?迭代之間以什么符號作為分隔符,close表?以什么結束,在使?foreach的時候最關鍵的也最容易出錯的就是collection屬性,該屬性是必須指定的,但是在不同情況下,該屬性的值是不?樣的,主要有?下3種情況:
- 1.如果傳?的是單參數且參數類型是?個List的時候,collection屬性值為list。
- 2.如果傳?的是單參數且參數類型是?個array數組的時候,collection的屬性值為array。
- 3.如果傳?的參數是多個的時候,我們就需要把它們封裝成?個Map了,當然單參數也可以封裝map實際上如果你在傳?參數的時候,在MyBatis??也是會把它封裝成?個Map的,map的key就是參數名所以這個時候collection屬性值就是傳?的List或array對象在??封裝的map??的key。
七、MyBatis – concat
模糊查詢
<select id="queryById" resultMap="BascResultMap" parameterType="entity">
?? ?SELECT * ?from entity
?? ?<where>
?? ??? ?<if test="name!=null">
?? ??? ??? ?name like ?concat('%',concat(#{name},'%'))
?? ??? ?</if>
?? ?</where>
</select>八、MyBatis – sql片段
sql?段標簽 :通過該標簽可定義能復?的sql語句?段,在執(zhí)?sql語句標簽中直接引?即可。
這既可以提?編碼效率,還能有效簡化代碼,提?可讀性。
<!--定義sql?段-->
<sql id="orderAndItem">
?? ?o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count
</sql>
<select id="findOrderAndItemsByOid" parameterType="java.lang.String" resultMap="BaseResultMap">
?? ?select
?? ?<!--引?sql?段-->
?? ?<include refid="orderAndItem" />
?? ?from ordertable o
?? ?join orderitem i on o.orderitem_id = i.orderitem_id
?? ?where o.order_id = #{orderId}
</select>需要配置的屬性:id="" >>>表?需要改sql語句?段的唯?標識
引用:通過 標簽引?,refid="" 中的值指向需要引?的 中的id=“”屬性
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring mvc 實現(xiàn)用戶登錄的方法(攔截器)
這篇文章主要介紹了Spring mvc 實現(xiàn)用戶登錄的方法(攔截器),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
SSH框架網上商城項目第6戰(zhàn)之基于DataGrid的數據顯示
SSH框架網上商城項目第6戰(zhàn)之基于DataGrid的數據顯示,提供了豐富的選擇、排序、分組和編輯數據的功能支持,感興趣的小伙伴們可以參考一下2016-05-05
解決java.sql.SQLException:?validateConnection?false問題的方法匯總(最
這篇文章主要給大家介紹了關于解決java.sql.SQLException:?validateConnection?false問題的方法匯總,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-03-03
淺談利用Spring的AbstractRoutingDataSource解決多數據源的問題
本篇文章主要介紹了淺談利用Spring的AbstractRoutingDataSource解決多數據源的問題,具有一定的參考價值,有需要的可以了解一下2017-08-08

