贴代码吧,简单封装了一个分批次操作数据的工具类,依赖Hutool和Lombok,少写几行代码偷偷懒
package org.ssh.boot.pay.util;import cn.hutool.core.collection.CollUtil;import cn.hutool.core.thread.ThreadUtil;import lombok.extern.slf4j.Slf4j;import java.util.List;import java.util.concurrent.CompletableFuture;/*** 批量处理数据工具类*/@Slf4jpublic class BatchDataUtils {private final static int BATCH_SIZE = 1000;/*** 分批次处理数据 <br>* 默认每个批次1000条数据* @param list* @param handler* @param <T>*/public static <T> void batchHandle(List<T> list, BatchHandle<T> handler) {batchHandle(list, BATCH_SIZE, handler);}/*** 分批次处理数据* @param list* @param handler* @param <T>* @return*/public static <T> void batchHandle(List<T> list, int maxSize, BatchHandle<T> handler) {if (CollUtil.isEmpty(list)) {return;}List<List<T>> splitList = CollUtil.split(list, maxSize);CompletableFuture<Void> cf = CompletableFuture.runAsync(() -> {for (List<T> tList : splitList) {handler.handle(tList);}}, ThreadUtil.newExecutor(4, 8));try {cf.get();} catch (Exception e) {log.error("批量处理数据异常", e);throw new RuntimeException(e);}cf.thenRun(() -> {log.info("批量处理完成");});}public interface BatchHandle<T> {void handle(List<T> list);}}
如何使用呢,再贴一段代码。
这里利用Java8的lambda机制,一行代码实现分批次录入数据
BatchDataUtils.batchHandle(hisReconciliationList, (list) -> hisReconciliationDao.saveAll(list));
