注:本文不涉及效率处理,毕竟我跑的定时任务,1秒执行完毕跟10秒执行完毕对我来说没啥关系
起因, 很久前写了个定时更新的代码, 但是一直相安无事, 到前几天突然收到预警, 说超过了1000条数据,抛异常了,就顺手修复了下.
原语句
update tableA set key = 'updateKey' where id in(
<foreach collection="list" item="id" separator=",">
#{id}
</foreach>
)
更新后语句
update tableA set key = 'updateKey' where id in(
<foreach collection="list" item="id" index="index">
<!-- 解决 list中数据大于1K条的问题 -->
<if test="index != 0">
<choose>
<when test="index % 1000 == 999"> )
or id in (
</when>
<otherwise>,</otherwise>
</choose>
</if>
#{id}
</foreach>
)
写出来其实就是 update tableA set key = 'updateKey' where id in('A','B','C') or ('D','E','F')
PS: 再次强调, 不考虑性能问题,也不想测试性能问题,我的定时任务不讲究这些