Mybatis中updateBatch實(shí)現(xiàn)批量更新
一.更新多條數(shù)據(jù),每條數(shù)據(jù)都不一樣
背景描述:通常如果需要一次更新多條數(shù)據(jù)有兩個方式,(1)在業(yè)務(wù)代碼中循環(huán)遍歷逐條更新。(2)一次性更新所有數(shù)據(jù)(更準(zhǔn)確的說是一條sql語句來更新所有數(shù)據(jù),逐條更新的操作放到數(shù)據(jù)庫端,在業(yè)務(wù)代碼端展現(xiàn)的就是一次性更新所有數(shù)據(jù))。兩種方式各有利弊,下面將會對兩種方式的利弊做簡要分析,主要介紹第二種方式在mybatis中的實(shí)現(xiàn)。
1.逐條更新(java實(shí)現(xiàn))
這種方式顯然是最簡單,也最不容易出錯的,即便出錯也只是影響到當(dāng)條出錯的數(shù)據(jù),而且可以對每條數(shù)據(jù)都比較可控,更新失敗或成功,從什么內(nèi)容更新到什么內(nèi)容,都可以在邏輯代碼中獲取。代碼可能像下面這個樣子:
updateBatch(List<MyData> datas){
? ? for(MyData data : datas){
? ? ? ? try{
? ? ? ? ? ? myDataDao.update(data);//更新一條數(shù)據(jù),mybatis中如下面的xml文件的update
? ? ? ? }
? ? ? ? catch(Exception e){
? ? ? ? ? ? ...//如果更新失敗可以做一些其他的操作,比如說打印出錯日志等
? ? ? ? }
? ? }
}
//mybatis中update操作的實(shí)現(xiàn)
<update>
? ? update mydata
? ? set ? ...
? ? where ...
</update>這種方式最大的問題就是效率問題,逐條更新,每次都會連接數(shù)據(jù)庫,然后更新,再釋放連接資源(雖然通過連接池可以將頻繁連接數(shù)據(jù)的效率大大提高,抗不住數(shù)據(jù)量大),這中損耗在數(shù)據(jù)量較大的時候便會體現(xiàn)出效率問題。這也是在滿足業(yè)務(wù)需求的時候,通常會使用上述提到的第二種批量更新的實(shí)現(xiàn)(當(dāng)然這種方式也有數(shù)據(jù)規(guī)模的限制,后面會提到)。
2.逐條更新(mybatis實(shí)現(xiàn))
通過循環(huán),依次執(zhí)行多條update的sql
前提條件:
要實(shí)現(xiàn)批量更新,首先得設(shè)置mysql支持批量操作,在jdbc鏈接中需要附加&allowMultiQueries=true屬性才行
例如:
jdbc:mysql://localhost:3306/dbname?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
<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一次,性能比較差,容易造成阻塞。
3.sql批量更新(主力實(shí)現(xiàn))
(1)、實(shí)際實(shí)踐(傳入的是List<Map<String, Object>>)
務(wù)必注意:一定要加where條件,里面的id為需要更新的數(shù)據(jù)的id;如果不加where條件,則會全部更新,但是需要更新且有數(shù)據(jù)的更新為傳遞的數(shù)據(jù),沒有數(shù)據(jù)的則更新為null,此時更新出錯
<update id="updateChartParamByAccountAndChartid" parameterType="list">
? ? ? ? update followme_parameters
? ? ? ? <trim prefix="set" suffixOverrides=",">
? ? ? ? ? ? <trim prefix="signal_source =case" suffix="end,">
? ? ? ? ? ? ? ? <foreach collection="list" item="item" index="index">
? ? ? ? ? ? ? ? ? ? <if test="item.signalSource!=null">
? ? ? ? ? ? ? ? ? ? ? ? when account=#{item.account} and chart_id=#{item.chartId}
? ? ? ? ? ? ? ? ? ? ? ? ?then #{item.signalSource}
? ? ? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? </foreach>
? ? ? ? ? ? </trim>
? ? ? ? ? ? <trim prefix="rate =case" suffix="end,">
? ? ? ? ? ? ? ? <foreach collection="list" item="item" index="index">
? ? ? ? ? ? ? ? ? ? <if test="item.rate!=null">
? ? ? ? ? ? ? ? ? ? ? ? when account=#{item.account} and chart_id=#{item.chartId}
? ? ? ? ? ? ? ? ? ? ? ? then #{item.rate}
? ? ? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? ? ? </foreach>
? ? ? ? ? ? </trim>
? ? ? ? </trim>
? ? ? ? where id in
? ? ? ? <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
? ? ? ? ? ? #{item.id}
? ? ? ? </foreach>
? ? </update>另外文章的樣板
<update id="updateBatch" parameterType="list">
? ? ?update course
? ? ? <trim prefix="set" suffixOverrides=",">
? ? ? ?<trim prefix="peopleId =case" suffix="end,">
? ? ? ? ? ?<foreach collection="list" item="i" index="index">
? ? ? ? ? ? ? ? ? ?<if test="i.peopleId!=null">
? ? ? ? ? ? ? ? ? ? when id=#{i.id} then #{i.peopleId}
? ? ? ? ? ? ? ? ? ?</if>
? ? ? ? ? ?</foreach>
? ? ? ? </trim>
? ? ? ? <trim prefix=" roadgridid =case" suffix="end,">
? ? ? ? ? ?<foreach collection="list" item="i" index="index">
? ? ? ? ? ? ? ? ? ?<if test="i.roadgridid!=null">
? ? ? ? ? ? ? ? ? ? when id=#{i.id} then #{i.roadgridid}
? ? ? ? ? ? ? ? ? ?</if>
? ? ? ? ? ?</foreach>
? ? ? ? </trim>
? ? ? ? <trim prefix="type =case" suffix="end," >
? ? ? ? ? ?<foreach collection="list" item="i" index="index">
? ? ? ? ? ? ? ? ? ?<if test="i.type!=null">
? ? ? ? ? ? ? ? ? ? when id=#{i.id} then #{i.type}
? ? ? ? ? ? ? ? ? ?</if>
? ? ? ? ? ?</foreach>
? ? ? ? </trim>
? ? ? ? <trim prefix="unitsid =case" suffix="end," >
? ? ? ? ? ? <foreach collection="list" item="i" index="index">
? ? ? ? ? ? ? ? ? ? <if test="i.unitsid!=null">
? ? ? ? ? ? ? ? ? ? ?when id=#{i.id} then #{i.unitsid}
? ? ? ? ? ? ? ? ? ? </if>
? ? ? ? ? ? </foreach>
? ? ?</trim>
? ? </trim>
? ? where
? ? <foreach collection="list" separator="or" item="i" index="index" >
? ? ? ? id=#{i.id}
? ? </foreach>
</update>(2)、下面逐步講解
一條sql語句來批量更新所有數(shù)據(jù),下面直接看一下在mybatis中通常是怎么寫的(去掉mybatis語法就是原生的sql語句了,所有就沒單獨(dú)說sql是怎么寫的)。
<update id="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的語法來拼湊成了批量更新的sql,上面的意思就是批量更新id在updateBatch參數(shù)所傳遞List中的數(shù)據(jù)的status字段。還可以使用實(shí)現(xiàn)同樣的功能,代碼如下:
<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>
<trim>屬性說明
1.prefix,suffix 表示在trim標(biāo)簽包裹的部分的前面或者后面添加內(nèi)容
2.如果同時有prefixOverrides,suffixOverrides 表示會用prefix,suffix覆蓋Overrides中的內(nèi)容。
3.如果只有prefixOverrides,suffixOverrides 表示刪除開頭的或結(jié)尾的xxxOverides指定的內(nèi)容。
上述代碼轉(zhuǎn)化成sql如下:
update mydata_table?
? ? set status =?
? ? case
? ? ? ? when id = #{item.id} then #{item.status}//此處應(yīng)該是<foreach>展開值
? ? ? ? ...
? ? end
? ? where id in (...);當(dāng)然這是最簡單的批量更新實(shí)現(xiàn),有時候可能需要更新多個字段,那就需要將
<trim prefix="status =case" suffix="end,">
? ? ?<foreach collection="list" item="item" index="index">
? ? ? ? ? when id=#{item.id} then #{item.status}
? ? ?</foreach>
</trim>復(fù)制拷貝多次,更改prefix和when...then...的內(nèi)容即可.而如果當(dāng)需要為某個字段設(shè)置默認(rèn)值的時候可以使用else
<trim prefix="status =case" suffix="end,">
? ? ?<foreach collection="list" item="item" index="index">
? ? ? ? ? when id=#{item.id} then #{item.status}
? ? ?</foreach>
? ? ?else default_value
</trim>還有更常見的情況就是需要對要更新的數(shù)據(jù)進(jìn)行判斷,只有符合條件的數(shù)據(jù)才能進(jìn)行更新,這種情況可以這么做:
<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的數(shù)據(jù)才能進(jìn)行status更新.其他的將使用默認(rèn)值更新,而不會保持原數(shù)據(jù)不變.如果要保持原數(shù)據(jù)不變呢?即滿足條件的更新,不滿足條件的保持原數(shù)據(jù)不變,簡單的來做就是再加一個,因?yàn)閙ybatis中沒有if...else...語法,但可以通過多個實(shí)現(xiàn)同樣的效果,如下:
<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 ? ? ?//這里就是原數(shù)據(jù)
? ? ? ? ?</if>
? ? ?</foreach>
</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">
? ? ? ? ? ? ? ? ?<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//原數(shù)據(jù)
? ? ? ? ? ? ? ? ?</if>
? ? ? ? ? ? ?</foreach>
? ? ? ? </trim>
? ? </trim>
? ? where id in
? ? <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
? ? ? ? #{item.id,jdbcType=BIGINT}
? ? </foreach>
</update>4.批量更新(單個字段,傳參list),實(shí)際是sql批量更新的簡化版本而已
(1)、單個字段方法1
<update id="updateByBatch" parameterType="java.util.List">
? ? update t_goods
? ? set NODE_ID=
? ? <foreach collection="list" item="item" index="index"
? ? ? ? ? ? ?separator=" " open="case" close="end">
? ? ? when GOODS_ID=#{item.goodsId} then #{item.nodeId}
? ? </foreach>
? ? where GOODS_ID in
? ? <foreach collection="list" index="index" item="item"
? ? ? ? ? ? ?separator="," open="(" close=")">
? ? ? #{item.goodsId,jdbcType=BIGINT}
? ? </foreach>
? </update>(2)、單個字段方法2
<update id="updateByBatch" parameterType="java.util.List">
? ? UPDATE
? ? t_goods
? ? SET NODE_ID = CASE
? ? <foreach collection="list" item="item" index="index">
? ? ? WHEN GOODS_ID = #{item.goodsId} THEN #{item.nodeId}
? ? </foreach>
? ? END
? ? WHERE GOODS_ID IN
? ? <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
? ? ? #{item.goodsId}
? ? </foreach>
? </update>以上單字段更新實(shí)際執(zhí)行:
UPDATE t_goods SET NODE_ID = CASE WHEN GOODS_ID = ? THEN ? END WHERE GOODS_ID IN ( ? )
5.sql批量更新(通過insert實(shí)現(xiàn))
傳入的是List<Map<String,Object>>
直接運(yùn)行插入,如果有插入的數(shù)據(jù)轉(zhuǎn)為更新該條數(shù)據(jù)
<insert id="updateChartParamByAccountAndChartid">
insert into followme_parameters
(account,chart_id,signal_source,rate)
values
<foreach collection="list" separator="," index="index" item="item">
(#{item.account},#{item.chartId},#{item.signalSource},#{item.rate})
</foreach>
ON duplicate KEY UPDATE
signal_source=values(signal_source),rate=values(rate)
</insert>
二.更新多條數(shù)據(jù),更新的內(nèi)容一樣.
1.傳map/傳String
NODE_ID從map中取出來,goodsIdList是字符串拼接好的(如下面的"1,2,5")
<update id="updateByBatchPrimaryKey" parameterType="java.util.Map">
UPDATE t_goods
SET NODE_ID = #{nodeId}
WHERE GOODS_ID IN (${goodsIdList})
</update>
實(shí)際的sql
UPDATE t_goods SET NODE_ID = ? WHERE GOODS_ID IN (1,2,5);
2.傳map/傳list
NODE_ID從map中取出來,goodsIdList是用list拼接出來的
<update id="updateByBatchPrimaryKey" parameterType="java.util.Map">
UPDATE t_goods
SET NODE_ID = #{nodeId}
WHERE GOODS_ID IN
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item.goodsId}
</foreach>
</update>
實(shí)際的sql
UPDATE t_goods SET NODE_ID = ? WHERE GOODS_ID IN (1,2,5);
參考文章:
主力:https://blog.csdn.net/xyjawq1/article/details/74129316/
輔助:https://www.jianshu.com/p/041bec8ae6d3
到此這篇關(guān)于Mybatis中updateBatch實(shí)現(xiàn)批量更新 的文章就介紹到這了,更多相關(guān)Mybatis 批量更新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中的HashMap和Hashtable區(qū)別解析
這篇文章主要介紹了Java中的HashMap和Hashtable區(qū)別解析,HashMap和Hashtable都實(shí)現(xiàn)了Map接口,但決定用哪一個之前先要弄清楚它們之間的區(qū)別,主要的區(qū)別有線程安全性、同步和速度,需要的朋友可以參考下2023-11-11
java中treemap和treeset實(shí)現(xiàn)紅黑樹
這篇文章主要為大家詳細(xì)介紹了java中treemap和treeset實(shí)現(xiàn)紅黑樹,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
java調(diào)用mysql存儲過程實(shí)例分析
這篇文章主要介紹了java調(diào)用mysql存儲過程的方法,以實(shí)例形式較為詳細(xì)的分析了mysql數(shù)據(jù)庫的建立和存儲過程的實(shí)現(xiàn)方法,需要的朋友可以參考下2015-06-06
java實(shí)現(xiàn)統(tǒng)計字符串中字符及子字符串個數(shù)的方法示例
這篇文章主要介紹了java實(shí)現(xiàn)統(tǒng)計字符串中字符及子字符串個數(shù)的方法,涉及java針對字符串的遍歷、判斷及運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
深入淺出重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)
通常來講,重構(gòu)是指不改變功能的情況下優(yōu)化代碼,但本文所說的重構(gòu)也包括了添加功能。這篇文章主要介紹了重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)的相關(guān)資料,需要的朋友可以參考下2016-11-11

