思路:将集合分割成小于1000的若干个集合,然后利用or 关键字拼接, 下面附工具类
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.apache.commons.lang3.ObjectUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class MybatisParameterUtils {
public static <T, F> void cutInParameterForSet(LambdaQueryWrapper<T> wrapper, SFunction<T, ?> column, Set<F> set)throws Exception {
List<F> coll = new ArrayList<>(set);
List<List<F>> newList = splitList(coll, 900);
if (ObjectUtils.isEmpty(newList)) {
throw new Exception("参数错误");
} else if (newList.size() == 1) {
wrapper.in(column, newList.get(0));
return;
}
wrapper.and(i -> {
i.in(column, newList.get(0));
newList.remove(0);
for (List<F> objects : newList) {
i.or().in(column, objects);
}
});
}
public static <F> List<List<F>> splitList(List<F> list, int groupSize) {
int length = list.size();
int num = (length + groupSize - 1) / groupSize;
List<List<F>> newList = new ArrayList<>(num);
for (int i = 0; i < num; i++) {
int fromIndex = i * groupSize;
int toIndex = Math.min((i + 1) * groupSize, length);
newList.add(list.subList(fromIndex, toIndex));
}
return newList;
}
}
使用
if (StringUtils.isNotEmpty(constructionMaterialListReq.getMaterialLabel())) {
queryWrapper.apply(true, "FIND_IN_SET ('" + constructionMaterialListReq.getMaterialLabel() + "',material_labels)");
}
String fitArea = constructionMaterialListReq.getFitArea();
if (StringUtils.isNotEmpty(fitArea)) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add(fitArea);
arrayList.add(SysAreaEnum.whole_country.getKey().toString());
List<StationConstructionFitArea> list = stationConstructionFitAreaService.lambdaQuery()
.eq(StationConstructionFitArea::getBizType, DictionaryConstants.BIZ_TYPE_MATERIAL)
.in(StationConstructionFitArea::getAreaCode, arrayList)
.eq(StationConstructionFitArea::getDeletedId, 0L)
.list();
Set<Long> collect = list.stream().map(StationConstructionFitArea::getTargetId).collect(Collectors.toSet());
if (CollectionUtils.isNotEmpty(collect)) {
try {
MybatisParameterUtils.cutInParameterForSet(queryWrapper,StationConstructionMaterialInfo::getId,collect);
} catch (Exception e) {
throw new StationLandingException("物料发包group构造查询条件时异常");
}
}
|