文章目錄
-
- 1. 動态SQL 簡介
- 2. 使用動态SQL
-
- 2.1 if
- 2.2 where
- 2.3 trim
- 2.4 choose
- 2.5 set
- 2.6 foreach
- 2.7 MySQL 批量插入
- 3. 内置參數
- 4. 綁定(bind)
1. 動态SQL 簡介
通過mybatis提供的各種标簽方法實作動态拼接sql,極大的簡化我們拼裝SQL的操作。動态SQL元素和使用 JSTL 或其他類似基于 XML 的文本處理器相似。MyBatis采用功能強大的基于 OGNL 的表達式來簡化操作。
元素種類
- if
- choose (when, otherwise)
- trim (where, set)
- foreach
2. 使用動态SQL
2.1 if
查詢員工,要求,攜帶了哪個字段查詢條件就帶上這個字段的值
<!-- public List<Employee> getEmpsByConditionIf(Employee employee); -->
<select id="getEmpsByConditionIf" resultType="Employee">
select * from employee where
<!-- test:判斷表達式(OGNL)
OGNL參照PPT或者官方文檔。
c:if test
從參數中取值進行判斷,遇見特殊符号應該去寫轉義字元
-->
<if test="id!=null">
id=#{id}
</if>
<if test="lastName!=null and lastName!=''">
and last_name like #{lastName}
</if>
<if test="email!=null and email.trim()!=''">
and email=#{email}
</if>
<!-- ognl會進行字元串與數字的轉換判斷 "0"==0 -->
<if test="gender==0 or gender==1">
and gender=#{gender}
</if>
</select>
2.2 where
上面的寫法有個問題,就是,當查詢的條件中,id為空時,sql語句中,會多個
and
解決這個問題的方法有兩種
- 在
後加一個where
(隻要結果是true就行),再在1=1
前加一個id
(不推薦)and
- 使用where标簽來将所有的查詢條件包括在内(推薦)
where 标簽會自動加上 where 并且處理開頭多餘的 and 或 or,如果結尾有多餘的and 或 or 不會去掉
<select id="getEmpsByConditionIf" resultType="Employee">
select * from employee where
<!-- test:判斷表達式(OGNL)
OGNL參照PPT或者官方文檔。
c:if test
從參數中取值進行判斷,遇見特殊符号應該去寫轉義字元
-->
<if test="id!=null">
id=#{id}
</if>
<if test="lastName!=null && lastName!=''">
and last_name like #{lastName}
</if>
<if test="email!=null and email.trim()!=''">
and email=#{email}
</if>
<!-- ognl會進行字元串與數字的轉換判斷 "0"==0 -->
<if test="gender==0 or gender==1">
and gender=#{gender}
</if>
</select>
2.3 trim
如果語句後面多出
and
或者
or
,
where
标簽不能解決,可以使用
trim
标簽
trim
标簽中的屬性
- prefix : 字首,trim标簽體中是整個字元串拼串後的結果,prefix給拼串後的整個字元串加一個字首
- prefixOverrides : 字首覆寫,去掉整個字元串前面多餘的字元(
指定的字元)prefixOverrides
- suffix :字尾,suffix給拼串後的整個字元串加一個字尾
- suffixOverrides: 字尾覆寫,去掉整個字元串後面多餘的字元(
指定的字元)suffixOverrides
<select id="getEmpsByConditionTrim" resultType="Employee">
select * from employee
<!-- 自定義字元串的截取規則 -->
<trim prefix="where" suffixOverrides="and">
<if test="id!=null">
id=#{id} and
</if>
<if test="lastName!=null && lastName!=""">
last_name like #{lastName} and
</if>
<if test="email!=null and email.trim()!=""">
email=#{email} and
</if>
<!-- ognl會進行字元串與數字的轉換判斷 "0"==0 -->
<if test="gender==0 or gender==1">
gender=#{gender}
</if>
</trim>
</select>
2.4 choose
有時候,我們不想使用所有的條件,而隻是想從多個條件中選擇一個使用。針對這種情況,MyBatis 提供了
choose
元素,它有點像 Java 中的
switch
語句,隻有一個條件會被使用。
<!-- public List<Employee> getEmpsByConditionChoose(Employee employee); -->
<select id="getEmpsByConditionChoose" resultType="ean.Employee">
select * from employee
<where>
<!-- 如果帶了id就用id查,如果帶了lastName就用lastName查;隻會進入其中一個 -->
<choose>
<when test="id!=null">
id=#{id}
</when>
<when test="lastName!=null">
last_name like #{lastName}
</when>
<when test="email!=null">
email = #{email}
</when>
<otherwise>
gender = 0
</otherwise>
</choose>
</where>
</select>
2.5 set
set 元素可以用于動态包含需要更新的列,忽略其它不更新的列。set 元素會動态地在行首插入 SET 關鍵字,并會删掉額外的逗号(這些逗号是在使用條件語句給列指派時引入的)。
<!--public void updateEmp(Employee employee); -->
<update id="updateEmp">
<!-- Set标簽的使用 -->
update employee
<set>
<if test="lastName!=null">
last_name=#{lastName},
</if>
<if test="email!=null">
email=#{email},
</if>
<if test="gender!=null">
gender=#{gender}
</if>
</set>
where id=#{id}
動态更新操作,也可以使用
trim
标簽實作
update tbl_employee
<trim prefix="set" suffixOverrides=",">
<if test="lastName!=null">
last_name=#{lastName},
</if>
<if test="email!=null">
email=#{email},
</if>
<if test="gender!=null">
gender=#{gender}
</if>
</trim>
where id=#{id}
2.6 foreach
動态 SQL 的另一個常見使用場景是對集合進行周遊(尤其是在建構 IN 條件語句的時候)
foreach
中的屬性
- collection:指定要周遊的集合,list類型的參數會特殊處理封裝在map中,map的key就叫list
- item:将目前周遊出的元素指派給指定的變量
- separator:每個元素之間的分隔符
- open:周遊出所有結果拼接一個開始的字元
- close:周遊出所有結果拼接一個結束的字元
- index:索引。周遊list的時候是index就是索引,item就是目前值。周遊map的時候index表示的就是map的key,item就是map的值。
#{變量名}就能取出變量的值也就是目前周遊出的元素
<!--public List<Employee> getEmpsByConditionForeach(List<Integer> ids); -->
<select id="getEmpsByConditionForeach" resultType="com.lun.c01.helloworld.bean.Employee">
select * from employee
<foreach collection="ids" item="item_id" separator=","
open="where id in(" close=")">
#{item_id}
</foreach>
</select>
2.7 MySQL 批量插入
可以利用
foreach
标簽進行批量插入,有兩種方式
第一種:利用MySQL的插入支援values(),(),()…
<!-- 批量儲存 -->
<!--public void addEmps(@Param("emps")List<Employee> emps); -->
<!--MySQL下批量儲存:可以foreach周遊 mysql支援values(),(),()文法-->
<insert id="addEmps">
insert into employee(last_name,email,gender,department_id)
values
<foreach collection="emps" item="emp" separator=",">
(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.department.id})
</foreach>
</insert>
第二種方式:
<!-- 這種方式需要資料庫連接配接屬性allowMultiQueries=true;
這種分号分隔多個sql可以用于其他的批量操作(删除,修改) -->
<insert id="addEmps2">
<foreach collection="emps" item="emp" separator=";">
insert into employee(last_name,email,gender,department_id)
values(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.department.id})
</foreach>
</insert>
注意,MySQL資料庫連接配接屬性allowMultiQueries=true,才能批量删除,修改資料。(在連接配接MySQL的url後添加參數)。
3. 内置參數
不隻是方法傳遞過來的參數可以被用來判斷,mybatis預設還有兩個内置參數:
- _parameter:代表整個參數,如果是單個參數,
r就是這個參數。多個參數,參數會被封裝為一個map,_paramete
就是代表這個map_parameter
- _databaseId:如果配置了databaseIdProvider标簽。_databaseId就是代表目前資料庫的别名oracle
根據配置的環境,選擇執行sql語句
<!--public List<Employee> getEmpsTestInnerParameter(Employee employee); -->
<select id="getEmpsTestInnerParameter" resultType="com.lun.c01.helloworld.bean.Employee">
<if test="_databaseId=='mysql'">
select * from tbl_employee
<if test="_parameter!=null">
where last_name like #{_parameter.lastName}
</if>
</if>
<if test="_databaseId=='oracle'">
select * from employees
<if test="_parameter!=null">
where last_name like #{_parameter.lastName}
</if>
</if>
</select>
4. 綁定(bind)
可以将OGNL表達式的值綁定到一個變量中,友善後來引用這個變量的值
<!--public List<Employee> getEmpsTestInnerParameter(Employee employee); -->
<select id="getEmpsTestInnerParameter" resultType="com.atguigu.mybatis.bean.Employee">
<!-- bind:可以将OGNL表達式的值綁定到一個變量中,友善後來引用這個變量的值 -->
<bind name="lastName" value="'%'+lastName+'%'"/>
<if test="_databaseId=='mysql'">
select * from tbl_employee
<if test="_parameter!=null">
where last_name like #{lastName}<!-- 這裡使用到bind中lastName, -->
</if>
</if>
<if test="_databaseId=='oracle'">
select * from employees
<if test="_parameter!=null">
where last_name like #{_parameter.lastName}
</if>
</if>
</select>
參考:
- 尚矽谷mybatis教程
- 官方文檔
如有不足之處,歡迎指正,謝謝!