mybatis如何批量修改數(shù)據(jù)
批量修改主要有兩種方式
第一種
可以通過for循環(huán)一條一條修改數(shù)據(jù),這樣會(huì)影響效率,因此我不推薦,所以在這里我也不多說。
第二種
通過修改mybatis中mapper.xml文件,如下:
<update id="updateRoleMenus" parameterType="java.util.List">?
? ? ? <foreach collection="list" item="item" index="index" open="" close="" separator=";">?
? ? ? ?update TB_ROLE_MENU ?
? ? ? ? ? <set>?
? ? ? ? ? FID=#{item.fid}
? ? ? ? ? </set>?
? ? ? ? ? where ROLEID = #{item.roleid}?
? ? ? </foreach>?
? ? </update>mysql及mybatis批量更新數(shù)據(jù)update
mysql批量更新update
使用case when語句,數(shù)據(jù)表如下:

case1:
其中age為非空字段。sql如下
update test1 set age=case when id=2 then 1 when id =3 then 2 end where id in (2,3,4)
對(duì)id為2,3,4的設(shè)置age字段,id為2的設(shè)置為1,3的設(shè)置為2,結(jié)果為:
Column ‘age’ cannot be null.
原因是由于id=4沒有被上面的when匹配到,因此會(huì)被默認(rèn)設(shè)為空null。即未被匹配到的記錄并不會(huì)保持原來的數(shù)值,而是會(huì)被設(shè)置為null。
case2:
如果想設(shè)默認(rèn)值,可寫為:
update test1 set age=case when id=2 then 1 when id =3 then 2 else 30 end where id in (2,3,4)
結(jié)果是

可見id=4的設(shè)為了30,也就是通過else default_value可以對(duì)未被匹配到的行設(shè)置默認(rèn)值。
case3:
如果想對(duì)未匹配到的行設(shè)置未原來的值,則可寫為:
update test1 set age=case when id=2 then 1 when id =3 then 2 when id =4 then test1.age end where id in (2,3,4)
這樣就可以通過test1.age來使id=4的行保持原值。在mybatis中可看到這種寫法的用處。
mybatis實(shí)現(xiàn)批量更新update
對(duì)應(yīng)上面的各種case,來寫xml。
通常在寫代碼的時(shí)候,有時(shí)候不更新的字段就不會(huì)設(shè)置到對(duì)象里,比如Person對(duì)象分別有id,name,age三個(gè)屬性對(duì)應(yīng)數(shù)據(jù)庫的三個(gè)字段,如果不想更新id=4的age時(shí),就通常不設(shè)置age的值,也就是age=null,這樣在case1里,就會(huì)被誤設(shè)為null,詳細(xì)見如下代碼。
case1:
update test1
<set>
<trim prefix="age=case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.age !=null">
when id=#{item.id} then #{item.age}
</if>
</foreach>
</trim>
<trim prefix="name=case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=#{item.id} then #{item.name}
</foreach>
</trim>
</set>
where id in
<foreach collection="list" item="item" index="index" separator="," open="(" close=")">
#{item.id}
</foreach>
當(dāng)傳入的list中person對(duì)象有三個(gè),分別對(duì)應(yīng)id=2,3,4,若不想更新4的age,而只想更新4的姓名,則id=4的person對(duì)象age就會(huì)被設(shè)為null,這樣傳入該sql時(shí),id=4的數(shù)據(jù)不會(huì)被when匹配,而又沒有設(shè)置默認(rèn)值,則原數(shù)據(jù)會(huì)被刪除并設(shè)為null。
case2:
update test1
<set>
<trim prefix="age=case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.age !=null">
when id=#{item.id} then #{item.age}
</if>
</foreach>
else 30
</trim>
<trim prefix="name=case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=#{item.id} then #{item.name}
</foreach>
</trim>
</set>
where id in
<foreach collection="list" item="item" index="index" separator="," open="(" close=")">
#{item.id}
</foreach>
該代碼由于設(shè)置了else 30,因此會(huì)id=4的數(shù)據(jù)在不設(shè)置age的情況下會(huì)被更新為30。
case3:
update test1
<set>
<trim prefix="age=case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.age !=null">
when id=#{item.id} then #{item.age}
</if>
<if test="item.age ==null">
when id=#{item.id} then test1.age
</if>
</foreach>
</trim>
<trim prefix="name=case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=#{item.id} then #{item.name}
</foreach>
</trim>
</set>
where id in
<foreach collection="list" item="item" index="index" separator="," open="(" close=")">
#{item.id}
</foreach>
通過if標(biāo)簽,若傳入的屬性為null,則保持?jǐn)?shù)據(jù)庫原值。
補(bǔ)充:
更新多條數(shù)據(jù)同一字段為同一值:
UPDATE test1 SET age=24 WHERE id in(2,3,4);
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot?Starter自定義全局加解密組件的詳細(xì)流程
SpringBoot?Starter作用將一組相關(guān)的依賴打包,簡化項(xiàng)目的配置和初始化過程,通過特定的Starter開發(fā)者可以快速的實(shí)現(xiàn)特定功能模塊的開發(fā)和擴(kuò)展,本文給大家介紹了SpringBoot?Starter自定義全局加解密組件的詳細(xì)流程,需要的朋友可以參考下2024-02-02
Springboot基于Redisson實(shí)現(xiàn)Redis分布式可重入鎖源碼解析
這篇文章主要介紹了Springboot基于Redisson實(shí)現(xiàn)Redis分布式可重入鎖,本文通過案例源碼分析給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
RocketMq 消息重試機(jī)制及死信隊(duì)列詳解
這篇文章主要為大家介紹了RocketMq 消息重試機(jī)制及死信隊(duì)列詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
關(guān)于如何正確地定義Java內(nèi)部類方法詳解
在Java中,我們通常是把不同的類創(chuàng)建在不同的包里面,對(duì)于同一個(gè)包里的類來說,它們都是同一層次的,但其實(shí)還有另一種情況,有些類可以被定義在另一個(gè)類的內(nèi)部,本文將詳細(xì)帶你了解如何正確地定義Java內(nèi)部類,需要的朋友可以參考下2023-05-05
Spring Boot應(yīng)用Docker化的步驟詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot應(yīng)用Docker化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
責(zé)任鏈模式在spring security過濾器鏈中的應(yīng)用小結(jié)
責(zé)任鏈模式在SpringSecurity過濾器鏈中的應(yīng)用,通過一系列的過濾器按順序處理請(qǐng)求,每個(gè)過濾器負(fù)責(zé)特定的安全功能,實(shí)現(xiàn)靈活且可擴(kuò)展的請(qǐng)求處理機(jī)制,感興趣的朋友跟隨小編一起看看吧2024-11-11
Java Web實(shí)現(xiàn)登錄頁面驗(yàn)證碼驗(yàn)證功能
這篇文章主要介紹了Java Web登錄頁面驗(yàn)證碼驗(yàn)證功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12

