前言

我們知道在專案開發中之前使用資料庫查詢,都是基於jdbc,進行連線查詢,然後是高階一點jdbcTemplate進行查詢,但是我們發現還是不是很方便,有大量重複sql語句,與程式碼偶合,效率低下,於是就衍生出來ORM框架,如Mybatis,Hibernate,還有SpringBoot的,Spring Data JPA

條件查詢

我們知道在mybatis mapper檔案中條件查詢符,如>=,<,之類是不能直接寫的會報錯的需要轉移一下 如下圖表

詳細內容參考

常見的條件查詢操作有

我們通過mybatis 提供的特有標籤進行條件判斷,達到動態拼接sql語句

if標籤 where標籤 choose when otherwise標籤 foreach標籤

快速入門

if標籤

語法:

  1. <if test="xxx != null and xxx != ''">

test中寫判斷條件 引數直接paramN或者別名 多個條件使用and或者or連線

只要條件成立就拼接在Sql語句中,都成立就全部都拼接

注意where子句中加上1=1來規避and的風險

如下例子:

  1. <select id="selg" resultType="log">
  2. select * from log where 1=1
  3. <if test="param1!=null and param1!=''">
  4. and outno=#{param1}
  5. </if>
  6. <if test="param2!=null and param2!=''">
  7. and inno=#{param2}
  8. </if>
  9. </select>

where標籤

對上面if標籤條件判斷where連線做了處理會自動的給Sql語句新增where關鍵字,並將第一個and去除

上面sql可以改造成如下:

  1. <select id="selg" resultType="log">
  2. select * from log
  3. <where>
  4. <if test="param1!=null and param1!=''">
  5. and outno=#{param1}
  6. </if>
  7. <if test="param2!=null and param2!=''">
  8. and inno=#{param2}
  9. </if>
  10. </where>
  11. </select>

choose when otherwise標籤

類似於Java語法中的,case,switch語句判斷

條件只要有一個成立,其他的就不會再判斷了。如果沒有成立的條件則預設執行otherwise中的內容

上面sql可以改造成如下:

  1. <select id="selg" resultType="log">
  2. select * from log
  3. <where>
  4. <choose>
  5. <when test="param1!=null and param1!=''">
  6. and outno=#{param1}
  7. </when>
  8. <when test="param2!=null and param2!=''">
  9. and inno=#{param2}
  10. </when>
  11. <otherwise>
  12. and 1=1
  13. </otherwise>
  14. </choose>
  15. </where>
  16. </select>

foreach標籤

語法:

  1. <foreach collection="idList" item="id" open="(" separator="," close=")">
  2. </foreach>
  1. collection:要遍歷的集合物件
  2. item:記錄每次遍歷的結果
  3. open:在結果的左邊新增內容
  4. separator:結果和結果之間的內容
  5. close:在最後新增的內容

常用於in查詢,和批量插入操作 如下案例:

  1. <select id="selF" parameterType="list" resultType="account">
  2. select * from account where ano in
  3. <foreach collection="list" item="item" open="(" separator="," close=")">
  4. #{item}
  5. </foreach>
  6. </select>

  1. <insert id="insertBatch">
  2. INSERT INTO t_user
  3. (id, name, password)
  4. VALUES
  5. <foreach collection ="userList" item="user" separator =",">
  6. (#{user.id}, #{user.name}, #{user.password})
  7. </foreach >
  8. </insert>

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

場景案例

  1. 當我們需要對多張表的關聯資料進行復雜動態條件查詢的時候,就需要用到 if標籤進行判斷 如下

根據使用者手機號姓名年齡性別,等進行動態條件檢索,這個時候我們需要動態通過調節去拼接sql 當條件滿足sql語句加上對應條件差許

  1. <select id="findUsersByUser" resultType="cn.soboys.kmall.sys.entity.User">
  2. select tu.USER_ID,tu.USERNAME,tu.SSEX,td.DEPT_NAME,tu.MOBILE,tu.EMAIL,tu.STATUS,tu.CREATE_TIME,
  3. td.DEPT_ID
  4. from t_user tu left join t_dept td on tu.DEPT_ID = td.DEPT_ID
  5. where tu.ADMIN_TYPE_ID &gt;= 0 AND tu.ADMIN_TYPE_ID &lt;= #{userParams.adminType}
  6. <if test="userParams.roleId != null and userParams.roleId != ''">
  7. and (select group_concat(ur.ROLE_ID)
  8. from t_user u
  9. right join t_user_role ur on ur.USER_ID = u.USER_ID,
  10. t_role r
  11. where r.ROLE_ID = ur.ROLE_ID
  12. and u.USER_ID = tu.USER_ID and r.ROLE_ID=#{userParams.roleId})
  13. </if>
  14. <if test="userParams.mobile != null and userParams.mobile != ''">
  15. AND tu.MOBILE =#{userParams.mobile}
  16. </if>
  17. <if test="userParams.username != null and userParams.username != ''">
  18. AND tu.USERNAME like CONCAT('%',#{userParams.username},'%')
  19. </if>
  20. <if test="userParams.ssex != null and userParams.ssex != ''">
  21. AND tu.SSEX =#{userParams.ssex}
  22. </if>
  23. <if test="userParams.status != null and userParams.status != ''">
  24. AND tu.STATUS =#{userParams.status}
  25. </if>
  26. <if test="userParams.deptId != null and userParams.deptId != ''">
  27. AND td.DEPT_ID =#{userParams.deptId}
  28. </if>
  29. <if test="userParams.createTime != null and userParams.createTime != ''">
  30. AND DATE_FORMAT(tu.CREATE_TIME,'%Y%m%d') BETWEEN substring_index(#{userParams.createTime},'#',1) and substring_index(#{userParams.createTime},'#',-1)
  31. </if>
  32. </select>

對應mapper對應的方法

  1. <T> IPage<User> findUsersByUser(Page<T> page, @Param("userParams") SearchUserParams userParams);

對應引數實體物件

  1. @Data
  2. public class SearchUserParams {
  3. private String username;
  4. private String mobile;
  5. private String status;
  6. private String ssex;
  7. private Long deptId;
  8. private String createTime;
  9. private long adminType;
  10. private String roleId;
  11. }

通過if標籤去判斷條件是否滿足,滿足就拼接對應sql

注意在上面我們提到的條件拼接第一個是where連線,而不是and應規避and風險保證sql語法正確 如下

  1. <select id="findSearchCouponsPage" parameterType="cn.soboys.kmall.bean.web.params.SearchCouponParams" resultType="coupon">
  2. select *
  3. from coupon c
  4. left join user_coupon uc on c.coupon_id = uc.coupon_id
  5. WHERE 1 = 1
  6. <if test="couponParams.userId != null and couponParams.userId != ''">
  7. and uc.user_id =#{couponParams.userId}
  8. </if>
  9. <if test="couponParams.status != null and couponParams.status != ''">
  10. and c.status =#{couponParams.status}
  11. </if>
  12. <if test="couponParams.couponId != null and couponParams.couponId != ''">
  13. and c.coupon_id =#{couponParams.couponId}
  14. </if>
  15. <if test="couponParams.couponType != null and couponParams.couponType != ''">
  16. and c.type =#{couponParams.couponType}
  17. </if>
  18. </select>

我們可以通過假定給他一個預設條件 WHERE 1 = 1來解決,也可以通過巢狀where標籤來解決