MyBatis的動態SQL是基於OGNL表達式的,它可以幫助我們方便的在SQL語句中實現某些邏輯。
1、if 標籤
使用 if 元素判斷,如果值爲 null 或等於空字符串,我們就不進行此條件的判斷,增加靈活性。
<!-- 添加 if(判斷參數) - 將實體類 User 不爲空的屬性作爲 where 條件 -->
<select id="getUserList" resultMap="resultMap_User" parameterType="com.yiibai.pojo.User">
SELECT u.username,
u.password,
u.sex,
u.birthday,
u.photo,
u.score,
u.sign
FROM user u
WHERE
<if test="userId != null and userId != '' ">
AND id.user_id = #{userId, jdbcType=VARCHAR}
</if>
</select>
或者 if-else 寫法
<select id="selectSelective" resultMap="xxx" parameterType="xxx">
select
<include refid="Base_Column_List"/>
from xxx
where del_flag=0
<choose>
<when test="xxx !=null and xxx != ''">
and xxx like concat(concat('%', #{xxx}), '%')
</when>
<otherwise>
and xxx like '**%'
</otherwise>
</choose>
</select>
2、foreach 標籤
foreach 的主要用在構建in條件中,它可以在SQL語句中進行迭代一個集合,用於傳入一個list參數。
public void delQuestion(@Param("idList") List<String> idList);
<where> AND id IN
<foreach collection="idList" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</where>
- 如果傳入的是單參數且參數類型是一個List的時候,collection屬性值爲list。
- 如果傳入的是單參數且參數類型是一個array數組的時候,collection的屬性值爲array。
- 如果傳入的參數是多個的時候,我們就需要把它們封裝成一個Map了,當然單參數也可。
(1)實現批量更新
<updateid="updateBatch"parameterType="list">
update course
<trim prefix="set" suffixOverrides=",">
<trim prefix="name=case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.name!=null">
when id=#{item.id} then #{item.name}
</if>
</foreach>
</trim>
<trim prefix="title =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.title!=null">
when id=#{item.id} then #{item.title}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="item" index="index">
id=#{item.id}
</foreach>
</update>
(2)實現批量刪除
<!-- 批量刪除 -->
<delete id="deleteMoreEmp" parameterType="int[]">
<!-- delete from emp where empno in(7789,7790) -->
<!-- forEach : 用來循環 collection : 用來指定循環的數據的類型 可以填的值有:array,list,map item -->
delete from emp where empno in
<foreach collection="array" item="arr" index="no" open="("
separator="," close=")">
#{arr}
</foreach>
</delete>
(3)實現批量插入or更新數據(MySQL庫)
//批量新增or更新數據
public Boolean InsertOrUpdate_List(List<MyInfo> info);
<insert id="InsertOrUpdate_List" parameterType="MyInfo">
insert into UesrInfo (id,type,name)
values
<foreach collection ="list" item="info" separator="," >
(#{info.id},#{info.type},#{info.name})
</foreach>
ON DUPLICATE KEY UPDATE
type = VALUES(type),name = VALUES(name)
</insert>
3、include 標籤
在數據庫的使用中,查詢的時候時候不要使用*號,*是查詢所有,可以用include來拼接SQL語句。
<sql id="result_column">
id,title,small_pic,intime,status,inuser,org_name,org_id,online_from_date,online_to_date,source_type,update_time,org_type
</sql>
<select id="getFocusInfoBatchById" resultMap="BaseResultMap">
select
<include refid="result_column"/>
from
focus_info_batch
where
id = #{id}
</select>
4、trim 標籤
trim標記是一個格式化的標記,可以完成set或者是where標記的功能,自定義格式。有如下屬性:
<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>
- prefix:在trim標籤內sql語句加上前綴。
- suffix:在trim標籤內sql語句加上後綴。
- suffixOverrides:指定去除多餘的後綴內容,如:suffixOverrides=",",去除trim標籤內sql語句多餘的後綴","。
- prefixOverrides:指定去除多餘的前綴內容。
demo展示:
<insert id="insert" parameterType="com.tortuousroad.groupon.cart.entity.Cart">
insert into cart
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="userId != null">
user_id,
</if>
<if test="dealId != null">
deal_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="userId != null">
#{userId,jdbcType=BIGINT},
</if>
</trim>
</insert>
5、<![CDATA[ sql 語句 ]]> 特殊字符
在mapper文件中寫sql語句時,遇到特殊字符時,不被解析器解析。
(1)<![CDATA[]]>
<select id="allUserInfo" parameterType="java.util.HashMap" resultMap="userInfo1">
<![CDATA[
SELECT newsEdit,newsId, newstitle FROM shoppingGuide WHERE 1=1 AND newsday > #{startTime} AND newsday <= #{endTime}
]]>
<if test="query.abnormalFlag != null">
AND NOW() <![CDATA[ >= ]]> dateStart <!-- 判斷條件 開始時間 -->
</if>
</select>
(2)轉移符
符號 | 轉移符 | 說明 |
---|---|---|
> | > |
大於 |
< | < |
小於 |
>= | >= |
大於等於 |
<= | << |
小於等於 |
& | & |
和 |
‘ | ' |
單引號 |
“ | " |
雙引號 |