天天看點

Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例

Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例

  • 用于實作動态Sql的元素主要有
    • if
    • trim
    • where
    • set
    • choose(when、otherwise)
    • foreach

if 标簽詳解

需求說明

  • 實作對使用者表資料的查詢
  • 要求通過姓名模糊查詢以及性别的聯合或者單一查詢

解讀:

就是說可以通過姓名模糊單條件查詢,也可以通過性别單條件查詢,也可以雙條件查詢

問題

  • 如何實作傳入單個條件,查詢單個條件,傳入多個條件,查詢多個條件呢?
  • 處理
    • 使用if(處理參數):實作簡單的條件判斷

解決:

  • 首先寫出原始的雙條件查詢的Sql
select * from system_userinfo where userinfo_Name like 
concat(concat('%','張'),'%') and userinfo_Sex='男';

           
注:因為我使用的是oracle資料庫,它的concat函數隻有兩個參數(左字元串,右字元串),是以需要拼接兩個實作mysql的concat(left,center,right)!
  • 編寫< if>标簽
    Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例
Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例

where 标簽詳解

主要用于select的sql語句,會自動添加上where,并且自動去除條件的and或者or關鍵字。

需求說明

  • 實作對使用者表資料的查詢
  • 要求通過姓名查詢以及性别的聯合或者單一查詢

解讀:

就是說可以通過姓名單條件查詢,也可以通過性别單條件查詢,也可以雙條件查詢

問題

  • 如何實作傳入單個條件,查詢單個條件,傳入多個條件,查詢多個條件呢?
  • 處理
    • 使用where 和if(處理參數):實作簡單的條件判斷

解決

Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例
  • 測試
    Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例

set标簽詳解

主要用于update的sql語句,會自動添加上set,并且去除最後一個條件的,逗号

問題

  • 更新使用者表資料時,若某個參數為null時,會導緻更新錯誤
  • 處理
    • 使用set和if(處理參數):實作簡單的條件判斷

需求說明

  • 實作對使用者表資料的更新
  • 要求通過輸入哪些參數,更新哪些參數

解讀:

就是說可以更新使用者表資料的姓名或者性别,或者同時更新!!!

解決

  • 編寫xml檔案
    Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例
  • 測試
    Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例

trim标簽詳解

prefixOverrides=“and|or” prefix=“where” suffixOverrides="" suffix=""

屬性 介紹 常用值
prefixOverrides 字首覆寫 and或or
prefix 字首 一般是where或者set(隻執行一次)
suffixOverrides 字尾覆寫 一般是逗号
suffic 字尾 一般是更新語句的查詢條件

當然,trim标簽可以處理

<where>

<set>

标簽所需要處理的問題,根據需求使用即可

問題

  • 同上個問題
    • 更新使用者表資料時,若某個參數為null時,會導緻更新錯誤
  • 處理
    • 使用trim和if(處理參數):實作簡單的條件判斷

需求說明

  • 實作對使用者表資料的更新
  • 要求通過輸入哪些參數,更新哪些參數

解讀:

就是說可以更新使用者表資料的姓名或者性别,或者同時更新!!!

解決

Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例
  • 測試
    Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例

當然,trim也能實作where标簽的功能,一下就不在示範了!

choose标簽詳解

問題

  • 查詢使用者表,實作有姓名參數用姓名查詢,其次性别查詢,最後全部查詢
  • 處理
    • 使用choose(處理參數

需求說明

  • 實作對使用者表資料的查詢
  • 要求通過優先使用姓名查詢,其次性别查詢,最後全部查詢

解決

  • 編寫choose标簽
    Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例
  • 測試
    Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例

foreach标簽詳解

介紹

  • 疊代一個集合,通常用于in條件
  • 屬性
    • item 周遊的集合的某個元素命名
    • index 下标
    • collection類型:必須指定
      • list集合
      • array數組
      • map-key鍵值對
    • open 開始拼接的字元串内容
    • sepatator 分隔
    • close 結束拼接的字元串内容

問題

  • 查詢使用者表,查詢在角色id在集合、數組、鍵值對的記錄
  • 處理
    • 使用foreach

需求說明

  • 查詢使用者表,查詢在角色id在集合、數組、鍵值對的記錄

解決

  • 編寫foreach标簽(有一下四種方法)
    Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例
  • 接口編寫
    Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例
  • 測試
    Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例
Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例

分頁功能的實作

  • 業務需求
    • 實作對使用者表的查詢
    • 實作對使用者id的降序分頁查詢

編寫sql語句

  • 思路:
    • 首先将使用者表降序排序
select * from system_userinfo order by userinfo_uid desc;
           
  • 設定查詢記錄數的右區間(3)
select rownum r, u.* from (select * from system_userinfo order by userinfo_uid desc) u 
where rownum <=3;
           
  • 設定查詢記錄數的左區間(2)
select * from (select rownum r, u.* from (select * from system_userinfo order by userinfo_uid desc) u where rownum <=5)
        where r>2
           
  • 以上便是查詢第2-3條資料(不包含2,包含3)

編寫接口

Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例

編寫mapper檔案

Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例

編寫測試類

Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach、查詢分頁功能)詳解示例Mybatis的動态Sql(if 、trim 、where 、set 、choose(when、otherwise)、 foreach)詳解示例