怎么解决mybatis in查询条件过长的问题,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
代码:
int splitNum =(int) Math.ceil( (float) ids.length/999); //切片数量
List<String> itemIdList = new ArrayList<>(Arrays.asList(ids));
List<List<String>> splitList = averageAssign(itemIdList, splitNum);
for (List<String> list : splitList) {
param.put("itemIds",list);
List<Map<Object, Object>> itemStatisticsList = iProcessExtMapper.getItemStatisticsList(param);
result.addAll(itemStatisticsList);
}
将list分成N等分方法方法:
public static <T> List<List<T>> averageAssign(List<T> source,int n){
List<List<T>> result=new ArrayList<List<T>>();
int remaider=source.size()%n; //(先计算出余数)
int number=source.size()/n; //然后是商
int offset=0;//偏移量
for(int i=0;i<n;i++){
List<T> value=null;
if(remaider>0){
value=source.subList(i*number+offset, (i+1)*number+offset+1);
remaider--;
offset++;
}else{
value=source.subList(i*number+offset, (i+1)*number+offset);
}
result.add(value);
}
return result;
}
i.id in
<foreach collection="itemIds" index="index" item="item" open="(" close=")">
<if test="index != 0">
<choose>
<when test="index % 1000 == 999"> ) OR ID IN( </when>
<otherwise>,</otherwise>
</choose>
</if>
#{item}
</foreach>
sql逻辑:
ID IN(ids[0],ids[1]+...+ids[998])OR ID IN (ids[999],ids[1000],...ids[max])
之前公司一位同事写的方法:
<select id="getByDirIds" parameterType="string" resultMap="dirDocLinkMap">
SELECT
<include refid="columns"/>
FROM KM_DIR_DOC_LINK T
WHERE T.DIR_ID IN
<foreach collection="array" index="index" open="(" close=")" item="item" separator=",">
<if test="(index % 1000) == 999">NULL) OR T.DIR_ID IN (</if>#{item}
</foreach>
</select>
但是随着数据量增加,发现大于2000这种方法会报错;
<foreach collection="array" item="item" index="index" open="(" close=")" separator=",">
<if test="(index % 999) == 998"> NULL ) OR DOC.ID IN (</if>#{item}
</foreach>
关于怎么解决mybatis in查询条件过长的问题问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。