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ù)說明---
collection:表示類型,就寫成array,如果是集合,就寫成list
?item? : 是一個變量名,自己隨便起名
2.這種方式,可以一次執(zhí)行多條SQL語句
<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>屬性說明-------
1.prefix,suffix 表示在trim標簽包裹的部分的前面或者后面添加內(nèi)容
2.如果同時有prefixOverrides,suffixOverrides 表示會用prefix,suffix覆蓋Overrides中的內(nèi)容。
3.如果只有prefixOverrides,suffixOverrides 表示刪除開頭的或結(jié)尾的xxxOverides指定的內(nèi)容。
總結(jié)
以上所述是小編給大家介紹的Mybatis批量修改的操作代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Springboot如何利用攔截器攔截請求信息收集到日志詳解
java?9大性能優(yōu)化經(jīng)驗總結(jié)

