🐎 批量插入分批处理
This commit is contained in:
@@ -23,6 +23,11 @@ public interface ExtendService<T> {
|
||||
*/
|
||||
int DEFAULT_BATCH_SIZE = 1000;
|
||||
|
||||
/**
|
||||
* 默认一次批量插入的数量
|
||||
*/
|
||||
int DEFAULT_INSERT_BATCH_SIZE = 5000;
|
||||
|
||||
/**
|
||||
* 插入一条记录(选择字段,策略插入)
|
||||
* @param entity 实体对象
|
||||
@@ -118,6 +123,18 @@ public interface ExtendService<T> {
|
||||
* @return int 改动行
|
||||
* @author lingting 2020-08-26 22:11
|
||||
*/
|
||||
boolean saveBatchSomeColumn(Collection<T> list);
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
default boolean saveBatchSomeColumn(Collection<T> list) {
|
||||
return this.saveBatchSomeColumn(list, DEFAULT_INSERT_BATCH_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入数据
|
||||
* @param list 数据列表
|
||||
* @param batchSize 批次插入数据量
|
||||
* @return int 改动行
|
||||
* @author lingting 2020-08-26 22:11
|
||||
*/
|
||||
boolean saveBatchSomeColumn(Collection<T> list, int batchSize);
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
/**
|
||||
@@ -92,13 +93,24 @@ public class ExtendServiceImpl<M extends ExtendMapper<T>, T> implements ExtendSe
|
||||
// ^^^^^^ Copy From com.baomidou.mybatisplus.extension.service.impl.ServiceImpl end
|
||||
// ^^^^^^
|
||||
|
||||
/**
|
||||
* 批量插入数据
|
||||
* @param list 数据列表
|
||||
* @param batchSize 批次插入数据量
|
||||
* @return int 改动行
|
||||
* @author lingting 2020-08-26 22:11
|
||||
*/
|
||||
@Override
|
||||
public boolean saveBatchSomeColumn(Collection<T> list) {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean saveBatchSomeColumn(Collection<T> list, int batchSize) {
|
||||
if (CollectionUtil.isEmpty(list)) {
|
||||
return false;
|
||||
}
|
||||
int i = baseMapper.insertBatchSomeColumn(list);
|
||||
return SqlHelper.retBool(i);
|
||||
List<List<T>> segmentDataList = CollectionUtil.split(list, batchSize);
|
||||
for (List<T> data : segmentDataList) {
|
||||
baseMapper.insertBatchSomeColumn(data);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user