Jelajahi Sumber

fix github issues/3768

hubin 3 tahun lalu
induk
melakukan
f0d0f12d55

+ 1 - 1
build.gradle

@@ -2,7 +2,7 @@ import java.time.LocalDateTime
 
 allprojects {
     group = 'com.baomidou'
-    version = "3.4.4.3-SNAPSHOT"
+    version = "3.4.3.2"
 }
 
 ext {

+ 12 - 4
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfo.java

@@ -293,11 +293,15 @@ public class TableInfo implements Constants {
      *
      * @return sql 脚本片段
      */
-    public String getKeyInsertSqlProperty(final String prefix, final boolean newLine) {
+    public String getKeyInsertSqlProperty(final boolean batch, final String prefix, final boolean newLine) {
         final String newPrefix = prefix == null ? EMPTY : prefix;
         if (havePK()) {
             String keyColumn = SqlScriptUtils.safeParam(newPrefix + keyProperty) + COMMA;
             if (idType == IdType.AUTO) {
+                if (batch) {
+                    // 批量插入必须返回空自增情况下
+                    return EMPTY;
+                }
                 return SqlScriptUtils.convertIf(keyColumn, String.format("%s != null", keyProperty), newLine);
             }
             return keyColumn + (newLine ? NEWLINE : EMPTY);
@@ -312,9 +316,13 @@ public class TableInfo implements Constants {
      *
      * @return sql 脚本片段
      */
-    public String getKeyInsertSqlColumn(final boolean newLine) {
+    public String getKeyInsertSqlColumn(final boolean batch, final boolean newLine) {
         if (havePK()) {
             if (idType == IdType.AUTO) {
+                if (batch) {
+                    // 批量插入必须返回空自增情况下
+                    return EMPTY;
+                }
                 return SqlScriptUtils.convertIf(keyColumn + COMMA, String.format("%s != null", keyProperty), newLine);
             }
             return keyColumn + COMMA + (newLine ? NEWLINE : EMPTY);
@@ -333,7 +341,7 @@ public class TableInfo implements Constants {
      */
     public String getAllInsertSqlPropertyMaybeIf(final String prefix) {
         final String newPrefix = prefix == null ? EMPTY : prefix;
-        return getKeyInsertSqlProperty(newPrefix, true) + fieldList.stream()
+        return getKeyInsertSqlProperty(false, newPrefix, true) + fieldList.stream()
             .map(i -> i.getInsertSqlPropertyMaybeIf(newPrefix)).filter(Objects::nonNull).collect(joining(NEWLINE));
     }
 
@@ -348,7 +356,7 @@ public class TableInfo implements Constants {
      */
     public String getAllInsertSqlColumnMaybeIf(final String prefix) {
         final String newPrefix = prefix == null ? EMPTY : prefix;
-        return getKeyInsertSqlColumn(true) + fieldList.stream().map(i -> i.getInsertSqlColumnMaybeIf(newPrefix))
+        return getKeyInsertSqlColumn(false, true) + fieldList.stream().map(i -> i.getInsertSqlColumnMaybeIf(newPrefix))
             .filter(Objects::nonNull).collect(joining(NEWLINE));
     }
 

+ 2 - 2
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/InsertBatchSomeColumn.java

@@ -79,10 +79,10 @@ public class InsertBatchSomeColumn extends AbstractMethod {
         KeyGenerator keyGenerator = NoKeyGenerator.INSTANCE;
         SqlMethod sqlMethod = SqlMethod.INSERT_ONE;
         List<TableFieldInfo> fieldList = tableInfo.getFieldList();
-        String insertSqlColumn = tableInfo.getKeyInsertSqlColumn(false) +
+        String insertSqlColumn = tableInfo.getKeyInsertSqlColumn(true, false) +
             this.filterTableFieldInfo(fieldList, predicate, TableFieldInfo::getInsertSqlColumn, EMPTY);
         String columnScript = LEFT_BRACKET + insertSqlColumn.substring(0, insertSqlColumn.length() - 1) + RIGHT_BRACKET;
-        String insertSqlProperty = tableInfo.getKeyInsertSqlProperty(ENTITY_DOT, false) +
+        String insertSqlProperty = tableInfo.getKeyInsertSqlProperty(true, ENTITY_DOT, false) +
             this.filterTableFieldInfo(fieldList, predicate, i -> i.getInsertSqlProperty(ENTITY_DOT), EMPTY);
         insertSqlProperty = LEFT_BRACKET + insertSqlProperty.substring(0, insertSqlProperty.length() - 1) + RIGHT_BRACKET;
         String valuesScript = SqlScriptUtils.convertForeach(insertSqlProperty, "list", null, ENTITY, COMMA);

+ 2 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlRunner.java

@@ -192,7 +192,8 @@ public class SqlRunner implements ISqlRunner {
     public long selectCount(String sql, Object... args) {
         SqlSession sqlSession = sqlSession();
         try {
-            return SqlHelper.retCount(sqlSession.<Long>selectOne(COUNT, sqlMap(sql, args)));
+            Object count = sqlSession.selectOne(COUNT, sqlMap(sql, args));
+            return null == count ? 0 : Long.valueOf(String.valueOf(count));
         } finally {
             closeSqlSession(sqlSession);
         }