소스 검색

主键处理.

nieqiuqiu 5 년 전
부모
커밋
c7edec6981

+ 4 - 5
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/ConfigBuilder.java

@@ -398,7 +398,7 @@ public class ConfigBuilder {
             // 无父类开启 AR 模式
             tableInfo.getImportPackages().add(com.baomidou.mybatisplus.extension.activerecord.Model.class.getCanonicalName());
         }
-        if (null != globalConfig.getIdType() && tableInfo.hasPrimaryKey()) {
+        if (null != globalConfig.getIdType() && tableInfo.isHavePrimaryKey()) {
             // 指定需要 IdType 场景
             tableInfo.getImportPackages().add(com.baomidou.mybatisplus.annotation.IdType.class.getCanonicalName());
             tableInfo.getImportPackages().add(com.baomidou.mybatisplus.annotation.TableId.class.getCanonicalName());
@@ -636,11 +636,10 @@ public class ConfigBuilder {
 
                     // 处理ID
                     if (isId && !haveId) {
-                        field.setKeyFlag(true);
-                        if (DbType.H2 == dbType || DbType.SQLITE == dbType || dbQuery.isKeyIdentity(results)) {
-                            field.setKeyIdentityFlag(true);
-                        }
                         haveId = true;
+                        field.setKeyFlag(true);
+                        tableInfo.setHavePrimaryKey(true);
+                        field.setKeyIdentityFlag(dbQuery.isKeyIdentity(results));
                     } else {
                         field.setKeyFlag(false);
                     }

+ 1 - 9
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableInfo.java

@@ -49,6 +49,7 @@ public class TableInfo {
     private String serviceImplName;
     private String controllerName;
     private List<TableField> fields;
+    private boolean havePrimaryKey;
     /**
      * 公共字段
      */
@@ -158,13 +159,4 @@ public class TableInfo {
         return fieldNames;
     }
 
-    /**
-     * 是否含有主键
-     *
-     * @return 是否含有主键
-     * @since 3.3.3
-     */
-    public boolean hasPrimaryKey() {
-        return this.fields.stream().anyMatch(TableField::isKeyFlag);
-    }
 }