背景:
我们使用mysql in超过1024直接报错,想到解决办法,这里我每1000个让他 or in 一下。
解决方案:
在代码中,造一个每1000个ID放到一个list中,然后把这些list再放list里。
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; //然后是商
if( number>0){
System.out.println("进入》1");
int times=number;
if(remaider>0){
times+=1;
}
for(int i=0;i<times;i++){
List<T> value=null;
// 最后一次,如果
if(i==number&& remaider>0){
int start=n*i;
int end=source.size();
System.out.println("i,开始结束:"+i+","+start+","+end);
//左闭右开, 第一个参数是开始位置(包含),第二参数是结束位置(不包含)
value=source.subList(start,end);
}else{
int start=n*i;
int end=n*(i+1);
/*if(i!=0){
// 只有第一次是不加1
start+=1;
}*/
System.out.println("i,开始结束:"+i+","+start+","+end);
value=source.subList(start,end);
}
result.add(value);
}
}else{
System.out.println("不足,直接放入source.size():"+source.size());
result.add(source);
}
return result;
}
这样我们就能获取到
List<List<xxxBean>>,这时候你需要去循环,然后stram一下找到对应ID,不再赘述;
然后到mybatis部分,如下图:
<if test="bList != null and bList.size()>0">
and (a.ID in
<foreach collection="bList" item="subList" open="(" close=")" index="fst">
<if test="fst != 0">
) OR a.ID in(
</if>
<foreach collection="subList" item="item" index="idx">
<if test="idx != 0">
,
</if>
#{item}
</foreach>
</foreach>
)
</if>
这个鸡毛foreach我调了2小时,本来抄xi(呸)参考别人的写法,但是不适用,我愣是没看明白,还是按照Java 的写法来吧,只要你第一步分割好了,传进来自动给你or in