mybatis中批量更新多個(gè)字段的2種實(shí)現(xiàn)方法
在mybatis中批量更新多個(gè)字段
推薦使用如下操作:
方式1:在Dao層接口中:
void updateBatch(@Param("list")List<Student> list);
在對(duì)應(yīng)的mapper文件中如下:
<update id="updateBatch" parameType="java.lang.List"> update student <trim prefix="set" suffixOverrides=","> <trim prefix=" age = case " suffix="end,"> <foreach collection="list" item="stu" index="index"> <if test=" item.age != null and item.id != null"> when id = #{item.id} then #{item.age} </if> <if test=" item.age == null and item.id != null"> when id = #{item.id} then mydata_table.age //原始值 </if> </foreach> </trim> <trim prefix=" name = case" suffix="end,"> <foreach collection="list" item="stu" index="index"> <if test=" item.name!= null and item.id != null"> when id = #{item.id} then #{item.name} </if> <if test=" item.name == null and item.id != null"> when id = #{item.id} then mydata_table.name //原始值 </if> </foreach> </trim> </trim> </update>
上面的sql語(yǔ)句打印出來(lái),應(yīng)該是這個(gè)樣子的:
update student set age = case when id = #{item.id} then #{item.status}//此處應(yīng)該是<foreach>展開(kāi)值 when id = #{item.id} then #{item.status} .... end, name = case when id = #{item.id} then #{item.status} ... end where id in (?,?,?,?...);
<trim>屬性說(shuō)明
1.prefix,suffix 表示在trim標(biāo)簽包裹的部分的前面或者后面添加內(nèi)容
2.如果同時(shí)有prefixOverrides,suffixOverrides 表示會(huì)用prefix,suffix覆蓋Overrides中的內(nèi)容。
3.如果只有prefixOverrides,suffixOverrides 表示刪除開(kāi)頭的或結(jié)尾的xxxOverides指定的內(nèi)容
方式2:在Dao層接口方法定義同上
mapper文件如下:
<update id="updateBatch" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="" close="" separator=";"> update student <set> name=#{item.name}, age = #{item.age} </set> where id = #{item.id} </foreach> </update>
到此這篇關(guān)于mybatis中批量更新多個(gè)字段的2種實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)mybatis 批量更新多個(gè)字段內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Mybatis中使用updateBatch進(jìn)行批量更新
- 詳解mybatis 批量更新數(shù)據(jù)兩種方法效率對(duì)比
- mybatisplus添加真正的批量新增、批量更新的實(shí)現(xiàn)
- Mybatis批量更新三種方式的實(shí)現(xiàn)
- mybatis執(zhí)行update批量更新時(shí)報(bào)錯(cuò)的解決方案
- Mybatis批量更新報(bào)錯(cuò)問(wèn)題
- Mybatis傳入List實(shí)現(xiàn)批量更新的示例代碼
- MyBatis foreach 批量更新實(shí)例
- Mybatis執(zhí)行多條語(yǔ)句/批量更新方式
- Mybatis中如何進(jìn)行批量更新(updateBatch)
相關(guān)文章
java隨機(jī)數(shù)生成具體實(shí)現(xiàn)代碼
這篇文章主要為大家分享了java隨機(jī)數(shù)生成具體實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04Spring項(xiàng)目里將SQL語(yǔ)句寫在.sql文件中的方法
這篇文章主要介紹了Spring項(xiàng)目里如何將SQL語(yǔ)句寫在.sql文件中的方法,文中給出了詳細(xì)的介紹和示例代碼,相信對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,有需要的朋友們下面來(lái)一起看看吧。2017-01-01springboot?log4j2.xml如何讀取application.yml中屬性值
這篇文章主要介紹了springboot?log4j2.xml如何讀取application.yml中屬性值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Java由淺入深細(xì)數(shù)數(shù)組的操作下
數(shù)組對(duì)于每一門編程語(yǔ)言來(lái)說(shuō)都是重要的數(shù)據(jù)結(jié)構(gòu)之一,當(dāng)然不同語(yǔ)言對(duì)數(shù)組的實(shí)現(xiàn)及處理也不盡相同。Java?語(yǔ)言中提供的數(shù)組是用來(lái)存儲(chǔ)固定大小的同類型元素2022-04-04詳解Springboot Oauth2 Server搭建Oauth2認(rèn)證服務(wù)
這篇文章主要介紹了Springboot Oauth2 Server 搭建Oauth2認(rèn)證服務(wù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05