mybatis的mapper特殊字符轉移以及動態SQL條件查詢
- 2021 年 9 月 24 日
- 筆記
- mybatis-plus, mybatisPlus
前言
我們知道在項目開發中之前使用數據庫查詢,都是基於jdbc
,進行連接查詢,然後是高級一點jdbcTemplate
進行查詢,但是我們發現還是不是很方便,有大量重複sql語句,與代碼偶合,效率低下,於是就衍生出來ORM
框架,如Mybatis,Hibernate,
還有SpringBoot的,Spring Data JPA
條件查詢
我們知道在mybatis mapper
文件中條件查詢符,如>=,<,
之類是不能直接寫的會報錯的需要轉移一下 如下圖表
詳細內容參考
常見的條件查詢操作有
我們通過mybatis 提供的特有標籤進行條件判斷,達到動態拼接sql
語句
if標籤 where標籤 choose when otherwise標籤 foreach標籤
快速入門
if標籤
語法:
<if test="xxx != null and xxx != ''">
test中寫判斷條件 參數直接paramN或者別名 多個條件使用and
或者or
連接
只要條件成立就拼接在Sql語句中,都成立就全部都拼接
注意where子句中加上1=1來規避and的風險
如下例子:
<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
關鍵字,並將第一個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
語句判斷
條件只要有一個成立,其他的就不會再判斷了。如果沒有成立的條件則默認執行otherwise
中的內容
上面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:記錄每次遍歷的結果
- open:在結果的左邊添加內容
- separator:結果和結果之間的內容
- close:在最後添加的內容
常用於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>
其他標籤使用參考點擊進入·
場景案例
- 當我們需要對多張表的關聯數據進行複雜動態條件查詢的時候,就需要用到
if
標籤進行判斷 如下
根據用戶手機號姓名年齡性別,等進行動態條件檢索,這個時候我們需要動態通過調節去拼接sql
當條件滿足sql語句加上對應條件差許
<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>
對應mapper對應的方法
<T> IPage<User> findUsersByUser(Page<T> page, @Param("userParams") SearchUserParams userParams);
對應參數實體對象
@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
標籤去判斷條件是否滿足,滿足就拼接對應sql
注意在上面我們提到的條件拼接第一個是
where
連接,而不是and
應規避and風險保證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
標籤來解決