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)