批量修改-xml代码
mybatis批量查询,批量新增就不聊了,今天看看批量修改。
直接上代码吧
xml文件中代码如下:
<update id="updateBatchById" parameterType="java.util.List">
update
employee
set
userName =
<foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
when #{item.id} then #{item.userName}
</foreach>
, age =
<foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
when #{item.id} then #{item.age}
</foreach>
where
ID
in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.id}
</foreach>
</update>
运行出来的代码如下
update
employee
set
userName =
case ID
when ? then ?
case ID
when ? then ?
end
, age =
case ID
when ? then ?
case ID
when ? then ?
end
where
ID
in
(?,?)
mapper层代码
/**
*批量修改员工信息
* @param employees
*/
public boolean updateBatchById(List<Employee> employees);