mybatis中批量更新多個字段的2種實現(xiàn)方法
在mybatis中批量更新多個字段
推薦使用如下操作:
方式1:在Dao層接口中:
void updateBatch(@Param("list")List<Student> list);在對應(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īng)該是這個樣子的:
update student
set age = case
when id = #{item.id} then #{item.status}//此處應(yīng)該是<foreach>展開值
when id = #{item.id} then #{item.status}
....
end,
name = case
when id = #{item.id} then #{item.status}
...
end
where id in (?,?,?,?...);<trim>屬性說明
1.prefix,suffix 表示在trim標(biāo)簽包裹的部分的前面或者后面添加內(nèi)容
2.如果同時有prefixOverrides,suffixOverrides 表示會用prefix,suffix覆蓋Overrides中的內(nèi)容。
3.如果只有prefixOverrides,suffixOverrides 表示刪除開頭的或結(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中批量更新多個字段的2種實現(xiàn)方法的文章就介紹到這了,更多相關(guān)mybatis 批量更新多個字段內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot?log4j2.xml如何讀取application.yml中屬性值
這篇文章主要介紹了springboot?log4j2.xml如何讀取application.yml中屬性值問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Java由淺入深細(xì)數(shù)數(shù)組的操作下
數(shù)組對于每一門編程語言來說都是重要的數(shù)據(jù)結(jié)構(gòu)之一,當(dāng)然不同語言對數(shù)組的實現(xiàn)及處理也不盡相同。Java?語言中提供的數(shù)組是用來存儲固定大小的同類型元素2022-04-04
詳解Springboot Oauth2 Server搭建Oauth2認(rèn)證服務(wù)
這篇文章主要介紹了Springboot Oauth2 Server 搭建Oauth2認(rèn)證服務(wù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05

