mybatis的mapper特殊字符轉(zhuǎn)移及動(dòng)態(tài)SQL條件查詢小結(jié)
前言
我們知道在項(xiàng)目開發(fā)中之前使用數(shù)據(jù)庫(kù)查詢,都是基于jdbc,進(jìn)行連接查詢,然后是高級(jí)一點(diǎn)jdbcTemplate進(jìn)行查詢,但是我們發(fā)現(xiàn)還是不是很方便,有大量重復(fù)sql語(yǔ)句,與代碼偶合,效率低下,于是就衍生出來ORM框架,如Mybatis,Hibernate,還有SpringBoot的,Spring Data JPA
條件查詢
我們知道在mybatis mapper文件中條件查詢符,如>=,<,之類是不能直接寫的會(huì)報(bào)錯(cuò)的需要轉(zhuǎn)移一下 如下圖表

詳細(xì)內(nèi)容參考
常見的條件查詢操作有

我們通過mybatis 提供的特有標(biāo)簽進(jìn)行條件判斷,達(dá)到動(dòng)態(tài)拼接sql語(yǔ)句
if標(biāo)簽 where標(biāo)簽 choose when otherwise標(biāo)簽 foreach標(biāo)簽
快速入門
if標(biāo)簽
語(yǔ)法:
<if test="xxx != null and xxx != ''">
test中寫判斷條件 參數(shù)直接paramN或者別名 多個(gè)條件使用and或者or連接
只要條件成立就拼接在Sql語(yǔ)句中,都成立就全部都拼接
注意where子句中加上1=1來規(guī)避and的風(fēng)險(xiǎn)
如下例子:
<select id="selg" resultType="log">
select * from log where 1=1
<if test="param1!=null and param1!=''">
and outno=#{param1}
</if>
<if test="param2!=null and param2!=''">
and inno=#{param2}
</if>
</select>
where標(biāo)簽
對(duì)上面if標(biāo)簽條件判斷where連接做了處理會(huì)自動(dòng)的給Sql語(yǔ)句添加where關(guān)鍵字,并將第一個(gè)and去除
上面sql可以改造成如下:
<select id="selg" resultType="log">
select * from log
<where>
<if test="param1!=null and param1!=''">
and outno=#{param1}
</if>
<if test="param2!=null and param2!=''">
and inno=#{param2}
</if>
</where>
</select>
choose when otherwise標(biāo)簽
類似于Java語(yǔ)法中的,case,switch語(yǔ)句判斷
條件只要有一個(gè)成立,其他的就不會(huì)再判斷了。如果沒有成立的條件則默認(rèn)執(zhí)行otherwise中的內(nèi)容
上面sql可以改造成如下:
<select id="selg" resultType="log">
select * from log
<where>
<choose>
<when test="param1!=null and param1!=''">
and outno=#{param1}
</when>
<when test="param2!=null and param2!=''">
and inno=#{param2}
</when>
<otherwise>
and 1=1
</otherwise>
</choose>
</where>
</select>
foreach標(biāo)簽
語(yǔ)法:
<foreach collection="idList" item="id" open="(" separator="," close=")">
</foreach>
- collection:要遍歷的集合對(duì)象
- item:記錄每次遍歷的結(jié)果
- open:在結(jié)果的左邊添加內(nèi)容
- separator:結(jié)果和結(jié)果之間的內(nèi)容
- close:在最后添加的內(nèi)容
常用于in查詢,和批量插入操作 如下案例:
<select id="selF" parameterType="list" resultType="account">
select * from account where ano in
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
<insert id="insertBatch">
INSERT INTO t_user
(id, name, password)
VALUES
<foreach collection ="userList" item="user" separator =",">
(#{user.id}, #{user.name}, #{user.password})
</foreach >
</insert>
其他標(biāo)簽使用參考點(diǎn)擊進(jìn)入·
場(chǎng)景案例
1.當(dāng)我們需要對(duì)多張表的關(guān)聯(lián)數(shù)據(jù)進(jìn)行復(fù)雜動(dòng)態(tài)條件查詢的時(shí)候,就需要用到 if標(biāo)簽進(jìn)行判斷 如下
根據(jù)用戶手機(jī)號(hào)姓名年齡性別,等進(jìn)行動(dòng)態(tài)條件檢索,這個(gè)時(shí)候我們需要?jiǎng)討B(tài)通過調(diào)節(jié)去拼接sql 當(dāng)條件滿足sql語(yǔ)句加上對(duì)應(yīng)條件差許
<select id="findUsersByUser" resultType="cn.soboys.kmall.sys.entity.User">
select tu.USER_ID,tu.USERNAME,tu.SSEX,td.DEPT_NAME,tu.MOBILE,tu.EMAIL,tu.STATUS,tu.CREATE_TIME,
td.DEPT_ID
from t_user tu left join t_dept td on tu.DEPT_ID = td.DEPT_ID
where tu.ADMIN_TYPE_ID >= 0 AND tu.ADMIN_TYPE_ID <= #{userParams.adminType}
<if test="userParams.roleId != null and userParams.roleId != ''">
and (select group_concat(ur.ROLE_ID)
from t_user u
right join t_user_role ur on ur.USER_ID = u.USER_ID,
t_role r
where r.ROLE_ID = ur.ROLE_ID
and u.USER_ID = tu.USER_ID and r.ROLE_ID=#{userParams.roleId})
</if>
<if test="userParams.mobile != null and userParams.mobile != ''">
AND tu.MOBILE =#{userParams.mobile}
</if>
<if test="userParams.username != null and userParams.username != ''">
AND tu.USERNAME like CONCAT('%',#{userParams.username},'%')
</if>
<if test="userParams.ssex != null and userParams.ssex != ''">
AND tu.SSEX =#{userParams.ssex}
</if>
<if test="userParams.status != null and userParams.status != ''">
AND tu.STATUS =#{userParams.status}
</if>
<if test="userParams.deptId != null and userParams.deptId != ''">
AND td.DEPT_ID =#{userParams.deptId}
</if>
<if test="userParams.createTime != null and userParams.createTime != ''">
AND DATE_FORMAT(tu.CREATE_TIME,'%Y%m%d') BETWEEN substring_index(#{userParams.createTime},'#',1) and substring_index(#{userParams.createTime},'#',-1)
</if>
</select>
對(duì)應(yīng)mapper對(duì)應(yīng)的方法
<T> IPage<User> findUsersByUser(Page<T> page, @Param("userParams") SearchUserParams userParams);
對(duì)應(yīng)參數(shù)實(shí)體對(duì)象
@Data
public class SearchUserParams {
private String username;
private String mobile;
private String status;
private String ssex;
private Long deptId;
private String createTime;
private long adminType;
private String roleId;
}
通過if標(biāo)簽去判斷條件是否滿足,滿足就拼接對(duì)應(yīng)sql
注意在上面我們提到的條件拼接第一個(gè)是where連接,而不是and應(yīng)規(guī)避and風(fēng)險(xiǎn)保證sql語(yǔ)法正確 如下
<select id="findSearchCouponsPage" parameterType="cn.soboys.kmall.bean.web.params.SearchCouponParams" resultType="coupon">
select *
from coupon c
left join user_coupon uc on c.coupon_id = uc.coupon_id
WHERE 1 = 1
<if test="couponParams.userId != null and couponParams.userId != ''">
and uc.user_id =#{couponParams.userId}
</if>
<if test="couponParams.status != null and couponParams.status != ''">
and c.status =#{couponParams.status}
</if>
<if test="couponParams.couponId != null and couponParams.couponId != ''">
and c.coupon_id =#{couponParams.couponId}
</if>
<if test="couponParams.couponType != null and couponParams.couponType != ''">
and c.type =#{couponParams.couponType}
</if>
</select>
我們可以通過假定給他一個(gè)默認(rèn)條件 WHERE 1 = 1來解決,也可以通過嵌套where標(biāo)簽來解決
到此這篇關(guān)于mybatis的mapper特殊字符轉(zhuǎn)移及動(dòng)態(tài)SQL條件查詢的文章就介紹到這了,更多相關(guān)mybatis的mapper特殊字符轉(zhuǎn)移內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)配置文件的替換
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)配置文件的替換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot+Redis實(shí)現(xiàn)不重復(fù)消費(fèi)的隊(duì)列的示例代碼
本文主要介紹了SpringBoot+Redis實(shí)現(xiàn)不重復(fù)消費(fèi)的隊(duì)列的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
關(guān)于Http持久連接和HttpClient連接池的深入理解
眾所周知,httpclient是java開發(fā)中非常常見的一種訪問網(wǎng)絡(luò)資源的方式了,下面這篇文章主要給大家介紹了關(guān)于Http持久連接和HttpClient連接池的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
JAVA遞歸與非遞歸實(shí)現(xiàn)斐波那契數(shù)列
這篇文章主要為大家詳細(xì)介紹了JAVA遞歸與非遞歸實(shí)現(xiàn)斐波那契數(shù)列,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
一文詳解如何配置MyBatis實(shí)現(xiàn)打印可執(zhí)行的SQL語(yǔ)句
在MyBatis中,動(dòng)態(tài)SQL是一個(gè)強(qiáng)大的特性,允許我們?cè)赬ML映射文件或注解中編寫條件語(yǔ)句,根據(jù)運(yùn)行時(shí)的參數(shù)來決定SQL的具體執(zhí)行內(nèi)容,這篇文章主要給大家介紹了關(guān)于如何配置MyBatis實(shí)現(xiàn)打印可執(zhí)行的SQL語(yǔ)句的相關(guān)資料,需要的朋友可以參考下2024-08-08
java?list和map切割分段的實(shí)現(xiàn)及多線程應(yīng)用案例
這篇文章主要為大家介紹了java?list和map切割分段的實(shí)現(xiàn)及多線程應(yīng)用案例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Springboot靜態(tài)資源訪問實(shí)現(xiàn)代碼解析
這篇文章主要介紹了Springboot靜態(tài)資源訪問實(shí)現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Java web spring異步方法實(shí)現(xiàn)步驟解析
這篇文章主要介紹了Java web spring異步方法實(shí)現(xiàn)步驟解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
java使用jdbc連接數(shù)據(jù)庫(kù)工具類和jdbc連接mysql數(shù)據(jù)示例
這篇文章主要介紹了java使用jdbc連接數(shù)據(jù)庫(kù)的工具類和使用jdbc連接mysql數(shù)據(jù)的示例,需要的朋友可以參考下2014-03-03
Java 實(shí)戰(zhàn)項(xiàng)目之誠(chéng)途旅游系統(tǒng)的實(shí)現(xiàn)流程
讀萬(wàn)卷書不如行萬(wàn)里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+Vue+maven+Mysql實(shí)現(xiàn)一個(gè)精美的物流管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11

