虽然不经常遇到但每次遇到都要重写下,这里把代码保存下方便复制粘贴
调用
new SqlUtilForIn<MakerDto>(mapper, "listMaker", new ArrayList<>(codes)).get();
util类
package org.jon.lv.utils;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class SqlUtilForIn<T> {
private Method method;
private BaseMapper mapper;
private List<Object> codes;
private int step = 500;
public SqlUtilForIn(BaseMapper mapper, String methodName, List<Object> codes) throws NoSuchMethodException {
super();
this.mapper = mapper;
this.method = mapper.getClass().getMethod(methodName, Collection.class);
this.codes = codes;
}
public void setSize(int step) {
this.step = step;
}
public List<T> get() throws InvocationTargetException, IllegalAccessException {
if (codes.size() < step) {
return (List<T>) method.invoke(mapper, codes);
} else {
int size = step;
int index = 0;
int max = codes.size();
List<T> result = new ArrayList<>();
while (true) {
int start = index * size;
int end = size * (index + 1);
end = max > end ? end : max;
result.addAll((List<T>) method.invoke(mapper, codes.subList(start, end)));
if (end >= max) {
break;
}
index++;
}
return result;
}
}
}