Mybatis批量修改的操作代碼
1.修改的字段值都是一樣的,id不同
<update id="batchUpdate" parameterType="String">
update cbp_order
set status=1
where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</update>
---參數(shù)說(shuō)明---
collection:表示類(lèi)型,就寫(xiě)成array,如果是集合,就寫(xiě)成list
?item? : 是一個(gè)變量名,自己隨便起名
2.這種方式,可以一次執(zhí)行多條SQL語(yǔ)句
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update test
<set>
test=#{item.test}+1
</set>
where id = #{item.id}
</foreach>
</update>
3.整體批量更新
<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>
----<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)容。
總結(jié)
以上所述是小編給大家介紹的Mybatis批量修改的操作代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
springboot項(xiàng)目事務(wù)標(biāo)簽驗(yàn)證
本文主要介紹了springboot項(xiàng)目事務(wù)標(biāo)簽驗(yàn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),詳細(xì)的介紹了不加事務(wù)標(biāo)簽和加事物標(biāo)簽的使用,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
詳解Java中類(lèi)與對(duì)象的關(guān)系
Springboot如何利用攔截器攔截請(qǐng)求信息收集到日志詳解
JAVA生產(chǎn)者消費(fèi)者(線程同步)代碼學(xué)習(xí)示例
java?9大性能優(yōu)化經(jīng)驗(yàn)總結(jié)

