确认
- [X] 我的版本是最新版本, 我的版本号与 version 相同, 并且项目里无依赖冲突
- [X] 我已经在 issue 中搜索过, 确认问题没有被提出过
- [X] 我已经修改标题, 将标题中的 描述 替换为遇到的问题
功能改进
@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
Comment From: silent-night-no-trace
按照这个理解,粗浅的理解是实现了 伪批量插入 并未实现真实的批量插入
Comment From: nieqiurong
jdbco批量就是那样,如果你要insert values()()那种就参考这 https://gitee.com/baomidou/mybatis-plus-samples/blob/master/mybatis-plus-sample-deluxe/README.md