Mybatis批量更新数据

本文介绍了如何使用Mybatis进行批量更新操作,通过案例展示了针对设备状态更新的SQL语句,利用casewhen函数处理多个设备的更新条件,实现了高效的数据处理。更新语句涉及设备号与状态的对应匹配,适用于需要批量更新特定字段的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mybatis批量更新实现

<update id="updateDeviceStatusBatch">
        update s_camera_device
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="status =case" suffix="end,">
                <foreach collection="devices" item="item" index="index">
                    <if test="item != null">
                        when device_no = #{item.deviceNo}
                        then #{item.status}
                    </if>
                </foreach>
            </trim>
        </trim>
        where device_no in
        <foreach collection="devices" index="index" item="item" separator="," open="(" close=")">
            #{item.deviceNo}
        </foreach>
    </update>

根据设备号更新设备当前状态

该语法使用 case when 函数来解决批量更新数据

执行SQL

update s_camera_device set status = case when device_no = 1001 then 1  
 when device_no = 1002 then 1  
 when device_no = 1003 then 0 end   
 where device_no in (1001,1002,1003)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值