
解决mysql中in() 超过 1000报错的问题
解决mysql中in() 超过 1000报错的问题
·
解决思路:将 in() 拆分成 in or in 分批次in查询。
业务场景:查询公司所有工龄满2年员工的信息。
数据库信息:
表:user_infomation
字段:id name sex age workage(工龄) .....
实体类:id name sex age workage ....
java代码:
//存放工龄满2年的员工id,假设是有数据的,3500个元素
List<String> list = new ArrayList<>();
//传参,查询数据库
List<User> userList = userDao.getUserByIds(list);
mybatis代码:
<select id="getUserByIds" ....>
select
*
from
user_infomation
where
1=1
<if test="list != null and list.size() > 0">
and id in
<foreach collection="list" item="uid" index ="index" open="(" close=")">
<if test="index > 0">
<choose>
<when test="(index % 1000) == 999">
) or id in (
</when>
<otherwise> , </otherwise>
</choose>
</if>
#{uid}
</foreach>
</if>
</select>
总结:以上就是解决in查询时长度超过1000报错的解决思路。
结束语:okk! 欢迎大佬们的指点与改正!
更多推荐
所有评论(0)