소스 검색

重构生成器 QuerySQL 可自定义

= 7 년 전
부모
커밋
5937b35665

+ 34 - 0
mybatis-plus-generate/src/main/java/com/baomidou/mybatisplus/generator/config/DataSourceConfig.java

@@ -24,6 +24,10 @@ import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
 import com.baomidou.mybatisplus.generator.config.converts.OracleTypeConvert;
 import com.baomidou.mybatisplus.generator.config.converts.PostgreSqlTypeConvert;
 import com.baomidou.mybatisplus.generator.config.converts.SqlServerTypeConvert;
+import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
+import com.baomidou.mybatisplus.generator.config.querys.OracleQuery;
+import com.baomidou.mybatisplus.generator.config.querys.PostgreSqlQuery;
+import com.baomidou.mybatisplus.generator.config.querys.SqlServerQuery;
 import com.baomidou.mybatisplus.generator.config.rules.DbType;
 
 /**
@@ -36,6 +40,10 @@ import com.baomidou.mybatisplus.generator.config.rules.DbType;
  */
 public class DataSourceConfig {
 
+    /**
+     * 数据库信息查询
+     */
+    private IDbQuery dbQuery;
     /**
      * 数据库类型
      */
@@ -65,6 +73,32 @@ public class DataSourceConfig {
      */
     private String password;
 
+    public IDbQuery getDbQuery() {
+        if (null == dbQuery) {
+            switch (getDbType()) {
+                case ORACLE:
+                    dbQuery = new OracleQuery();
+                    break;
+                case SQL_SERVER:
+                    dbQuery = new SqlServerQuery();
+                    break;
+                case POSTGRE_SQL:
+                    dbQuery = new PostgreSqlQuery();
+                    break;
+                default:
+                    // 默认 MYSQL
+                    dbQuery = new MySqlQuery();
+                    break;
+            }
+        }
+        return dbQuery;
+    }
+
+    public DataSourceConfig setDbQuery(IDbQuery dbQuery) {
+        this.dbQuery = dbQuery;
+        return this;
+    }
+
     /**
      * 判断数据库类型
      *

+ 98 - 0
mybatis-plus-generate/src/main/java/com/baomidou/mybatisplus/generator/config/IDbQuery.java

@@ -0,0 +1,98 @@
+/**
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.generator.config;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import com.baomidou.mybatisplus.generator.config.rules.DbType;
+
+/**
+ * <p>
+ * 表数据查询接口
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-01-16
+ */
+public interface IDbQuery {
+
+
+    /**
+     * 数据库类型
+     */
+    DbType dbType();
+
+
+    /**
+     * 表信息查询 SQL
+     */
+    String tablesSql();
+
+
+    /**
+     * 表字段信息查询 SQL
+     */
+    String tableFieldsSql();
+
+
+    /**
+     * 表名称
+     */
+    String tableName();
+
+
+    /**
+     * 表注释
+     */
+    String tableComment();
+
+
+    /**
+     * 字段名称
+     */
+    String fieldName();
+
+
+    /**
+     * 字段类型
+     */
+    String fieldType();
+
+
+    /**
+     * 字段注释
+     */
+    String fieldComment();
+
+
+    /**
+     * 主键字段
+     */
+    String fieldKey();
+
+
+    /**
+     * <p>
+     * 判断主键是否为identity,目前仅对mysql进行检查
+     * </p>
+     *
+     * @param results ResultSet
+     * @return 主键是否为identity
+     * @throws SQLException
+     */
+    boolean isKeyIdentity(ResultSet results) throws SQLException;
+}

+ 47 - 60
mybatis-plus-generate/src/main/java/com/baomidou/mybatisplus/generator/config/builder/ConfigBuilder.java

@@ -31,6 +31,7 @@ import com.baomidou.mybatisplus.generator.InjectionConfig;
 import com.baomidou.mybatisplus.generator.config.ConstVal;
 import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
 import com.baomidou.mybatisplus.generator.config.GlobalConfig;
+import com.baomidou.mybatisplus.generator.config.IDbQuery;
 import com.baomidou.mybatisplus.generator.config.PackageConfig;
 import com.baomidou.mybatisplus.generator.config.StrategyConfig;
 import com.baomidou.mybatisplus.generator.config.TemplateConfig;
@@ -39,7 +40,6 @@ import com.baomidou.mybatisplus.generator.config.po.TableFill;
 import com.baomidou.mybatisplus.generator.config.po.TableInfo;
 import com.baomidou.mybatisplus.generator.config.rules.DbType;
 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
-import com.baomidou.mybatisplus.generator.config.rules.QuerySQL;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
 
 /**
@@ -67,7 +67,7 @@ public class ConfigBuilder {
     /**
      * SQL语句类型
      */
-    private QuerySQL querySQL;
+    private IDbQuery dbQuery;
     private String superEntityClass;
     private String superMapperClass;
     /**
@@ -101,6 +101,7 @@ public class ConfigBuilder {
      */
     private InjectionConfig injectionConfig;
 
+
     /**
      * <p>
      * 在构造器中处理配置
@@ -156,6 +157,7 @@ public class ConfigBuilder {
         return packageInfo;
     }
 
+
     /**
      * <p>
      * 所有路径配置
@@ -167,14 +169,17 @@ public class ConfigBuilder {
         return pathInfo;
     }
 
+
     public String getSuperEntityClass() {
         return superEntityClass;
     }
 
+
     public String getSuperMapperClass() {
         return superMapperClass;
     }
 
+
     /**
      * <p>
      * 获取超类定义
@@ -186,14 +191,17 @@ public class ConfigBuilder {
         return superServiceClass;
     }
 
+
     public String getSuperServiceImplClass() {
         return superServiceImplClass;
     }
 
+
     public String getSuperControllerClass() {
         return superControllerClass;
     }
 
+
     /**
      * <p>
      * 表信息
@@ -210,6 +218,7 @@ public class ConfigBuilder {
         return this;
     }
 
+
     /**
      * <p>
      * 模板路径配置信息
@@ -264,6 +273,7 @@ public class ConfigBuilder {
         }
     }
 
+
     /**
      * <p>
      * 处理数据源配置
@@ -273,9 +283,10 @@ public class ConfigBuilder {
      */
     private void handlerDataSource(DataSourceConfig config) {
         connection = config.getConn();
-        querySQL = getQuerySQL(config.getDbType());
+        dbQuery = config.getDbQuery();
     }
 
+
     /**
      * <p>
      * 处理数据库表 加载数据库表、列、注释相关数据集
@@ -288,6 +299,7 @@ public class ConfigBuilder {
         tableInfoList = getTablesInfo(config);
     }
 
+
     /**
      * <p>
      * 处理superClassName,IdClassType,IdStrategy配置
@@ -315,6 +327,7 @@ public class ConfigBuilder {
         superControllerClass = config.getSuperControllerClass();
     }
 
+
     /**
      * <p>
      * 处理表对应的类名称
@@ -361,6 +374,7 @@ public class ConfigBuilder {
         return tableList;
     }
 
+
     /**
      * <p>
      * 检查是否有
@@ -407,6 +421,7 @@ public class ConfigBuilder {
         }
     }
 
+
     /**
      * <p>
      * 获取所有的数据库表信息
@@ -429,37 +444,37 @@ public class ConfigBuilder {
         Set<String> notExistTables = new HashSet<>();
         PreparedStatement preparedStatement = null;
         try {
-            String tableCommentsSql = querySQL.getTableCommentsSql();
-            if (QuerySQL.POSTGRE_SQL == querySQL) {
-                tableCommentsSql = String.format(tableCommentsSql, dataSourceConfig.getSchemaname());
+            String tablesSql = dbQuery.tablesSql();
+            if (DbType.POSTGRE_SQL == dbQuery.dbType()) {
+                tablesSql = String.format(tablesSql, dataSourceConfig.getSchemaname());
             }
             //oracle数据库表太多,出现最大游标错误
-            else if (QuerySQL.ORACLE == querySQL) {
+            else if (DbType.ORACLE == dbQuery.dbType()) {
                 if (isInclude) {
-                    StringBuilder sb = new StringBuilder(tableCommentsSql);
-                    sb.append(" WHERE ").append(querySQL.getTableName()).append(" IN (");
+                    StringBuilder sb = new StringBuilder(tablesSql);
+                    sb.append(" WHERE ").append(dbQuery.tableName()).append(" IN (");
                     for (String tbname : config.getInclude()) {
                         sb.append("'").append(tbname.toUpperCase()).append("',");
                     }
                     sb.replace(sb.length() - 1, sb.length(), ")");
-                    tableCommentsSql = sb.toString();
+                    tablesSql = sb.toString();
                 } else if (isExclude) {
-                    StringBuilder sb = new StringBuilder(tableCommentsSql);
-                    sb.append(" WHERE ").append(querySQL.getTableName()).append(" NOT IN (");
+                    StringBuilder sb = new StringBuilder(tablesSql);
+                    sb.append(" WHERE ").append(dbQuery.tableName()).append(" NOT IN (");
                     for (String tbname : config.getExclude()) {
                         sb.append("'").append(tbname.toUpperCase()).append("',");
                     }
                     sb.replace(sb.length() - 1, sb.length(), ")");
-                    tableCommentsSql = sb.toString();
+                    tablesSql = sb.toString();
                 }
             }
-            preparedStatement = connection.prepareStatement(tableCommentsSql);
+            preparedStatement = connection.prepareStatement(tablesSql);
             ResultSet results = preparedStatement.executeQuery();
             TableInfo tableInfo;
             while (results.next()) {
-                String tableName = results.getString(querySQL.getTableName());
+                String tableName = results.getString(dbQuery.tableName());
                 if (StringUtils.isNotEmpty(tableName)) {
-                    String tableComment = results.getString(querySQL.getTableComment());
+                    String tableComment = results.getString(dbQuery.tableComment());
                     if (config.isSkipView() && "VIEW".equals(tableComment)) {
                         // 跳过视图
                         continue;
@@ -531,28 +546,6 @@ public class ConfigBuilder {
     }
 
 
-    /**
-     * <p>
-     * 判断主键是否为identity,目前仅对mysql进行检查
-     * </p>
-     *
-     * @param results ResultSet
-     * @return 主键是否为identity
-     * @throws SQLException
-     */
-    private boolean isKeyIdentity(ResultSet results) throws SQLException {
-        if (QuerySQL.MYSQL == this.querySQL) {
-            String extra = results.getString("Extra");
-            if ("auto_increment".equals(extra)) {
-                return true;
-            }
-        } else if (QuerySQL.SQL_SERVER == this.querySQL) {
-            int isIdentity = results.getInt("isIdentity");
-            return 1 == isIdentity;
-        }
-        return false;
-    }
-
     /**
      * <p>
      * 将字段信息与表信息关联
@@ -567,8 +560,8 @@ public class ConfigBuilder {
         List<TableField> fieldList = new ArrayList<>();
         List<TableField> commonFieldList = new ArrayList<>();
         try {
-            String tableFieldsSql = querySQL.getTableFieldsSql();
-            if (QuerySQL.POSTGRE_SQL == querySQL) {
+            String tableFieldsSql = dbQuery.tableFieldsSql();
+            if (DbType.POSTGRE_SQL == dbQuery.dbType()) {
                 tableFieldsSql = String.format(tableFieldsSql, dataSourceConfig.getSchemaname(), tableInfo.getName());
             } else {
                 tableFieldsSql = String.format(tableFieldsSql, tableInfo.getName());
@@ -577,13 +570,13 @@ public class ConfigBuilder {
             ResultSet results = preparedStatement.executeQuery();
             while (results.next()) {
                 TableField field = new TableField();
-                String key = results.getString(querySQL.getFieldKey());
+                String key = results.getString(dbQuery.fieldKey());
                 // 避免多重主键设置,目前只取第一个找到ID,并放到list中的索引为0的位置
                 boolean isId = StringUtils.isNotEmpty(key) && key.toUpperCase().equals("PRI");
                 // 处理ID
                 if (isId && !haveId) {
                     field.setKeyFlag(true);
-                    if (isKeyIdentity(results)) {
+                    if (dbQuery.isKeyIdentity(results)) {
                         field.setKeyIdentityFlag(true);
                     }
                     haveId = true;
@@ -591,11 +584,11 @@ public class ConfigBuilder {
                     field.setKeyFlag(false);
                 }
                 // 处理其它信息
-                field.setName(results.getString(querySQL.getFieldName()));
-                field.setType(results.getString(querySQL.getFieldType()));
+                field.setName(results.getString(dbQuery.fieldName()));
+                field.setType(results.getString(dbQuery.fieldType()));
                 field.setPropertyName(strategyConfig, processName(field.getName(), strategy));
                 field.setColumnType(dataSourceConfig.getTypeConvert().processTypeConvert(field.getType()));
-                field.setComment(results.getString(querySQL.getFieldComment()));
+                field.setComment(results.getString(dbQuery.fieldComment()));
                 if (strategyConfig.includeSuperEntityColumns(field.getName())) {
                     // 跳过公共字段
                     commonFieldList.add(field);
@@ -621,6 +614,7 @@ public class ConfigBuilder {
         return tableInfo;
     }
 
+
     /**
      * <p>
      * 连接路径字符串
@@ -641,6 +635,7 @@ public class ConfigBuilder {
         return parentDir + packageName;
     }
 
+
     /**
      * <p>
      * 连接父子包名
@@ -657,6 +652,7 @@ public class ConfigBuilder {
         return parent + "." + subPackage;
     }
 
+
     /**
      * <p>
      * 处理字段名称
@@ -668,6 +664,7 @@ public class ConfigBuilder {
         return processName(name, strategy, this.strategyConfig.getFieldPrefix());
     }
 
+
     /**
      * <p>
      * 处理表/字段名称
@@ -702,44 +699,34 @@ public class ConfigBuilder {
         return propertyName;
     }
 
-    /**
-     * <p>
-     * 获取当前的SQL类型
-     * </p>
-     *
-     * @return DB类型
-     */
-    private QuerySQL getQuerySQL(DbType dbType) {
-        for (QuerySQL qs : QuerySQL.values()) {
-            if (qs.getDbType().equals(dbType.getValue())) {
-                return qs;
-            }
-        }
-        return QuerySQL.MYSQL;
-    }
 
     public StrategyConfig getStrategyConfig() {
         return strategyConfig;
     }
 
+
     public ConfigBuilder setStrategyConfig(StrategyConfig strategyConfig) {
         this.strategyConfig = strategyConfig;
         return this;
     }
 
+
     public GlobalConfig getGlobalConfig() {
         return globalConfig;
     }
 
+
     public ConfigBuilder setGlobalConfig(GlobalConfig globalConfig) {
         this.globalConfig = globalConfig;
         return this;
     }
 
+
     public InjectionConfig getInjectionConfig() {
         return injectionConfig;
     }
 
+
     public ConfigBuilder setInjectionConfig(InjectionConfig injectionConfig) {
         this.injectionConfig = injectionConfig;
         return this;

+ 38 - 0
mybatis-plus-generate/src/main/java/com/baomidou/mybatisplus/generator/config/querys/AbstractDbQuery.java

@@ -0,0 +1,38 @@
+/**
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.generator.config.querys;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import com.baomidou.mybatisplus.generator.config.IDbQuery;
+
+/**
+ * <p>
+ * 表数据查询抽象类
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-01-16
+ */
+public abstract class AbstractDbQuery implements IDbQuery {
+
+
+    @Override
+    public boolean isKeyIdentity(ResultSet results) throws SQLException {
+        return false;
+    }
+}

+ 93 - 0
mybatis-plus-generate/src/main/java/com/baomidou/mybatisplus/generator/config/querys/MySqlQuery.java

@@ -0,0 +1,93 @@
+/**
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.generator.config.querys;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import com.baomidou.mybatisplus.generator.config.IDbQuery;
+import com.baomidou.mybatisplus.generator.config.rules.DbType;
+
+/**
+ * <p>
+ * MySql 表数据查询
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-01-16
+ */
+public class MySqlQuery implements IDbQuery {
+
+
+    @Override
+    public DbType dbType() {
+        return DbType.MYSQL;
+    }
+
+
+    @Override
+    public String tablesSql() {
+        return "show table status";
+    }
+
+
+    @Override
+    public String tableFieldsSql() {
+        return "show full fields from `%s`";
+    }
+
+
+    @Override
+    public String tableName() {
+        return "NAME";
+    }
+
+
+    @Override
+    public String tableComment() {
+        return "COMMENT";
+    }
+
+
+    @Override
+    public String fieldName() {
+        return "FIELD";
+    }
+
+
+    @Override
+    public String fieldType() {
+        return "TYPE";
+    }
+
+
+    @Override
+    public String fieldComment() {
+        return "COMMENT";
+    }
+
+
+    @Override
+    public String fieldKey() {
+        return "KEY";
+    }
+
+
+    @Override
+    public boolean isKeyIdentity(ResultSet results) throws SQLException {
+        return "auto_increment".equals(results.getString("Extra"));
+    }
+}

+ 93 - 0
mybatis-plus-generate/src/main/java/com/baomidou/mybatisplus/generator/config/querys/OracleQuery.java

@@ -0,0 +1,93 @@
+/**
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.generator.config.querys;
+
+import com.baomidou.mybatisplus.generator.config.rules.DbType;
+
+/**
+ * <p>
+ * Oracle 表数据查询
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-01-16
+ */
+public class OracleQuery extends AbstractDbQuery {
+
+
+    @Override
+    public DbType dbType() {
+        return DbType.ORACLE;
+    }
+
+
+    @Override
+    public String tablesSql() {
+        return "SELECT * FROM USER_TAB_COMMENTS";
+    }
+
+
+    @Override
+    public String tableFieldsSql() {
+        return "SELECT A.COLUMN_NAME, CASE WHEN A.DATA_TYPE='NUMBER' THEN "
+            + "(CASE WHEN A.DATA_PRECISION IS NULL THEN A.DATA_TYPE "
+            + "WHEN NVL(A.DATA_SCALE, 0) > 0 THEN A.DATA_TYPE||'('||A.DATA_PRECISION||','||A.DATA_SCALE||')' "
+            + "ELSE A.DATA_TYPE||'('||A.DATA_PRECISION||')' END) "
+            + "ELSE A.DATA_TYPE END DATA_TYPE, B.COMMENTS,DECODE(C.POSITION, '1', 'PRI') KEY "
+            + "FROM USER_TAB_COLUMNS A INNER JOIN USER_COL_COMMENTS B ON A.TABLE_NAME = B.TABLE_NAME"
+            + " AND A.COLUMN_NAME = B.COLUMN_NAME LEFT JOIN USER_CONSTRAINTS D "
+            + "ON D.TABLE_NAME = A.TABLE_NAME AND D.CONSTRAINT_TYPE = 'P' "
+            + "LEFT JOIN USER_CONS_COLUMNS C ON C.CONSTRAINT_NAME = D.CONSTRAINT_NAME "
+            + "AND C.COLUMN_NAME=A.COLUMN_NAME WHERE A.TABLE_NAME = '%s' ORDER BY A.COLUMN_ID ";
+    }
+
+
+    @Override
+    public String tableName() {
+        return "TABLE_NAME";
+    }
+
+
+    @Override
+    public String tableComment() {
+        return "COMMENTS";
+    }
+
+
+    @Override
+    public String fieldName() {
+        return "COLUMN_NAME";
+    }
+
+
+    @Override
+    public String fieldType() {
+        return "DATA_TYPE";
+    }
+
+
+    @Override
+    public String fieldComment() {
+        return "COMMENTS";
+    }
+
+
+    @Override
+    public String fieldKey() {
+        return "KEY";
+    }
+
+}

+ 85 - 0
mybatis-plus-generate/src/main/java/com/baomidou/mybatisplus/generator/config/querys/PostgreSqlQuery.java

@@ -0,0 +1,85 @@
+/**
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.generator.config.querys;
+
+import com.baomidou.mybatisplus.generator.config.rules.DbType;
+
+/**
+ * <p>
+ * PostgreSql 表数据查询
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-01-16
+ */
+public class PostgreSqlQuery extends AbstractDbQuery {
+
+
+    @Override
+    public DbType dbType() {
+        return DbType.POSTGRE_SQL;
+    }
+
+
+    @Override
+    public String tablesSql() {
+        return "SELECT A.tablename, obj_description(relfilenode, 'pg_class') AS comments FROM pg_tables A, pg_class B WHERE A.schemaname='%s' AND A.tablename = B.relname";
+    }
+
+
+    @Override
+    public String tableFieldsSql() {
+        return "SELECT A.attname AS name, format_type(A.atttypid, A.atttypmod) AS type,col_description(A.attrelid, A.attnum) AS comment, (CASE C.contype WHEN 'p' THEN 'PRI' ELSE '' END) AS key " +
+            "FROM pg_attribute A LEFT JOIN pg_constraint C ON A.attnum = C.conkey[1] AND A.attrelid = C.conrelid " +
+            "WHERE  A.attrelid = '%s.%s'::regclass AND A.attnum > 0 AND NOT A.attisdropped ORDER  BY A.attnum";
+    }
+
+
+    @Override
+    public String tableName() {
+        return "tablename";
+    }
+
+
+    @Override
+    public String tableComment() {
+        return "comments";
+    }
+
+
+    @Override
+    public String fieldName() {
+        return "name";
+    }
+
+
+    @Override
+    public String fieldType() {
+        return "type";
+    }
+
+
+    @Override
+    public String fieldComment() {
+        return "comment";
+    }
+
+
+    @Override
+    public String fieldKey() {
+        return "key";
+    }
+}

+ 109 - 0
mybatis-plus-generate/src/main/java/com/baomidou/mybatisplus/generator/config/querys/SqlServerQuery.java

@@ -0,0 +1,109 @@
+/**
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.generator.config.querys;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import com.baomidou.mybatisplus.generator.config.IDbQuery;
+import com.baomidou.mybatisplus.generator.config.rules.DbType;
+
+/**
+ * <p>
+ * SqlServer 表数据查询
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-01-16
+ */
+public class SqlServerQuery implements IDbQuery {
+
+
+    @Override
+    public DbType dbType() {
+        return DbType.SQL_SERVER;
+    }
+
+
+    @Override
+    public String tablesSql() {
+        return "select cast(so.name as varchar(500)) as TABLE_NAME, " +
+            "cast(sep.value as varchar(500)) as COMMENTS from sysobjects so " +
+            "left JOIN sys.extended_properties sep on sep.major_id=so.id and sep.minor_id=0 " +
+            "where (xtype='U' or xtype='v')";
+    }
+
+
+    @Override
+    public String tableFieldsSql() {
+        return "SELECT  cast(a.NAME AS VARCHAR(500)) AS TABLE_NAME,cast(b.NAME AS VARCHAR(500)) AS COLUMN_NAME, "
+            + "cast(c.VALUE AS VARCHAR(500)) AS COMMENTS,cast(sys.types.NAME AS VARCHAR (500)) AS DATA_TYPE,"
+            + "(" + " SELECT CASE count(1) WHEN 1 then 'PRI' ELSE '' END"
+            + " FROM syscolumns,sysobjects,sysindexes,sysindexkeys,systypes "
+            + " WHERE syscolumns.xusertype = systypes.xusertype AND syscolumns.id = object_id (A.NAME) AND sysobjects.xtype = 'PK'"
+            + " AND sysobjects.parent_obj = syscolumns.id " + " AND sysindexes.id = syscolumns.id "
+            + " AND sysobjects.NAME = sysindexes.NAME AND sysindexkeys.id = syscolumns.id "
+            + " AND sysindexkeys.indid = sysindexes.indid "
+            + " AND syscolumns.colid = sysindexkeys.colid AND syscolumns.NAME = B.NAME) as 'KEY',"
+            + "  b.is_identity isIdentity "
+            + " FROM ( select name,object_id from sys.tables UNION all select name,object_id from sys.views ) a "
+            + " INNER JOIN sys.COLUMNS b ON b.object_id = a.object_id "
+            + " LEFT JOIN sys.types ON b.user_type_id = sys.types.user_type_id   "
+            + " LEFT JOIN sys.extended_properties c ON c.major_id = b.object_id AND c.minor_id = b.column_id "
+            + " WHERE a.NAME = '%s' and sys.types.NAME !='sysname' ";
+    }
+
+    @Override
+    public String tableName() {
+        return "TABLE_NAME";
+    }
+
+
+    @Override
+    public String tableComment() {
+        return "COMMENTS";
+    }
+
+
+    @Override
+    public String fieldName() {
+        return "COLUMN_NAME";
+    }
+
+
+    @Override
+    public String fieldType() {
+        return "DATA_TYPE";
+    }
+
+
+    @Override
+    public String fieldComment() {
+        return "COMMENTS";
+    }
+
+
+    @Override
+    public String fieldKey() {
+        return "KEY";
+    }
+
+
+    @Override
+    public boolean isKeyIdentity(ResultSet results) throws SQLException {
+        return 1 == results.getInt("isIdentity");
+    }
+}

+ 4 - 4
mybatis-plus-generate/src/main/java/com/baomidou/mybatisplus/generator/config/rules/DbColumnType.java

@@ -46,10 +46,10 @@ public enum DbColumnType {
     CHARACTER("Character", null),
     OBJECT("Object", null),
     DATE("Date", "java.util.Date"),
-    TIME("Time", "java.sql.Time"),
-    BLOB("Blob", "java.sql.Blob"),
-    CLOB("Clob", "java.sql.Clob"),
-    TIMESTAMP("Timestamp", "java.sql.Timestamp"),
+    TIME("Time", "java.querys.Time"),
+    BLOB("Blob", "java.querys.Blob"),
+    CLOB("Clob", "java.querys.Clob"),
+    TIMESTAMP("Timestamp", "java.querys.Timestamp"),
     BIG_INTEGER("BigInteger", "java.math.BigInteger"),
     BIG_DECIMAL("BigDecimal", "java.math.BigDecimal"),
     LOCAL_DATE("LocalDate", "java.time.LocalDate"),

+ 7 - 1
mybatis-plus-generate/src/main/java/com/baomidou/mybatisplus/generator/config/rules/DbType.java

@@ -22,15 +22,21 @@ package com.baomidou.mybatisplus.generator.config.rules;
  * @since 2016/8/30
  */
 public enum DbType {
+    MYSQL("mysql"),
+    ORACLE("oracle"),
+    SQL_SERVER("sql_server"),
+    POSTGRE_SQL("postgre_sql"),
+    OTHER("other db");
 
-    MYSQL("mysql"), ORACLE("oracle"), SQL_SERVER("sql_server"), POSTGRE_SQL("postgre_sql");
 
     private final String value;
 
+
     DbType(String value) {
         this.value = value;
     }
 
+
     public String getValue() {
         return value;
     }

+ 0 - 141
mybatis-plus-generate/src/main/java/com/baomidou/mybatisplus/generator/config/rules/QuerySQL.java

@@ -1,141 +0,0 @@
-/**
- * Copyright (c) 2011-2020, hubin (jobob@qq.com).
- * <p>
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- * <p>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-package com.baomidou.mybatisplus.generator.config.rules;
-
-/**
- * <p>
- * 表数据查询
- * </p>
- *
- * @author hubin, tangguo
- * @since 2016-04-25
- */
-public enum QuerySQL {
-    MYSQL("mysql", "show tables", "show table status", "show full fields from `%s`", "NAME", "COMMENT", "FIELD", "TYPE",
-            "COMMENT", "KEY"),
-
-    ORACLE("oracle", "SELECT * FROM USER_TABLES", "SELECT * FROM USER_TAB_COMMENTS",
-            "SELECT A.COLUMN_NAME, CASE WHEN A.DATA_TYPE='NUMBER' THEN "
-                    + "(CASE WHEN A.DATA_PRECISION IS NULL THEN A.DATA_TYPE "
-                    + "WHEN NVL(A.DATA_SCALE, 0) > 0 THEN A.DATA_TYPE||'('||A.DATA_PRECISION||','||A.DATA_SCALE||')' "
-                    + "ELSE A.DATA_TYPE||'('||A.DATA_PRECISION||')' END) "
-                    + "ELSE A.DATA_TYPE END DATA_TYPE, B.COMMENTS,DECODE(C.POSITION, '1', 'PRI') KEY "
-                    + "FROM USER_TAB_COLUMNS A INNER JOIN USER_COL_COMMENTS B ON A.TABLE_NAME = B.TABLE_NAME"
-                    + " AND A.COLUMN_NAME = B.COLUMN_NAME LEFT JOIN USER_CONSTRAINTS D "
-                    + "ON D.TABLE_NAME = A.TABLE_NAME AND D.CONSTRAINT_TYPE = 'P' "
-                    + "LEFT JOIN USER_CONS_COLUMNS C ON C.CONSTRAINT_NAME = D.CONSTRAINT_NAME "
-                    + "AND C.COLUMN_NAME=A.COLUMN_NAME WHERE A.TABLE_NAME = '%s' ORDER BY A.COLUMN_ID ",
-            "TABLE_NAME", "COMMENTS", "COLUMN_NAME", "DATA_TYPE", "COMMENTS", "KEY"),
-
-	SQL_SERVER("sql_server",
-			"select cast(name as varchar(500)) as TABLE_NAME from sysObjects where (xtype='U' or xtype='v') order by name",
-			"select " +
-					"cast(so.name as varchar(500)) as TABLE_NAME, " +
-					"cast(sep.value as varchar(500)) as COMMENTS " +
-					"from sysobjects so " +
-					"left JOIN sys.extended_properties sep on sep.major_id=so.id and sep.minor_id=0 " +
-					"where (xtype='U' or xtype='v')",
-			"SELECT  cast(a.NAME AS VARCHAR(500)) AS TABLE_NAME,cast(b.NAME AS VARCHAR(500)) AS COLUMN_NAME, "
-					+ "cast(c.VALUE AS VARCHAR(500)) AS COMMENTS,cast(sys.types.NAME AS VARCHAR (500)) AS DATA_TYPE,"
-					+ "(" + " SELECT CASE count(1) WHEN 1 then 'PRI' ELSE '' END"
-					+ " FROM syscolumns,sysobjects,sysindexes,sysindexkeys,systypes "
-					+ " WHERE syscolumns.xusertype = systypes.xusertype AND syscolumns.id = object_id (A.NAME) AND sysobjects.xtype = 'PK'"
-					+ " AND sysobjects.parent_obj = syscolumns.id " + " AND sysindexes.id = syscolumns.id "
-					+ " AND sysobjects.NAME = sysindexes.NAME AND sysindexkeys.id = syscolumns.id "
-					+ " AND sysindexkeys.indid = sysindexes.indid "
-					+ " AND syscolumns.colid = sysindexkeys.colid AND syscolumns.NAME = B.NAME) as 'KEY',"
-					+ "  b.is_identity isIdentity "
-					+ " FROM ( select name,object_id from sys.tables UNION all select name,object_id from sys.views ) a "
-					+ " INNER JOIN sys.COLUMNS b ON b.object_id = a.object_id "
-					+ " LEFT JOIN sys.types ON b.user_type_id = sys.types.user_type_id   "
-					+ " LEFT JOIN sys.extended_properties c ON c.major_id = b.object_id AND c.minor_id = b.column_id "
-					+ " WHERE a.NAME = '%s' and sys.types.NAME !='sysname' ",
-			"TABLE_NAME", "COMMENTS", "COLUMN_NAME", "DATA_TYPE", "COMMENTS", "KEY"),
-
-    POSTGRE_SQL("postgre_sql", "select tablename from pg_tables where schemaname='%s' ORDER BY tablename",
-            "SELECT A.tablename, obj_description(relfilenode, 'pg_class') AS comments FROM pg_tables A, pg_class B WHERE A.schemaname='%s' AND A.tablename = B.relname",
-            "SELECT A.attname AS name, format_type(A.atttypid, A.atttypmod) AS type,col_description(A.attrelid, A.attnum) AS comment, (CASE C.contype WHEN 'p' THEN 'PRI' ELSE '' END) AS key " +
-                    "FROM pg_attribute A LEFT JOIN pg_constraint C ON A.attnum = C.conkey[1] AND A.attrelid = C.conrelid " +
-                    "WHERE  A.attrelid = '%s.%s'::regclass AND A.attnum > 0 AND NOT A.attisdropped ORDER  BY A.attnum",
-            "tablename", "comments", "name", "type", "comment", "key");
-
-    private final String dbType;
-    private final String tablesSql;
-    private final String tableCommentsSql;
-    private final String tableFieldsSql;
-    private final String tableName;
-    private final String tableComment;
-    private final String fieldName;
-    private final String fieldType;
-    private final String fieldComment;
-    private final String fieldKey;
-
-    QuerySQL(final String dbType, final String tablesSql, final String tableCommentsSql, final String tableFieldsSql,
-             final String tableName, final String tableComment, final String fieldName, final String fieldType,
-             final String fieldComment, final String fieldKey) {
-        this.dbType = dbType;
-        this.tablesSql = tablesSql;
-        this.tableCommentsSql = tableCommentsSql;
-        this.tableFieldsSql = tableFieldsSql;
-        this.tableName = tableName;
-        this.tableComment = tableComment;
-        this.fieldName = fieldName;
-        this.fieldType = fieldType;
-        this.fieldComment = fieldComment;
-        this.fieldKey = fieldKey;
-    }
-
-    public String getDbType() {
-        return dbType;
-    }
-
-    public String getTablesSql() {
-        return tablesSql;
-    }
-
-    public String getTableCommentsSql() {
-        return tableCommentsSql;
-    }
-
-    public String getTableFieldsSql() {
-        return tableFieldsSql;
-    }
-
-    public String getTableName() {
-        return tableName;
-    }
-
-    public String getTableComment() {
-        return tableComment;
-    }
-
-    public String getFieldName() {
-        return fieldName;
-    }
-
-    public String getFieldType() {
-        return fieldType;
-    }
-
-    public String getFieldComment() {
-        return fieldComment;
-    }
-
-    public String getFieldKey() {
-        return fieldKey;
-    }
-
-}