分割list 批量插入数据指定条数数据

一、代码层面切割好list,然后插入

复制代码
// package org.apache.commons.collections4; 先将list切成1000条一份
List<List<DeptDO>> p1 = ListUtils.partition(deptList, 1000);
for (List<DeptDO> deptDOS : p1) {
// 1000条一次批量插入
      systemDeptMapper.insertBatch(deptDOS);
}

二、使用mybatisplus批量插入方法

复制代码
    /**
     * 插入(批量)
     *
     * @param entityList 实体对象集合
     */
    public static <T> boolean saveBatch(Collection<T> entityList) {
        return saveBatch(entityList, IService.DEFAULT_BATCH_SIZE);
    }

    /**
     * 插入(批量)
     *
     * @param entityList 实体对象集合
     * @param batchSize  插入批次数量
     */
    public static <T> boolean saveBatch(Collection<T> entityList, int batchSize) {
        if (CollectionUtils.isEmpty(entityList)) {
            return false;
        }
        Class<T> entityClass = getEntityClass(entityList);
        Class<?> mapperClass = ClassUtils.toClassConfident(getTableInfo(entityClass).getCurrentNamespace());
        String sqlStatement = SqlHelper.getSqlStatement(mapperClass, SqlMethod.INSERT_ONE);
        return SqlHelper.executeBatch(entityClass, LogFactory.getLog(Db.class), entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));
    }
相关推荐
极创信息4 分钟前
信创产品认证怎么做?信创产品测试认证的主要流程
java·大数据·数据库·金融·软件工程
SamDeepThinking23 分钟前
并发量就算只有2,该上锁还得上呀
java·后端·架构
Sam_Deep_Thinking39 分钟前
如何让订单系统和营销系统解耦
java·架构·系统架构
lzhdim1 小时前
SQL 入门 12:SQL 视图:创建、修改与可更新视图
java·大数据·服务器·数据库·sql
FQNmxDG4S2 小时前
Maven依赖管理:版本冲突解决与生命周期控制
java·数据库·maven
傻瓜搬砖人2 小时前
Spring集成Web环境
java·spring·maven
FQNmxDG4S2 小时前
Java泛型编程:类型擦除与泛型方法的应用场景
java·开发语言·python
GottdesKrieges3 小时前
OceanBase恢复常见问题
java·数据库·oceanbase
IGAn CTOU3 小时前
Java高级开发进阶教程之系列
java·开发语言
leo825...3 小时前
Claude Code Skills 清单(本地)
java·python·ai编程