mybatis的mapper特殊字符轉(zhuǎn)移及動態(tài)SQL條件查詢小結(jié)
前言
我們知道在項目開發(fā)中之前使用數(shù)據(jù)庫查詢,都是基于jdbc
,進(jìn)行連接查詢,然后是高級一點(diǎn)jdbcTemplate
進(jìn)行查詢,但是我們發(fā)現(xiàn)還是不是很方便,有大量重復(fù)sql語句,與代碼偶合,效率低下,于是就衍生出來ORM
框架,如Mybatis,Hibernate,
還有SpringBoot的,Spring Data JPA
條件查詢
我們知道在mybatis mapper
文件中條件查詢符,如>=,<,
之類是不能直接寫的會報錯的需要轉(zhuǎn)移一下 如下圖表
詳細(xì)內(nèi)容參考
常見的條件查詢操作有
我們通過mybatis 提供的特有標(biāo)簽進(jìn)行條件判斷,達(dá)到動態(tài)拼接sql
語句
if標(biāo)簽 where標(biāo)簽 choose when otherwise標(biāo)簽 foreach標(biāo)簽
快速入門
if標(biāo)簽
語法:
<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標(biāo)簽
對上面if
標(biāo)簽條件判斷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標(biāo)簽
類似于Java
語法中的,case,switch
語句判斷
條件只要有一個成立,其他的就不會再判斷了。如果沒有成立的條件則默認(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)簽
語法:
<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>
其他標(biāo)簽使用參考點(diǎn)擊進(jìn)入·
場景案例
1.當(dāng)我們需要對多張表的關(guān)聯(lián)數(shù)據(jù)進(jìn)行復(fù)雜動態(tài)條件查詢的時候,就需要用到 if
標(biāo)簽進(jìn)行判斷 如下
根據(jù)用戶手機(jī)號姓名年齡性別,等進(jìn)行動態(tài)條件檢索,這個時候我們需要動態(tài)通過調(diào)節(jié)去拼接sql
當(dāng)條件滿足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ù)實(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
標(biāo)簽去判斷條件是否滿足,滿足就拼接對應(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>
我們可以通過假定給他一個默認(rèn)條件 WHERE 1 = 1
來解決,也可以通過嵌套where
標(biāo)簽來解決
到此這篇關(guān)于mybatis的mapper特殊字符轉(zhuǎn)移及動態(tài)SQL條件查詢的文章就介紹到這了,更多相關(guān)mybatis的mapper特殊字符轉(zhuǎn)移內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot中@ControllerAdvice注解的使用方法
這篇文章主要介紹了SpringBoot中@ControllerAdvice注解的使用方法,這是一個增強(qiáng)的?Controller,對controller層做異常處理、數(shù)據(jù)預(yù)處理、全局?jǐn)?shù)據(jù)綁定,?springboot?會自動掃描到,不需要調(diào)用,這個注解是spring?MVC提供的,在springboot中也可以使用,需要的朋友可以參考下2024-01-01Java在并發(fā)環(huán)境中SimpleDateFormat多種解決方案
這篇文章主要介紹了Java在并發(fā)環(huán)境中SimpleDateFormat多種解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07基于Java實(shí)現(xiàn)一個高效可伸縮的計算結(jié)果緩存
這篇文章將通過對一個計算結(jié)果緩存的設(shè)計迭代介紹,分析每個版本的并發(fā)缺陷,并分析如何修復(fù)這些缺陷,最終完成一個高效可伸縮的計算結(jié)果緩存,感興趣的小伙伴可以了解一下2023-06-06Java8使用LocalDate計算日期實(shí)例代碼解析
這篇文章主要介紹了Java8使用LocalDate計算實(shí)例代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04Java8進(jìn)行多個字段分組統(tǒng)計的實(shí)例代碼
在本篇文章里小編給大家分享的是關(guān)于Java8進(jìn)行多個字段分組統(tǒng)計的實(shí)例代碼,需要的朋友們可以學(xué)習(xí)下。2020-05-05