优化批量插入

This commit is contained in:
zk
2026-03-10 20:29:53 +08:00
parent 49ba3929b2
commit cea131c5e5
2 changed files with 72 additions and 34 deletions
@@ -40,12 +40,6 @@ app:
#登陆配置 #登陆配置
login: login:
# 登陆图片验证相关
login_image_verification:
enable: false
images_code_sources: 123456789ABCDEFGHJKLMNPQRSTUVWXYZ
images_w: 111
images_h: 36
# token配置 # token配置
token: token:
exceed_time: 129600 exceed_time: 129600
@@ -6,64 +6,108 @@ import org.springframework.util.CollectionUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* 通用Mapper接口,提供批量操作功能
*
* @author zk * @author zk
*/ */
public interface CommonMapper<T> extends BaseMapper<T> { public interface CommonMapper<T> extends BaseMapper<T> {
/**
* 默认批量操作大小
*/
int DEFAULT_BATCH_SIZE = 5000; int DEFAULT_BATCH_SIZE = 5000;
/**
* 批量插入部分字段(需在XML中实现)
*
* @param entityList 实体列表
* @return 插入成功的记录数
*/
int insertBatchSomeColumn(Collection<T> entityList); int insertBatchSomeColumn(Collection<T> entityList);
/**
* 批量更新(需在XML中实现)
*
* @param entityList 实体列表
* @return 更新成功的记录数
*/
int updateBatchMethod(Collection<T> entityList); int updateBatchMethod(Collection<T> entityList);
/**
* 批量插入(使用默认批次大小)
*
* @param entityList 实体列表
* @return 插入成功的记录数
*/
default int batchInsert(List<T> entityList) { default int batchInsert(List<T> entityList) {
return this.batchInsert(entityList, DEFAULT_BATCH_SIZE); return this.batchInsert(entityList, DEFAULT_BATCH_SIZE);
} }
/**
* 批量插入(自定义批次大小)
*
* @param entityList 实体列表
* @param batchSize 每批次大小
* @return 插入成功的记录数
*/
default int batchInsert(List<T> entityList, int batchSize) { default int batchInsert(List<T> entityList, int batchSize) {
if (CollectionUtils.isEmpty(entityList)) { if (CollectionUtils.isEmpty(entityList)) {
return 0; return 0;
} else {
if (batchSize <= 0) {
batchSize = 5000;
}
List<List<T>> partition = partition(entityList, batchSize);
AtomicInteger total = new AtomicInteger();
partition.forEach((e) -> {
total.addAndGet(this.insertBatchSomeColumn(e));
});
return total.get();
} }
if (batchSize <= 0) {
batchSize = DEFAULT_BATCH_SIZE;
}
List<List<T>> partition = partition(entityList, batchSize);
int total = 0;
for (List<T> batch : partition) {
total += this.insertBatchSomeColumn(batch);
}
return total;
} }
/**
* 批量更新(使用默认批次大小)
*
* @param entityList 实体列表
* @return 更新成功的记录数
*/
default int batchUpdate(List<T> entityList) { default int batchUpdate(List<T> entityList) {
return this.batchUpdate(entityList, 5000); return this.batchUpdate(entityList, DEFAULT_BATCH_SIZE);
} }
/**
* 批量更新(自定义批次大小)
*
* @param entityList 实体列表
* @param batchSize 每批次大小
* @return 更新成功的记录数
*/
default int batchUpdate(List<T> entityList, int batchSize) { default int batchUpdate(List<T> entityList, int batchSize) {
if (CollectionUtils.isEmpty(entityList)) { if (CollectionUtils.isEmpty(entityList)) {
return 0; return 0;
} else {
if (batchSize <= 0) {
batchSize = 5000;
}
List<List<T>> partition = this.partition(entityList, batchSize);
AtomicInteger total = new AtomicInteger();
partition.forEach((e) -> {
total.addAndGet(updateBatchMethod(e));
});
return total.get();
} }
if (batchSize <= 0) {
batchSize = DEFAULT_BATCH_SIZE;
}
List<List<T>> partition = partition(entityList, batchSize);
int total = 0;
for (List<T> batch : partition) {
total += updateBatchMethod(batch);
}
return total;
} }
/**
default List<List<T>> partition(List<T> entityList, Integer batchSize) { * 将列表分批
*
* @param entityList 实体列表
* @param batchSize 每批次大小
* @return 分批后的列表
*/
default List<List<T>> partition(List<T> entityList, int batchSize) {
List<List<T>> partition = new ArrayList<>(); List<List<T>> partition = new ArrayList<>();
for (int i = 0; i < entityList.size(); i += batchSize) { for (int i = 0; i < entityList.size(); i += batchSize) {