业务场景:更新多条数据某一相同字段的值
sql:PGSQL(参考的是mysql的写法,一样可用)
参考链接1:MySQL批量更新数据
参考链接2:Mysql Update批量更新的几种方式
我的实现方式Mybatis:
<update id="updateXxx" parameterType="java.util.List">
update A
set field = CASE id
<foreach collection="list" item="item" index="index" open="" separator="" close="">
when #{item.id} THEN #{item.field}
</foreach>
END
where id in
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
#{item.id}
</foreach>
</update>
解释:通过匹配相同id的形式,更新A表中field这个字段的值。