实际开发中应尽量避免在循环中操作数据库,但是遇到需要将批量数据更新到数据库中怎么办?先别慌,mysql中提空了case when语句,我们可以利用case when的特性进行批量更新操作。
<!--批量更新操作-->
<update id="updateBatch">
update sys_channel
<trim prefix="set" suffixOverrides=",">
<trim prefix="status = case" suffix="end,">
<foreach collection="list" item="item">
<if test="item.status != null">
when channel_id = #{item.channelId} then #{item.status}
</if>
</foreach>
</trim>
<trim prefix="audio = case" suffix="end,">
<foreach collection="list" item="item">
<if test="item.audio != null&