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

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

我們通過mybatis 提供的特有標簽進行條件判斷,達到動態(tài)拼接sql語句
if標簽 where標簽 choose when otherwise標簽 foreach標簽
快速入門
if標簽
語法:
<if test="xxx != null and xxx != ''">
test中寫判斷條件 參數(shù)直接paramN或者別名 多個條件使用and或者or連接
只要條件成立就拼接在Sql語句中,都成立就全部都拼接
注意where子句中加上1=1來規(guī)避and的風(fēng)險
如下例子:
<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標簽
對上面if標簽條件判斷where連接做了處理會自動的給Sql語句添加where關(guān)鍵字,并將第一個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標簽
類似于Java語法中的,case,switch語句判斷
條件只要有一個成立,其他的就不會再判斷了。如果沒有成立的條件則默認執(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標簽
語法:
<foreach collection="idList" item="id" open="(" separator="," close=")">
</foreach>
- collection:要遍歷的集合對象
- 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>
其他標簽使用參考點擊進入·
場景案例
1.當我們需要對多張表的關(guān)聯(lián)數(shù)據(jù)進行復(fù)雜動態(tài)條件查詢的時候,就需要用到 if標簽進行判斷 如下
根據(jù)用戶手機號姓名年齡性別,等進行動態(tài)條件檢索,這個時候我們需要動態(tài)通過調(diào)節(jié)去拼接sql 當條件滿足sql語句加上對應(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>
對應(yīng)mapper對應(yīng)的方法
<T> IPage<User> findUsersByUser(Page<T> page, @Param("userParams") SearchUserParams userParams);
對應(yīng)參數(shù)實體對象
@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標簽去判斷條件是否滿足,滿足就拼接對應(yīng)sql
注意在上面我們提到的條件拼接第一個是where連接,而不是and應(yīng)規(guī)避and風(fēng)險保證sql語法正確 如下
<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>
我們可以通過假定給他一個默認條件 WHERE 1 = 1來解決,也可以通過嵌套where標簽來解決
到此這篇關(guān)于mybatis的mapper特殊字符轉(zhuǎn)移及動態(tài)SQL條件查詢的文章就介紹到這了,更多相關(guān)mybatis的mapper特殊字符轉(zhuǎn)移內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot+Redis實現(xiàn)不重復(fù)消費的隊列的示例代碼
本文主要介紹了SpringBoot+Redis實現(xiàn)不重復(fù)消費的隊列的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
關(guān)于Http持久連接和HttpClient連接池的深入理解
眾所周知,httpclient是java開發(fā)中非常常見的一種訪問網(wǎng)絡(luò)資源的方式了,下面這篇文章主要給大家介紹了關(guān)于Http持久連接和HttpClient連接池的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
一文詳解如何配置MyBatis實現(xiàn)打印可執(zhí)行的SQL語句
在MyBatis中,動態(tài)SQL是一個強大的特性,允許我們在XML映射文件或注解中編寫條件語句,根據(jù)運行時的參數(shù)來決定SQL的具體執(zhí)行內(nèi)容,這篇文章主要給大家介紹了關(guān)于如何配置MyBatis實現(xiàn)打印可執(zhí)行的SQL語句的相關(guān)資料,需要的朋友可以參考下2024-08-08
java?list和map切割分段的實現(xiàn)及多線程應(yīng)用案例
這篇文章主要為大家介紹了java?list和map切割分段的實現(xiàn)及多線程應(yīng)用案例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
Springboot靜態(tài)資源訪問實現(xiàn)代碼解析
這篇文章主要介紹了Springboot靜態(tài)資源訪問實現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Java web spring異步方法實現(xiàn)步驟解析
這篇文章主要介紹了Java web spring異步方法實現(xiàn)步驟解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
java使用jdbc連接數(shù)據(jù)庫工具類和jdbc連接mysql數(shù)據(jù)示例
這篇文章主要介紹了java使用jdbc連接數(shù)據(jù)庫的工具類和使用jdbc連接mysql數(shù)據(jù)的示例,需要的朋友可以參考下2014-03-03
Java 實戰(zhàn)項目之誠途旅游系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+Vue+maven+Mysql實現(xiàn)一個精美的物流管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11

