首页  编辑  

Springboot + mybatis 批量插入数据

Tags: /Java/   Date Created:
使用Mybatis批量插入大量数据的实践 - hellodev - 博客园 (cnblogs.com)
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);
    }
}
其中Mapper的.addUserId类似下面:
@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);