Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[功能改进]: 描述 mybatis-plus的伪批量插入 什么时候可以修复支持下呢 #6636

Open
3 tasks done
silent-night-no-trace opened this issue Dec 17, 2024 · 1 comment

Comments

@silent-night-no-trace
Copy link

确认

  • 我的版本是最新版本, 我的版本号与 version 相同, 并且项目里无依赖冲突
  • 我已经在 issue 中搜索过, 确认问题没有被提出过
  • 我已经修改标题, 将标题中的 描述 替换为遇到的问题

功能改进

@Transactional(rollbackFor = Exception.class)
@Override
public boolean saveBatch(Collection<T> entityList, int batchSize) {
      String sqlStatement = getSqlStatement(SqlMethod.INSERT_ONE);
      return executeBatch(entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));
}

public static <E> boolean executeBatch(Class<?> entityClass, Log log, Collection<E> list, 
                                                                                           int batchSize, BiConsumer<SqlSession, E> consumer) {
    Assert.isFalse(batchSize < 1, "batchSize must not be less than one");
    return !CollectionUtils.isEmpty(list) && executeBatch(entityClass, log, sqlSession -> {
        int size = list.size();
        int idxLimit = Math.min(batchSize, size);
        int i = 1;
        for (E element : list) {
            consumer.accept(sqlSession, element);
            if (i == idxLimit) {
                sqlSession.flushStatements();
                idxLimit = Math.min(idxLimit + batchSize, size);
            }
            i++;
        }
    });
}

粗浅的理解 批量插入 还是循环调用 sqlSession的 insert 当批量数目达到阈值 进行flush , 如果有理解不到位的,还请给解释下

参考资料

mybatis-plus 3.5.8 spring boot starter

@silent-night-no-trace
Copy link
Author

按照这个理解,粗浅的理解是实现了 伪批量插入 并未实现真实的批量插入

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant