public <T> void batchSplitInsert(List<T> list, Consumer insertFunc) {
List<List<T>> all = new ArrayList<>();
List<T> subList = new ArrayList<>();
if (list.size() > maxInsertItemNumPerTime) {
int i = 0;
while (i < list.size()) {
if (i + maxInsertItemNumPerTime > list.size()){
subList = list.subList(i, list.size());
} else {
subList = list.subList(i, i + maxInsertItemNumPerTime);
}
i = i + maxInsertItemNumPerTime;
all.add(subList);
}
all.parallelStream().forEach(insertFunc);
} else {
insertFunc.accept(list);
}
}
@Insert({"<script>", " INSERT INTO epr_fe_distinct_user_id(func_id, day_id, user_id, statistic_date, count) VALUES", " <foreach item='item' index='index' collection='list' separator=','>", " (#{item.functionId}, #{item.dayId}, #{item.userId}, #{item.statisticDate}, #{item.count})", " </foreach>", " ON DUPLICATE KEY UPDATE count = VALUES(count);", "</script>",}) long addUserId(List<ClapStatisticUser> list);
这样子我在做不同表的插入时:
// 待插入数据链表
List<TableDTO> needToInsert = ……;
// 进行新增
Consumer<List<TableDTO>> consumer = o -> mapper.addUserId(o);
batchSplitInsert(needToInsert, consumer);