当操作大量数据的时候往往需要分批次去处理,以减少内存和i/o的压力,比如用 mybatis-plus,添加、修改、查询大量数据时候,会造成数据库压力太大,导致服务异常,还有在执行sql 的in 方法时候,参数不能大于1000个等问题,都需要进行批量处理。下面整理一个BatchUtil 批处理工具类,很好的解决以上的问题。
工具类代码
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
public class BatchUtil {
public static final Integer NUMBER_BACH_PROTECT = 999;
public static void protectBach(List list, Consumer> bach){
if (isEmpty(list)){return;}
if (list.size() > NUMBER_BACH_PROTECT) {
for (int i = 0; i < list.size(); i += NUMBER_BACH_PROTECT) {
int lastIndex = Math.min(i + NUMBER_BACH_PROTECT, list.size());
bach.accept(list.subList(i, lastIndex));
}
}else {
bach.accept(list);
}
}
public static List protectBach(List list, Function, List> bach){
if (isEmpty(list)){return Collections.emptyList();}
if (list.size() > NUMBER_BACH_PROTECT) {
List end = new linkedList<>();
for (int i = 0; i < list.size(); i += NUMBER_BACH_PROTECT) {
int lastIndex = Math.min(i + NUMBER_BACH_PROTECT, list.size());
Optional.ofNullable(bach.apply(list.subList(i, lastIndex)))
.ifPresent(end::addAll);
}
return end;
}
return bach.apply(list);
}
private static boolean isEmpty(Collection collection) {
return collection == null || collection.isEmpty();
}
}
使用教程
比如 数据操作A和B表,A表是主表,B是子表,当批量删除A表的数据,同时删除B表关联的数据,代码如下
@Transactional(rollbackFor = Throwable.class)
public boolean delete(List ids) {
if(CollectionUtils.isEmpty(ids)){
return true;
}
removeByIds(ids);
return attributeSetService.deleteByClazz(ids);
}
@Transactional(rollbackFor = Throwable.class)
protected boolean delBySets(List setIds) {
LambdaQueryWrapper wrapper=new LambdaQueryWrapper();
wrapper.select(FeatureClazzEntity::getId).in(FeatureClazzEntity::getFeatureSetId,setIds);
List ids=list(wrapper).stream().map(FeatureClazzEntity::getId).collect(Collectors.toList());
BatchUtil.protectBach(ids, this::delete);
return true;
}
批量查询
比如 数据操作A和B表,A表是主表,B是子表,根据A表的数据,查出B表关联的数据,代码如下
protected List selectBySets(ListsetIds) { LambdaQueryWrapper wrapper=new LambdaQueryWrapper(); wrapper.select(FeatureClazzEntity::getId).in(FeatureClazzEntity::getFeatureSetId,setIds); List ids=list(wrapper).stream().map(FeatureClazzEntity::getId).collect(Collectors.toList()); List result= BatchUtil.protectBach(ids, idList->{ return attributeSetService.lambdaQuery().in(AttributeSetEntity::getClazzId,idList).list(); }); return result; }



