mybatis動態SQL標籤: if where choose otherwise trim set foreach
1. if標籤:
條件判斷標籤,通過判斷參數取值來決定是否使用某個查詢條件,基本用法如下:
<select id="testWhere" resultMap="BaseResultMap" parameterType="map">
select *from employee where 1=1
--判斷age是否等於22
<if test='age =="22"'>
and age = #{age}
</if>
--name不等於空且不能等於null
<if test="name != null and name != ''">
and name like %${name}%
</if>
</select>
這裏 where後面1=1,是爲了解決當兩個條件都不爲true時,sql語句不合法導致報錯的問題,也可以通過下面的where標籤解決
2. where標籤
- sql中不用寫where
- where標籤可以過濾掉條件語句中第一個and或or
<select id="testWhere" resultMap="BaseResultMap" parameterType="map">
select *from employee
<where> --執行時,where標籤會自動轉化爲一個where
<if test="age != null and age != ''">
age = #{age}
</if>
<if test="name != null and name != ''">
--如果age條件爲false,where標籤會去掉這個and,讓語句正確執行
and name like %${name}%
</if>
</where>
</select>
3. choose、otherwize標籤
有時候,我們並不想應用所有的條件,而只是想從多個選項中選一個,而if標籤是隻要條件爲真就會執行sql拼裝.這時就需要使用choose標籤,choose與java的switch功能類似;
<select id="chooseTest" parameterType="map" resultType="map">
select * from employee
<where>
<choose>
<when test="name != null">
and name = #{name}
</when>
<when test="age != null">
and age = #{age}
</when>
<otherwise>
and address = #{address}
</otherwise>
</choose>
</where>
</select>
mybatis會依次順序判斷各choose中的條件,當when某個條件滿足時,就會跳出choose,即只選擇第一個滿足條件的when,如果choose中的條件都不滿足,則執行otherwise中的語句;
總結:choose otherwise 類似於java的 switch…in case…default
4. trim標籤:
trim標籤用於控制sql語句的前綴及後綴,其具體屬性如下:
屬性 | 描述 |
---|---|
prefix | 指定sql語句拼接的前綴 |
subfix | 指定sql語句拼接的後綴 |
prefixOverrides | 指定sql語句前面要去除的關鍵字或字符,如AND 逗號 括號等 |
suffixOverrides | 指定sql語句後面要去除的關鍵字或字符 |
使用方法舉例:
使用trim標籤去掉多餘and或or
<select id="testWhere" resultMap="BaseResultMap" parameterType="map">
--這裏trim標籤給sql語句添加了前綴where,並會根據條件去掉多餘的and或or
select *from employee
<trim prefix="WHERE" prefixOverrides="AND | OR">
<if test="age != null">
age = #{age}
</if>
<if test="name != null">
AND name like #{name}
</if>
<if test="address != null ">
or address like #{address}
</if>
</trim>
</select>
使用trim去掉多餘的逗號:
--這是mybatis-generator插件自動生成的一條插入sql,我們來分析下
<insert id="insertSelective" parameterType="com.wg.demo.po.Department">
insert into department
--這裏添加了前綴爲(,後綴爲 ),並過濾掉了最後一個逗號
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="deptName != null">
dept_name,
</if>
<if test="descr != null">
descr,
</if>
<if test="createTime != null">
create_time, --過濾的逗號在這裏
</if>
</trim>
--這裏添加了前綴爲(,後綴爲 ),並過濾掉了最後一個逗號
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="deptName != null">
#{deptName,jdbcType=VARCHAR},
</if>
<if test="descr != null">
#{descr,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP}, --過濾的逗號在這裏
</if>
</trim>
</insert>
5. set標籤
在動態 update 語句中可以使用 set 元素動態更新列
如在update語句中,如果使用if標籤,當前面的if條件爲false時,就會出現多餘逗號的錯誤,此時用set就可以解決這個問題,如:
--這是mybatis-generator插件自動生成的條件更新sql
<update id="updateByPrimaryKeySelective" parameterType="com.wg.demo.po.Employee">
update employee
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
age = #{age,jdbcType=VARCHAR},
</if>
<if test="gender != null">
gender = #{gender,jdbcType=SMALLINT},
</if>
<if test="deptId != null">
dept_id = #{deptId,jdbcType=BIGINT},
</if>
<if test="address != null">
address = #{address,jdbcType=VARCHAR},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=TIMESTAMP}, -- 多餘的逗號會被處理掉
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
6. foreach標籤
動態sql的一個常用操作時要求對一個集合進行遍歷,通常是在IN條件語句中,如:
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT * FROM employee
WHERE id in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
--list中數據爲{1,2,3,4}
--sql拼接的結果爲: select * from employee where id in (1,2,3,4)
foreach標籤的屬性如下:
屬性 | 描述 |
---|---|
collection | 表示迭代集合的名稱,一般可爲list、set、array、map,該參數爲必選參數 |
item | 本次迭代獲取的元素,如collection爲list、set、array則item爲其中元素,若爲map,則item爲key-value中的alue,必填參數 |
open | 表示該語句以什麼開始,最常見的是左括號“(” , 可選參數 |
close | 表示該語句以什麼結束,最常見的是右括號“)” , 可選參數 |
separator | 分隔符,mybatis會在每次迭代後給item後添加一個分隔符,一般爲逗號,可選參數 |
index | 在list set 數組中,index表示當前迭代元素的下標,在map中index表示key-value中的key,可選參數 |
在來一個集合爲map的例子:
--字典表插入一條數據
<select id = "insertKeyMap" parameterType="map">
insert into t_key_value(key,value) values
<foreach collection="map" item="value" index="key" separator=",">
(#{key},#{value})
</foreach>
</select>