Explorar o código

优化SqlHelper中executeBatch刷新条件的判断;

Zjp %!s(int64=3) %!d(string=hai) anos
pai
achega
ca89252fe1

+ 3 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlHelper.java

@@ -216,11 +216,13 @@ public final class SqlHelper {
         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 % batchSize == 0) || i == size) {
+                if (i == idxLimit) {
                     sqlSession.flushStatements();
+                    idxLimit = Math.min(idxLimit + batchSize, size);
                 }
                 i++;
             }