Mybatis中動(dòng)態(tài)SQL,if,where,foreach的使用教程詳解
MyBatis的動(dòng)態(tài)SQL是基于OGNL表達(dá)式的,它可以幫助我們方便的在SQL語句中實(shí)現(xiàn)某些邏輯。
MyBatis中用于實(shí)現(xiàn)動(dòng)態(tài)SQL的元素主要有:
- if
- choose(when,otherwise)
- trim
- where
- set
- foreach
mybatis核心 對sql語句進(jìn)行靈活操作,通過表達(dá)式進(jìn)行判斷,對sql進(jìn)行靈活拼接、組裝。
1、statement中直接定義使用動(dòng)態(tài)SQL:
在statement中利用if 和 where 條件組合達(dá)到我們的需求,通過一個(gè)例子來說明:
原SQL語句:
<select id="findUserByUserQuveryVo" parameterType ="UserQueryVo" resultType="UserCustom"> select * from user where username = #{userCustom.username} and sex = #{userCustom.sex} </select>
現(xiàn)在需求是,如果返回值UserCustom為空或者UserCustom中的屬性值為空的話(在這里就是userCustom.username或者userCustom.sex)為空的話我們怎么進(jìn)行靈活的處理是程序不報(bào)異常。做法利用if和where判斷進(jìn)行SQL拼接。
<select id="findUserByUserQuveryVo" parameterType ="UserQueryVo" resultType="UserCustom"> select * from user <where> <if test="userCustom != null"> <if test="userCustom.username != null and userCustom.username != ''"><!-- 注意and不能大寫 --> and username = #{userCustom.username} </if> <if test="userCustom.sex != null and userCustom.sex != ''"> and sex = #{userCustom.sex} </if> </if> </where> </select>
有時(shí)候我們經(jīng)常使用where 1=1這條語句來處理第一條拼接語句,我們可以使用< where > < where />來同樣實(shí)現(xiàn)這一功能。
2、使用sql片段來處理statement
和我們寫程序一樣,有時(shí)候會出現(xiàn)一些重復(fù)的代碼,我們可以用SQL片段來處理。在sql片段中需要注意的是它的位置,我們也可以引用其它mapper文件里面的片段,此時(shí)需要我們定義它的位置。
(1)、sql片段的定義
<sql id="query_user_where"> <if test="sex != null and sex != ''"> and sex = #{sex} </if> <if test="id != null"> and id = #{id} </if> </sql>
(2)、sql片段的使用
<select id="findUserList" parameterType="User" resultType="User"> select * from user <where> <!-- 引用Sql片段 --> <include refid="query_user_where"></include> <!-- 在這里還要引用其它的sql片段 --> <!-- where 可以自動(dòng)去掉條件中的第一個(gè)and --> <!-- <if test="sex != null and sex != ''"> and sex = #{sex} </if> <if test="id != null"> and id = #{id} </if> --> </where> </select>
3、使用foreach進(jìn)行sql語句拼接
在向sql傳遞數(shù)組或List,mybatis使用foreach解析,我們可以使用foreach中元素進(jìn)行sql語句的拼接,請求數(shù)據(jù)。
通過一個(gè)例子來看:
需求:SELECT * FROM USER WHERE id=1 OR id=10 OR id=16
或者:SELECT * FROM USER WHERE id IN(1,10,16)
<if test="ids != null"> <foreach collection="ids" item="user_id" open="AND (" close=")" separator="or" > 每次遍歷需要拼接的串 id= #{user_id} </foreach> </if>
其中,collection:指定輸入對象中集合屬性,item: 每個(gè)遍歷生成對象,open:開始遍歷時(shí)拼接串,close: 結(jié)束遍歷是拼接的串,separator: 遍歷的兩個(gè)對象中需要拼接的串
<if test="ids != null"> <foreach collection="ids" item="user_id" open="and id IN(" close=")" separator=","> id= #{user_id} </foreach> </if>
總結(jié)
以上所述是小編給大家介紹的Mybatis中動(dòng)態(tài)SQL,if,where,foreach的使用教程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Scala 操作Redis使用連接池工具類RedisUtil
這篇文章主要介紹了Scala 操作Redis使用連接池工具類RedisUtil,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06SpringBoot ThreadLocal 簡單介紹及使用詳解
ThreadLocal 叫做線程變量,意思是 ThreadLocal 中填充的變量屬于當(dāng)前線程,該變量對其他線程而言是隔離的,也就是說該變量是當(dāng)前線程獨(dú)有的變量,這篇文章主要介紹了SpringBoot ThreadLocal 的詳解,需要的朋友可以參考下2024-01-01java聯(lián)調(diào)生成測試數(shù)據(jù)工具類方式
這篇文章主要介紹了java聯(lián)調(diào)生成測試數(shù)據(jù)工具類方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03如何使用@Value和@PropertySource注入外部資源
這篇文章主要介紹了如何使用@Value和@PropertySource注入外部資源的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06SpringBoot項(xiàng)目設(shè)置斷點(diǎn)debug調(diào)試無效忽略web.xml問題的解決
這篇文章主要介紹了SpringBoot項(xiàng)目設(shè)置斷點(diǎn)debug調(diào)試無效忽略web.xml問題的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08詳解如何獲取PreparedStatement參數(shù)示例詳解
這篇文章主要為大家介紹了詳解如何獲取PreparedStatement參數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09