瀏覽代碼

schema初始化顺序.

nieqiurong 4 年之前
父節點
當前提交
00eb2efebf

+ 26 - 2
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/DataSourceConfig.java

@@ -17,6 +17,7 @@ package com.baomidou.mybatisplus.generator.config;
 
 
 import com.baomidou.mybatisplus.annotation.DbType;
 import com.baomidou.mybatisplus.annotation.DbType;
 import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
 import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
 import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
 import com.baomidou.mybatisplus.generator.config.converts.TypeConverts;
 import com.baomidou.mybatisplus.generator.config.converts.TypeConverts;
 import com.baomidou.mybatisplus.generator.config.querys.DbQueryRegistry;
 import com.baomidou.mybatisplus.generator.config.querys.DbQueryRegistry;
@@ -169,11 +170,34 @@ public class DataSourceConfig {
     public Connection getConn() {
     public Connection getConn() {
         Connection conn;
         Connection conn;
         try {
         try {
-            Class.forName(driverName);
-            conn = DriverManager.getConnection(url, username, password);
+            Class.forName(this.driverName);
+            conn = DriverManager.getConnection(this.url, this.username, this.password);
+            String schema = StringUtils.isNotBlank(this.schemaName) ? this.schemaName : getDefaultSchema(getDbType());
+            if (StringUtils.isNotBlank(schema)) {
+                this.schemaName = schema;
+                conn.setSchema(schema);
+            }
         } catch (ClassNotFoundException | SQLException e) {
         } catch (ClassNotFoundException | SQLException e) {
             throw new RuntimeException(e);
             throw new RuntimeException(e);
         }
         }
         return conn;
         return conn;
     }
     }
+
+    private String getDefaultSchema(DbType dbType){
+        String schema = null;
+        if (DbType.POSTGRE_SQL == dbType) {
+            //pg 默认 schema=public
+            schema = "public";
+        } else if (DbType.KINGBASE_ES == dbType) {
+            //kingbase 默认 schema=PUBLIC
+            schema = "PUBLIC";
+        } else if (DbType.DB2 == dbType) {
+            //db2 默认 schema=current schema
+            schema = "current schema";
+        } else if (DbType.ORACLE == dbType) {
+            //oracle 默认 schema=username
+            schema = this.username.toUpperCase();
+        }
+        return schema;
+    }
 }
 }

+ 6 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/StrategyConfig.java

@@ -246,6 +246,7 @@ public class StrategyConfig {
      */
      */
     @Deprecated
     @Deprecated
     public StrategyConfig setTablePrefix(String... tablePrefix) {
     public StrategyConfig setTablePrefix(String... tablePrefix) {
+        this.tablePrefix.clear();   //保持语义
         return addTablePrefix(tablePrefix);
         return addTablePrefix(tablePrefix);
     }
     }
 
 
@@ -292,6 +293,7 @@ public class StrategyConfig {
      */
      */
     @Deprecated
     @Deprecated
     public StrategyConfig setSuperEntityColumns(String... superEntityColumns) {
     public StrategyConfig setSuperEntityColumns(String... superEntityColumns) {
+        this.superEntityColumns.clear();    //保持语义
         return addSuperEntityColumns(superEntityColumns);
         return addSuperEntityColumns(superEntityColumns);
     }
     }
 
 
@@ -315,6 +317,7 @@ public class StrategyConfig {
      */
      */
     @Deprecated
     @Deprecated
     public StrategyConfig setInclude(String... include) {
     public StrategyConfig setInclude(String... include) {
+        this.include.clear();   //保持语义
         return addInclude(include);
         return addInclude(include);
     }
     }
 
 
@@ -338,6 +341,7 @@ public class StrategyConfig {
      */
      */
     @Deprecated
     @Deprecated
     public StrategyConfig setExclude(String... exclude) {
     public StrategyConfig setExclude(String... exclude) {
+        this.exclude.clear();   //保持语义
         return addExclude(exclude);
         return addExclude(exclude);
     }
     }
 
 
@@ -361,6 +365,7 @@ public class StrategyConfig {
      */
      */
     @Deprecated
     @Deprecated
     public StrategyConfig setFieldPrefix(String... fieldPrefixs) {
     public StrategyConfig setFieldPrefix(String... fieldPrefixs) {
+        this.fieldPrefix.clear();   //保持语义
         return addFieldPrefix(fieldPrefixs);
         return addFieldPrefix(fieldPrefixs);
     }
     }
 
 
@@ -461,6 +466,7 @@ public class StrategyConfig {
      */
      */
     @Deprecated
     @Deprecated
     public StrategyConfig setTableFillList(List<TableFill> tableFillList) {
     public StrategyConfig setTableFillList(List<TableFill> tableFillList) {
+        this.tableFillList.clear(); //保持语义
         return this.addTableFills(tableFillList.toArray(new TableFill[]{}));
         return this.addTableFills(tableFillList.toArray(new TableFill[]{}));
     }
     }
 
 

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

@@ -319,6 +319,7 @@ public class ConfigBuilder {
      */
      */
     @Deprecated
     @Deprecated
     public ConfigBuilder setTableInfoList(List<TableInfo> tableInfoList) {
     public ConfigBuilder setTableInfoList(List<TableInfo> tableInfoList) {
+        this.tableInfoList.clear(); //保持语义
         this.tableInfoList.addAll(tableInfoList);
         this.tableInfoList.addAll(tableInfoList);
         return this;
         return this;
     }
     }

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

@@ -109,6 +109,7 @@ public class TableInfo {
      */
      */
     @Deprecated
     @Deprecated
     public TableInfo setFields(List<TableField> fields) {
     public TableInfo setFields(List<TableField> fields) {
+        this.fields.clear();    //保持语义
         return addFields(fields);
         return addFields(fields);
     }
     }
 
 
@@ -140,8 +141,8 @@ public class TableInfo {
      */
      */
     @Deprecated
     @Deprecated
     public TableInfo setImportPackages(String pkg) {
     public TableInfo setImportPackages(String pkg) {
-        importPackages.add(pkg);
-        return this;
+        importPackages.clear(); //保持语义
+        return addImportPackages(pkg);
     }
     }
 
 
     /**
     /**
@@ -191,6 +192,7 @@ public class TableInfo {
      */
      */
     @Deprecated
     @Deprecated
     public TableInfo setCommonFields(List<TableField> commonFields) {
     public TableInfo setCommonFields(List<TableField> commonFields) {
+        this.commonFields.clear(); //保持语义
         return addCommonFields(commonFields);
         return addCommonFields(commonFields);
     }
     }
 
 

+ 5 - 39
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/querys/DecoratorDbQuery.java

@@ -41,15 +41,13 @@ import java.util.stream.Collectors;
 public class DecoratorDbQuery extends AbstractDbQuery {
 public class DecoratorDbQuery extends AbstractDbQuery {
 
 
     private final IDbQuery dbQuery;
     private final IDbQuery dbQuery;
-    private final DataSourceConfig dataSourceConfig;
     private final Connection connection;
     private final Connection connection;
     private final DbType dbType;
     private final DbType dbType;
     private final StrategyConfig strategyConfig;
     private final StrategyConfig strategyConfig;
-    private String schema;
+    private final String schema;
 
 
     public DecoratorDbQuery(IDbQuery dbQuery, DataSourceConfig dataSourceConfig, StrategyConfig strategyConfig) {
     public DecoratorDbQuery(IDbQuery dbQuery, DataSourceConfig dataSourceConfig, StrategyConfig strategyConfig) {
         this.dbQuery = dbQuery;
         this.dbQuery = dbQuery;
-        this.dataSourceConfig = dataSourceConfig;
         this.connection = dataSourceConfig.getConn();
         this.connection = dataSourceConfig.getConn();
         this.dbType = dataSourceConfig.getDbType();
         this.dbType = dataSourceConfig.getDbType();
         this.strategyConfig = strategyConfig;
         this.strategyConfig = strategyConfig;
@@ -59,33 +57,8 @@ public class DecoratorDbQuery extends AbstractDbQuery {
     @Override
     @Override
     public String tablesSql() {
     public String tablesSql() {
         String tablesSql = dbQuery.tablesSql();
         String tablesSql = dbQuery.tablesSql();
-        String schema = dataSourceConfig.getSchemaName();
-        if (DbType.POSTGRE_SQL == dbType) {
-            if (schema == null) {
-                //pg 默认 schema=public
-                this.schema = "public";
-            }
-            tablesSql = String.format(tablesSql, schema);
-        } else if (DbType.KINGBASE_ES == dbType) {
-            if (schema == null) {
-                //kingbase 默认 schema=PUBLIC
-                this.schema = "PUBLIC";
-            }
-            tablesSql = String.format(tablesSql, schema);
-        } else if (DbType.DB2 == dbType) {
-            if (schema == null) {
-                //db2 默认 schema=current schema
-                this.schema = "current schema";
-            }
-            tablesSql = String.format(tablesSql, schema);
-        }
-        //oracle数据库表太多,出现最大游标错误
-        else if (DbType.ORACLE == dbType) {
-            //oracle 默认 schema=username
-            if (schema == null) {
-                this.schema = dataSourceConfig.getUsername().toUpperCase();
-            }
-            tablesSql = String.format(tablesSql, schema);
+        if (DbType.POSTGRE_SQL == dbType || DbType.KINGBASE_ES == dbType || DbType.DB2 == dbType || DbType.ORACLE == dbType) {
+            tablesSql = String.format(tablesSql, this.schema);
         }
         }
         if (strategyConfig.isEnableSqlFilter()) {
         if (strategyConfig.isEnableSqlFilter()) {
             StringBuilder sql = new StringBuilder(tablesSql);
             StringBuilder sql = new StringBuilder(tablesSql);
@@ -121,9 +94,7 @@ public class DecoratorDbQuery extends AbstractDbQuery {
      */
      */
     public String tableFieldsSql(String tableName) {
     public String tableFieldsSql(String tableName) {
         String tableFieldsSql = this.tableFieldsSql();
         String tableFieldsSql = this.tableFieldsSql();
-        if (DbType.KINGBASE_ES == dbType) {
-            tableFieldsSql = String.format(tableFieldsSql, this.schema, tableName);
-        } else if (DbType.DB2 == dbType) {
+        if (DbType.KINGBASE_ES == dbType || DbType.DB2 == dbType) {
             tableFieldsSql = String.format(tableFieldsSql, this.schema, tableName);
             tableFieldsSql = String.format(tableFieldsSql, this.schema, tableName);
         } else if (DbType.ORACLE == dbType) {
         } else if (DbType.ORACLE == dbType) {
             tableName = tableName.toUpperCase();
             tableName = tableName.toUpperCase();
@@ -190,8 +161,7 @@ public class DecoratorDbQuery extends AbstractDbQuery {
                 try {
                 try {
                     customMap.put(fc, resultSet.getObject(fc));
                     customMap.put(fc, resultSet.getObject(fc));
                 } catch (SQLException sqlException) {
                 } catch (SQLException sqlException) {
-                    //ignore
-                    customMap.put(fc, null);
+                    throw new RuntimeException("获取自定义字段错误:", sqlException);
                 }
                 }
             }
             }
             return customMap;
             return customMap;
@@ -200,10 +170,6 @@ public class DecoratorDbQuery extends AbstractDbQuery {
     }
     }
 
 
     public void query(String sql, Consumer<ResultSetWrapper> consumer) throws SQLException {
     public void query(String sql, Consumer<ResultSetWrapper> consumer) throws SQLException {
-        if (StringUtils.isNotBlank(this.schema)) {
-            //切换至指定schema.
-            connection.setSchema(this.schema);
-        }
         PreparedStatement preparedStatement = connection.prepareStatement(sql);
         PreparedStatement preparedStatement = connection.prepareStatement(sql);
         try (ResultSet resultSet = preparedStatement.executeQuery()) {
         try (ResultSet resultSet = preparedStatement.executeQuery()) {
             while (resultSet.next()) {
             while (resultSet.next()) {