Oracle 數據庫執行SQL時報錯,提示 maximum number of expressions in a list (列表中的最大表達式數爲 1000),這個錯誤的意思是指參數個數超過了1000
解決辦法:
- 新建工具類:
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
public class FuncUtils2 {
public static <T,U> void adjustDoSize(Collection<T> collection,U otherParam,
BiFunction<List<T>,U,?> function,Function after){
int step = 991;
List<T> allId = null;
if (collection instanceof List) {
allId = (List<T>)collection;
}
else {
allId = new ArrayList<>(collection);
}
if (after != null) {
function = function.andThen(after);
}
int szAll = allId.size();
//當個數超過1000時,分批查詢;
if (szAll>=1000) {
int n = (allId.size()+step-1)/step;
for (int i=0; i<n; ++i) {
int startIndex = i*step;
int endIndex = startIndex+step;
if (endIndex>szAll) {
endIndex = szAll;
}
List<T> subList = allId.subList(startIndex,endIndex);
function.apply(subList,otherParam);
}
}
else {
function.apply(allId,otherParam);
}
}
public static <T,U,R> List<R> doQueryWithIn(Collection<T> collection,U otherParam,
BiFunction<List<T>,U,List<R>> function){
List<R> retList = new ArrayList<>();
Function f = (x)->{
retList.addAll((List)x);
return retList;
};
adjustDoSize(collection,otherParam,function,f);
return retList;
}
}
- 調用方法:
FuncUtils.doQueryWithIn(uniqueIds,null,
(x,y)->questionExtRepository.findByUniqueIdIn(x));