mybatis 批量更新update詳解
1 更新單條記錄
UPDATE course SET name = 'course1' WHEREid = 'id1';
2 更新多條記錄的同一個欄位為同一個值
UPDATE course SET name='course1' WHERE id in('id1','id2','id3);
3 更新多條記錄為多個欄位為不同的值
比較普通的寫法,是通過迴圈,依次執行update語句。
Mybatis寫法如下:
<update id="updateBatch" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="" close="" separator=";"> update course <set> name=${item.name} </set> where id = ${item.id} </foreach> </update>
一條記錄update一次,效能比較差,容易造成阻塞。
MySQL沒有提供直接的方法來實現批量更新,但可以使用case when語法來實現這個功能。
UPDATE course SET name = CASE id WHEN 1 THEN 'name1' WHEN 2 THEN 'name2' WHEN 3 THEN 'name3' END, title = CASE id WHEN 1 THEN 'New Title 1' WHEN 2 THEN 'New Title 2' WHEN 3 THEN 'New Title 3' END WHERE id IN (1,2,3)
這條sql的意思是,如果id為1,則name的值為name1,title的值為New Title1;依此類推。
在Mybatis中的配置則如下:
<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>
<trim>屬性說明
1.prefix,suffix 表示在trim標籤包裹的部分的前面或者後面新增內容
2.如果同時有prefixOverrides,suffixOverrides 表示會用prefix,suffix覆蓋Overrides中的內容。
3.如果只有prefixOverrides,suffixOverrides 表示刪除開頭的或結尾的xxxOverides指定的內容。
4 sql批量更新
看另外一個示例:
<updateid="updateBatch"parameterType="java.util.List">
update mydata_table
set status=
<foreach collection="list" item="item" index="index"
separator=" " open="case ID" close="end">
when #{item.id} then #{item.status}
</foreach>
where id in
<foreach collection="list" index="index" item="item"
separator="," open="(" close=")">
#{item.id,jdbcType=BIGINT}
</foreach>
</update>
其中when...then...是sql中的"switch" 語法。這裡藉助mybatis的<foreach>語法來拼湊成了批量更新的sql,上面的意思就是批量更新id在updateBatch引數所傳遞List中的資料的status欄位。還可以使用<trim>實現同樣的功能,程式碼如下:
<update id="updateBatch" parameterType="java.util.List">
update mydata_table
<trim prefix="set" suffixOverrides=",">
<trim prefix="status =case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=#{item.id} then #{item.status}
</foreach>
</trim>
</trim>
where id in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.id,jdbcType=BIGINT}
</foreach>
</update>
其結構如下:
update mydata_table
set status =
case
when id = #{item.id} then #{item.status}//此處應該是<foreach>展開值
...
end
where id in (...);
如果對要更新的資料進行判斷,只有符合條件的資料才能進行更新,這種情況可以這麼做:
<trim prefix="status =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.status !=null and item.status != -1">
when id=#{item.id} then #{item.status}
</if>
</foreach>
</trim>
這樣的話只有要更新的list中status != null && status != -1的資料才能進行status更新.其他的將使用預設值更新,而不會保持原資料不變.如果要保持原資料不變呢?即滿足條件的更新,不滿足條件的保持原資料不變,簡單的來做就是再加一個<if>,因為mybatis中沒有if...else...語法,但可以通過多個<if>實現同樣的效果,如下:
<trim prefix="status =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.status !=null and item.status != -1">
when id=#{item.id} then #{item.status}
</if>
<if test="item.status == null or item.status == -1">
when id=#{item.id} then mydata_table.status //這裡就是原資料
</if>
</foreach>
</trim>
整體批量更新的寫法如下:
<updateid="updateBatch"parameterType="java.util.List">
update mydata_table
<trim prefix="set" suffixOverrides=",">
<trim prefix="status =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.status !=null and item.status != -1">
when id=#{item.id} then #{item.status}
</if>
<if test="item.status == null or item.status == -1">
when id=#{item.id} then mydata_table.status//原資料
</if>
</foreach>
</trim>
</trim>
where id in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.id,jdbcType=BIGINT}
</foreach>
</update>
1.組裝多個update語句,這種方式需要設定jdbc連線 allowMultiQueries=true
<update id="updateEquementWaterTest" parameterType="java.util.List">
<foreach collection="list" item="item" index="index">
update rent_hl_room l
SET l.water_meter_id=#{item.equipmentCode},
l.water_meter_source_type=#{item.equipmentSource}
WHERE
l.room_id=#{item.roomId};
</foreach>
</update>
2.case when
UPDATE
rent_hl_room l
SET
electricity_meter_id =
CASE
WHEN room_id = 1942
THEN 180524348
WHEN room_id = 1945
THEN 180524480
END,
electricity_meter_source_type =
CASE
WHEN room_id = 1942
THEN ym
WHEN room_id = 1945
THEN ym
END
WHERE room_id = 1942
OR room_id = 1945
<update id="updateEquementWater" parameterType="java.util.List">
update rent_hl_room l
<trim prefix="set" suffixOverrides=",">
<trim prefix="water_meter_id =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.equipmentCode!=null">
when room_id=#{i.roomId} then #{i.equipmentCode}
</if>
</foreach>
</trim>
<trim prefix=" water_meter_source_type =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.equipmentSource!=null">
when room_id=#{i.roomId} then #{i.equipmentSource}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index" >
room_id=#{i.roomId}
</foreach>
</update>
經測試 100條資料的時候第一種方式的效率比第二種差不多高1倍。。。。。
相關推薦
mybatis 批量更新update詳解
1 更新單條記錄 UPDATE course SET name = 'course1' WHEREid = 'id1'; 2 更新多條記錄的同一個欄位為同一個值 UPDATE course SET name='course1' WHERE id in('id1'
mybatis 批量更新update
ref list mybatis admin dom 字符串 reat blog 邏輯 使用mybatis逆向工程生成的Example處理批量邏輯刪除update /* 將前端接收的id集合拼接的字符串解析 */ String idListStr = bas
mybatis批量更新update-設定多個欄位值
mybatis由於簡單易用性得到大家的認可和使用 但是在批量更新操作中,網上介紹的貌似不全,正好今天做個記錄,大家一起進步 在實際專案開發過程中,常有這樣的需求:根據ids更新表的某一個欄位值,這時的sql語句是: public interface IStaffDao {
mybatis執行批量更新update
Mybatis的批量插入這裡有http://ljhzzyx.blog.163.com/blog/static/38380312201353536375/。目前想批量更新,如果update的值是相同的話,很簡單,組織update table set column='...' where id in (1,2,3
Mybatis之Insert操作詳解(返回主鍵、批量插入)
一、前言 資料庫操作怎能少了INSERT操作呢?下面記錄MyBatis關於INSERT操作的筆記,以便日後查閱。 二、 insert元素 屬性詳解
mybatis批量更新兩種方式:1.修改值全部一樣 2.修改每條記錄值不一樣
nic mis str link eba encoding type 配置 tails Mybatis批量更新數據 mybatis批量更新兩種方式:1.修改值全部一樣 2.修改每條記錄值不一樣 mybatis批量更新兩種方式:
mybatis 批量更新
mit char 參數 -s 成功 RR ati batis pre https://blog.csdn.net/xu1916659422/article/details/77971696 註意第一種方法要想成功,需要在db鏈接url後面帶一個參數 &allo
Mybatis批量更新 updateBatch
更新 col pen AI ref 同時 case 字段 lec <update id="updateBatch" parameterType="java.util.List"> update mydata_table &
mybatis事務管理機制詳解
attribute log builder tween gen 事務管理 getc there spro 1.mybatis事務的配置和使用 mybatis事務有兩種使用方式: (a):使用JDBC的事務管理機制:即使用java.Sql.Connection對象完成對事務的
ansible批量管理服務詳解
正常 html 不存在 開始 shell腳本 server str 就是 yum安裝 在開始之前我們先試想一個場景,你的公司有成百上千臺的服務器,這時候領導要求在所有服務器上都添加一個定時任務,或者是執行某個命令,你可能會說用xshell一個一個去連或者是編寫一個shell
Spring Boot中使用MyBatis註解配置開發詳解
Spring Boot中使用MyBatis註解配置詳解 原創 2018-04-03 宗野 Spring Boot 最近專案原因可能會繼續開始使用
5分鐘學會mybatis-批量更新異常處理及問題解決
mybatis系列-批量更新異常處理及問題解決
Mysql Mybatis 批量更新
Mapper介面 /** * 批量同步更新資料 * @param collectionBids */ void updateSyncs(@Param("collectionBids") List<CollectionBid>
mybatis批量更新遇到的坑
##連線資料庫的url中要加入?allowMultiQueries=true否則會報錯 Dao int updateGxMessageInfo(@Param(“list”)List list); xml update gx_message_info set status=2 wher
mysql mybatis 批量更新和新增
一、mybatis執行批量更新batch update 的方法(mysql資料庫) 1、資料庫連線必須配置:&allowMultiQueries=true(切記一定要加上這個屬性,否則會有問題,切記!切記!切記!) 我的配置如下:jdbc:mysql://127.0.0.1:3306/t
Linux命令列批量建立目錄詳解
以前一直用-p建立目錄鏈,覺得很方便了。 在空目錄/opt/app/myapp裡建立src,再建立main,再建立java mkdir -p /opt/app/myapp/src/main/java 沒想到還可以這樣玩##¥%……&*( [email protecte
mybatis批量更新和插入
批量更新(1) <update id="updateBatch" parameterType="java.util.List"> update supplier_unit_price <trim prefix="set" suffixOverrides=
mybatis批量更新報錯 MySQLSyntaxErrorException: You have an error in your SQL syntax; check the near 'UPDAT
問題: Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corre
win7 旗艦版關閉自動更新方法詳解
現在普遍都是用win7系統,在Win7系統的使用群體中win7旗艦版系統又是最大的一部分。今天win7之家 小編要向大家介紹的電腦計算機技巧就是“win7旗艦版關閉自動更新方法”。 本來電腦系統需要更新倒是沒有什麼壞處,自動更新的系統可以幫助電腦獲取最新最好的資源,為什麼要關閉系統自動更新
MyBatis:mybatis-generator配置檔案詳解
MyBatis:mybatis-generator配置檔案詳解 mybatis-generator外掛的配置檔案詳解如下: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorC