Browse Source

开放类型名称字段获取.

lanjerry 1 year ago
parent
commit
fe321ca973

+ 15 - 2
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableField.java

@@ -399,6 +399,13 @@ public class TableField {
          */
         private JdbcType jdbcType;
 
+        /**
+         * 类型名称(可用做额外判断处理,例如在pg下,json,uuid,jsonb,tsquery这种都认为是OHTER 1111)
+         *
+         * @since 3.5.3
+         */
+        private String typeName;
+
         public MetaInfo(DatabaseMetaDataWrapper.Column column, TableInfo tableInfo) {
             if (column != null) {
                 this.tableName = tableInfo.getName();
@@ -409,6 +416,7 @@ public class TableField {
                 this.defaultValue = column.getDefaultValue();
                 this.scale = column.getScale();
                 this.jdbcType = column.getJdbcType();
+                this.typeName = column.getTypeName();
             }
         }
 
@@ -436,17 +444,22 @@ public class TableField {
             return jdbcType;
         }
 
+        public String getTypeName() {
+            return typeName;
+        }
+
         @Override
         public String toString() {
             return "MetaInfo{" +
-                "tableName=" + tableName +
-                ", columnName=" + columnName +
+                "tableName='" + tableName + '\'' +
+                ", columnName='" + columnName + '\'' +
                 ", length=" + length +
                 ", nullable=" + nullable +
                 ", remarks='" + remarks + '\'' +
                 ", defaultValue='" + defaultValue + '\'' +
                 ", scale=" + scale +
                 ", jdbcType=" + jdbcType +
+                ", typeName='" + typeName + '\'' +
                 '}';
         }
     }

+ 11 - 1
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/DatabaseMetaDataWrapper.java

@@ -37,7 +37,7 @@ public class DatabaseMetaDataWrapper {
 
     private static final Logger logger = LoggerFactory.getLogger(DatabaseMetaDataWrapper.class);
 
-    private Connection connection;
+    private final Connection connection;
 
     private final DatabaseMetaData databaseMetaData;
 
@@ -106,6 +106,7 @@ public class DatabaseMetaDataWrapper {
                 String name = resultSet.getString("COLUMN_NAME");
                 column.name = name;
                 column.primaryKey = primaryKeys.contains(name);
+                column.typeName = resultSet.getString("TYPE_NAME");
                 column.jdbcType = JdbcType.forCode(resultSet.getInt("DATA_TYPE"));
                 column.length = resultSet.getInt("COLUMN_SIZE");
                 column.scale = resultSet.getInt("DECIMAL_DIGITS");
@@ -216,6 +217,8 @@ public class DatabaseMetaDataWrapper {
 
         private JdbcType jdbcType;
 
+        private String typeName;
+
         public String getName() {
             return name;
         }
@@ -252,5 +255,12 @@ public class DatabaseMetaDataWrapper {
             return autoIncrement;
         }
 
+        public String getTypeName() {
+            return typeName;
+        }
+
+        public void setTypeName(String typeName) {
+            this.typeName = typeName;
+        }
     }
 }

+ 6 - 2
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/type/TypeRegistry.java

@@ -77,7 +77,7 @@ public class TypeRegistry {
         //TODO 类型需要补充完整
     }
 
-    public IColumnType getColumnType(TableField.MetaInfo metaInfo) {
+    public IColumnType getColumnType(TableField.MetaInfo metaInfo, DbColumnType defaultType) {
         //TODO 是否用包装类??? 可以尝试判断字段是否允许为null来判断是否用包装类
         int typeCode = metaInfo.getJdbcType().TYPE_CODE;
         switch (typeCode) {
@@ -94,10 +94,14 @@ public class TypeRegistry {
             case Types.TIMESTAMP:
                 return getTimestampType(metaInfo);
             default:
-                return typeMap.getOrDefault(typeCode, DbColumnType.OBJECT);
+                return typeMap.getOrDefault(typeCode, defaultType);
         }
     }
 
+    public IColumnType getColumnType(TableField.MetaInfo metaInfo) {
+        return getColumnType(metaInfo, DbColumnType.OBJECT);
+    }
+
     private IColumnType getBitType(TableField.MetaInfo metaInfo) {
         if (metaInfo.getLength() > 1) {
             return DbColumnType.BYTE_ARRAY;