批量修改数据

需求背景:批量修改表的字段值,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);

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值