需求背景:批量修改表的字段值,where条件不一样,要修改的值也不一样
1、建表语句如下:
CREATE TABLE `t_test` (
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(60) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='test';
2、SQL代码参考如下:
update t_test set name = case
when id= 1 then 'a111'
when id= 2 then 'a222'
when id= 3 then 'a333'
else name
end
where id in (1,2,3)
3、mybatis里面的写法:
<update id="updateDeviceCodeApplicationDetailList" parameterType="java.util.List">
update device_code_application_detail set device_code = case
<foreach collection="list" item="item" >
when id= #{item.id} then #{item.deviceCode}
</foreach>
else device_code
end
where id in
<foreach item="item" collection="list" open="(" separator="," close=")">
#{item.id}
</foreach>
</update>
4、对应mapper的方法:
public int updateDeviceCodeApplicationDetailList(List<DeviceCodeApplicationDetail> deviceCodeApplicationDetailList);

1450

被折叠的 条评论
为什么被折叠?



