这篇文章主要介绍了mybatis in查询条件过长的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
这篇文章主要介绍了mybatis in查询条件过长的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
这篇文章主要介绍了mybatis in查询条件过长的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
代码:
|
1
2
3
4
5
6
7
8
|
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等分方法方法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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;} |
|
1
2
3
4
5
6
7
8
9
10
|
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逻辑:
|
1
|
ID IN(ids[0],ids[1]+...+ids[998])OR ID IN (ids[999],ids[1000],...ids[max]) |
之前公司一位同事写的方法:
|
1
2
3
4
5
6
7
8
9
|
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这种方法会报错;

|
1
2
3
|
<foreach collection="array" item="item" index="index" open="(" close=")" separator=","> <if test="(index % 999) == 998"> NULL ) OR DOC.ID IN (</if>#{item} </foreach> |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持米米素材网。
原文链接:https://blog.csdn.net/Mr_ye931/article/details/106102695
发表评论