解决思路:让sql在即将到达1000条时,重新再加一下条件,例如:
select * from user where id in(1,2,.....999) or id in(1000,1001,...1998) or id in(1999,2000,...)
按以上思路写sql如下:
select * from user
where
(id in
<foreach collection="ids" item="item" index="index" open="(" close=")">
<if test="index != 0">
<choose>
<when test="index % 1000 == 999">) OR id in (</when>
<otherwise>,</otherwise>
</choose>
</if>
#{item}
</foreach>)
解释:index%1000==999:即每当数据条数到达1000条时就在后面加上 or id in 这个条件,当不满足这个条件并且index不为0时,就在后面加上‘,’用来隔开两个查询的id
本文介绍了一种使用MyBatis分批处理大量数据的方法,通过在数据达到1000条时重新添加查询条件来避免一次性加载过多记录,有效提高查询效率。
698

被折叠的 条评论
为什么被折叠?



