前言

我們知道在專案開發中之前使用資料庫查詢,都是基於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>
  1. collection:要遍歷的集合物件
  2. item:記錄每次遍歷的結果
  3. open:在結果的左邊新增內容
  4. separator:結果和結果之間的內容
  5. 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>

其他標籤使用參考點選進入·

場景案例

  1. 當我們需要對多張表的關聯資料進行復雜動態條件查詢的時候,就需要用到 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 &gt;= 0 AND tu.ADMIN_TYPE_ID &lt;= #{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標籤來解決