Mybatis批量更新
1、單個字段批量更新
<!--批量更新使用者預約狀态-->
<update id="batchUpdateReserveStatus" parameterType="java.util.List">
update dm_simulator_reserve_user
<trim prefix="set" suffixOverrides=",">
<trim prefix="reserve_status =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.reserveStatus !=null and item.reserveStatus != -1">
when id=#{item.id} then #{item.reserveStatus}
</if>
</foreach>
</trim>
</trim>
where id in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.id,jdbcType=BIGINT}
</foreach>
</update>

代碼轉化成
sql
如下:
update dm_simulator_reserve_user
set reserve_status =
case when id=? then ? when id=? then ? end
where id in ( ? , ? )
2、多個字段批量更新
<update id="updateBatch" parameterType="java.util.List">
update t_user
<trim prefix="set" suffixOverrides=",">
<trim prefix="STATUS =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.status!=null">
when USER_ID=#{i.userId} then #{i.status}
</if>
</foreach>
</trim>
<trim prefix=" OPERATE_TIME =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.operateTime!=null">
when USER_ID=#{i.userId} then #{i.operateTime}
</if>
</foreach>
</trim>
<trim prefix="OPERATOR =case" suffix="end," >
<foreach collection="list" item="i" index="index">
<if test="i.operator!=null">
when USER_ID=#{i.userId} then #{i.operator}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index" >
USER_ID=#{i.userId}
</foreach>
</update>
trim标簽的使用
MyBatis的trim标簽一般用于去除sql語句中多餘的and關鍵字,逗号,或者給sql語句前拼接 “where“、“set“以及“values(“ 等字首,或者添加“)“等字尾,可用于選擇性插入、更新、删除或者條件查詢等操作。
屬性 | 描述 |
prefix | 給sql語句拼接的字首 表示在trim包裹的SQL前添加指定内容 |
suffix | 給sql語句拼接的字尾 表示在trim包裹的SQL末尾添加指定内容 |
prefixOverrides | 表示去掉(覆寫)trim包裹的SQL的指定首部内容 去除sql語句前面的關鍵字或者字元,該關鍵字或者字元由prefixOverrides屬性指定,假設該屬性指定為"AND",當sql語句的開頭為"AND",trim标簽将會去除該"AND" |
suffixOverrides | 表示去掉(覆寫)trim包裹的SQL的指定尾部内容 去除sql語句後面的關鍵字或者字元,該關鍵字或者字元由suffixOverrides屬性指定 |
Mybatis批量删除
1、傳數組
int deleteByBatch(String[] array);
<delete id="deleteByBatch" parameterType="java.lang.String">
delete from t_enterprise_output_value
where OUTPUT_ID IN
<foreach collection="array" item="outputId" open="(" separator="," close=")">
#{outputId}
</foreach>
</delete>
2、傳map
<delete id="deleteByRole" parameterType="java.util.Map">
DELETE
FROM
t_user_role
<where>
<if test="userIdList != null">
USER_ID IN (#{userIdList,jdbcType=VARCHAR})
</if>
<if test="roleId != null">
AND ROLE_ID=#{roleId,jdbcType=VARCHAR}
</if>
<if test="sysCode != null">
AND SYSCODE=#{sysCode}
</if>
</where>
</delete>
3、多參數批量删除示例
如果删除不是以主鍵為條件,而是多個條件同時成立才可以删除
<delete id="deleteByUserIdSysRoleBatch">
delete from t_user_role
where SYSCODE = #{sysCode,jdbcType=VARCHAR} AND ROLE_ID = #{roleId,jdbcType=VARCHAR} AND USER_ID IN
<foreach collection="userIds" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</delete>
<delete id="deleteUserJob" parameterType="java.util.List">
delete from sys_user_job where
<foreach collection="list" item="item" separator=" or " index="index">
(user_id = #{item.userId} and job_id= #{item.jobId})
</foreach>
</delete>
備注
Mybatis:通過on duplicate key update實作批量插入或更新
https://blog.csdn.net/fly910905/article/details/104004165
參考連結:
https://www.cnblogs.com/javalanger/p/10899088.html