MyBatis中的批量修改操作
-- mapper
void updateBatch(@Param("list") List<User> users);
--- sql
<update id="updateBatch">
update user
set
name =
<foreach collection="list" item="item" open="case id" index="index" separator="" close="end">
when #{item.id} then #{item.name}
</foreach>,
age =
<foreach collection="list" item="item" open="case id" index="index" separator="" close="end">
when #{item.id} then #{item.age}
</foreach>
where
id in
<foreach collection="list" item="item" open="(" index="index" separator="," close=")">
#{item.id}
</foreach>
</update>
总结:
1.通过case id 来保证foreach中循环的是同一个对象,在set和where条件中,从而达到批量更新