diff --git a/docs/zh/others/codegen.md b/docs/zh/others/codegen.md index 4dbdbb52..08be198d 100644 --- a/docs/zh/others/codegen.md +++ b/docs/zh/others/codegen.md @@ -62,6 +62,7 @@ public class Codegen { globalConfig.setBasePackage("com.test"); //设置表前缀和只生成哪些表 + globalConfig.setGenerateSchema("schema"); globalConfig.setTablePrefix("tb_"); globalConfig.setGenerateTable("account", "account_session"); @@ -92,6 +93,7 @@ public class Codegen { //设置表前缀和只生成哪些表,setGenerateTable 未配置时,生成所有表 globalConfig.getStrategyConfig() + .setGenerateSchema("schema") .setTablePrefix("tb_") .setGenerateTable("account", "account_session"); @@ -243,12 +245,14 @@ globalConfig.getPackageConfig() | setVersionColumn(String) | 乐观锁的字段名称 | null | | setGenerateForView(boolean) | 是否生成视图映射 | false | | setTableConfig(TableConfig) | 单独为某张表添加独立的配置 | null | +| setGenerateSchema(String) | 生成哪个schema下的表 | null | | setColumnConfig(ColumnConfig) | 设置某个列的全局配置 | null | | setGenerateTables(String...) | 生成哪些表,白名单 | null | | setUnGenerateTables(String...) | 不生成哪些表,黑名单 | null | ```java globalConfig.getStrategyConfig() + .setGenerateSchema("schema") .setTablePrefix("sys_") .setGenerateTables("sys_user","sys_dept"); ``` diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java index b222f58b..34e79d39 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java @@ -87,8 +87,9 @@ public class Generator { private List buildTables() throws SQLException { StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); + String schemaName = strategyConfig.getGenerateSchema(); List
tables = new ArrayList<>(); - try (ResultSet rs = getTablesResultSet()) { + try (ResultSet rs = getTablesResultSet(schemaName)) { while (rs.next()) { String tableName = rs.getString("TABLE_NAME"); if (!strategyConfig.isSupportGenerate(tableName)) { @@ -99,6 +100,7 @@ public class Generator { table.setGlobalConfig(globalConfig); table.setTableConfig(strategyConfig.getTableConfig(tableName)); + table.setSchema(schemaName); table.setName(tableName); String remarks = rs.getString("REMARKS"); @@ -116,11 +118,11 @@ public class Generator { } - protected ResultSet getTablesResultSet() throws SQLException { + protected ResultSet getTablesResultSet(String schema) throws SQLException { if (globalConfig.getStrategyConfig().isGenerateForView()) { - return dialect.getTablesResultSet(dbMeta, conn, new String[]{"TABLE", "VIEW"}); + return dialect.getTablesResultSet(dbMeta, conn, schema, new String[]{"TABLE", "VIEW"}); } else { - return dialect.getTablesResultSet(dbMeta, conn, new String[]{"TABLE"}); + return dialect.getTablesResultSet(dbMeta, conn, schema, new String[]{"TABLE"}); } } } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java index 5b0a9274..fbd1d848 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java @@ -612,6 +612,22 @@ public class GlobalConfig { getStrategyConfig().setGenerateForView(generateForView); } + + /** + * @see StrategyConfig#getGenerateSchema() + */ + public String getGenerateSchema() { + return getStrategyConfig().getGenerateSchema(); + } + + /** + * @see StrategyConfig#setGenerateSchema(String) + */ + public void setGenerateSchema(String generateSchema) { + getStrategyConfig().setGenerateSchema(generateSchema); + } + + /** * @see StrategyConfig#getGenerateTables() */ diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java index a409d79f..0ba37250 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java @@ -62,6 +62,10 @@ public class StrategyConfig { */ private Map columnConfigMap; + /** + * 需要生成的表在哪个模式下 + */ + private String generateSchema; /** * 生成哪些表,白名单。 */ @@ -72,6 +76,27 @@ public class StrategyConfig { */ private Set unGenerateTables; + + /** + * 设置要生成的模式 + * + * @return + */ + public String getGenerateSchema() { + return generateSchema; + } + + /** + * 获取要生成的模式 + * + * @param generateSchema + * @return + */ + public StrategyConfig setGenerateSchema(String generateSchema) { + this.generateSchema = generateSchema; + return this; + } + /** * 获取表配置。 */ diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/IDialect.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/IDialect.java index 92afdece..97cad28b 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/IDialect.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/IDialect.java @@ -17,6 +17,7 @@ package com.mybatisflex.codegen.dialect; import com.mybatisflex.codegen.config.GlobalConfig; import com.mybatisflex.codegen.entity.Table; +import com.mybatisflex.core.util.StringUtil; import java.sql.Connection; import java.sql.DatabaseMetaData; @@ -27,29 +28,29 @@ public interface IDialect { IDialect DEFAULT = new JdbcDialect() { @Override - String forBuildColumnsSql(String tableName) { - return "SELECT * FROM " + tableName + " WHERE 1 = 2"; + String forBuildColumnsSql(String schema, String tableName) { + return "SELECT * FROM " + (StringUtil.isNotBlank(schema) ? schema + "." : "") + tableName + " WHERE 1 = 2"; } }; IDialect MYSQL = new JdbcDialect() { @Override - String forBuildColumnsSql(String tableName) { - return "SELECT * FROM `" + tableName + "` WHERE 1 = 2"; + String forBuildColumnsSql(String schema, String tableName) { + return "SELECT * FROM `" + (StringUtil.isNotBlank(schema) ? schema + "`.`" : "") + tableName + "` WHERE 1 = 2"; } }; IDialect ORACLE = new JdbcDialect() { @Override - public String forBuildColumnsSql(String tableName) { - return "SELECT * FROM \"" + tableName + "\" WHERE rownum < 1"; + public String forBuildColumnsSql(String schema, String tableName) { + return "SELECT * FROM \"" + (StringUtil.isNotBlank(schema) ? schema + "\".\"" : "") + tableName + "\" WHERE rownum < 1"; } @Override - public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String[] types) throws SQLException { - return dbMeta.getTables(conn.getCatalog(), dbMeta.getUserName(), null, types); + public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException { + return dbMeta.getTables(conn.getCatalog(), StringUtil.isNotBlank(schema) ? schema : dbMeta.getUserName(), null, types); } }; @@ -58,5 +59,5 @@ public interface IDialect { void buildTableColumns(Table table, GlobalConfig globalConfig, DatabaseMetaData dbMeta, Connection conn) throws SQLException; - ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String[] types) throws SQLException; + ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException; } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java index 33bacbe3..676e7ba1 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java @@ -29,7 +29,7 @@ public abstract class JdbcDialect implements IDialect { public void buildTableColumns(Table table, GlobalConfig globalConfig, DatabaseMetaData dbMeta, Connection conn) throws SQLException { Map columnRemarks = buildColumnRemarks(table, dbMeta, conn); - String sql = forBuildColumnsSql(table.getName()); + String sql = forBuildColumnsSql(table.getSchema(), table.getName()); try (Statement stm = conn.createStatement(); ResultSet rs = stm.executeQuery(sql)) { ResultSetMetaData columnMetaData = rs.getMetaData(); @@ -67,10 +67,10 @@ public abstract class JdbcDialect implements IDialect { @Override - public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String[] types) throws SQLException { - return dbMeta.getTables(conn.getCatalog(), null, null, types); + public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException { + return dbMeta.getTables(conn.getCatalog(), schema, null, types); } - abstract String forBuildColumnsSql(String tableName); + abstract String forBuildColumnsSql(String schema, String tableName); } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/SqliteDialect.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/SqliteDialect.java index 20ddc91c..7e57e149 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/SqliteDialect.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/SqliteDialect.java @@ -50,8 +50,8 @@ public class SqliteDialect implements IDialect { @Override - public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String[] types) throws SQLException { - return dbMeta.getTables(conn.getCatalog(), null, null, types); + public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException { + return dbMeta.getTables(conn.getCatalog(), schema, null, types); } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java index 675c85eb..2d7e5119 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java @@ -24,6 +24,7 @@ import java.util.stream.Collectors; public class Table { + private String schema; private String name; private String comment; private Set primaryKeys; @@ -32,6 +33,14 @@ public class Table { private GlobalConfig globalConfig; private TableConfig tableConfig; + public String getSchema() { + return schema; + } + + public void setSchema(String schema) { + this.schema = schema; + } + public String getName() { return name; } @@ -41,7 +50,10 @@ public class Table { } public String getComment() { - return globalConfig.getJavadocConfig().formatTableComment(comment); + if (StringUtil.isNotBlank(comment)) { + return globalConfig.getJavadocConfig().formatTableComment(comment); + } + return null; } public void setComment(String comment) { @@ -268,10 +280,14 @@ public class Table { tableAnnotation.append("@Table(value = \"").append(name).append("\""); + if (StringUtil.isNotBlank(schema)) { + tableAnnotation.append(", schema = \"").append(schema).append("\""); + } + if (tableConfig != null) { - if (tableConfig.getSchema() != null) { - tableAnnotation.append(", schema = \"").append(tableConfig.getSchema()).append("\""); - } +// if (tableConfig.getSchema() != null) { +// tableAnnotation.append(", schema = \"").append(tableConfig.getSchema()).append("\""); +// } if (tableConfig.getCamelToUnderline() != null) { tableAnnotation.append(", camelToUnderline = \"").append(tableConfig.getCamelToUnderline()).append("\""); } @@ -294,6 +310,7 @@ public class Table { @Override public String toString() { return "Table{" + + "schema'" + schema + '\'' + "name='" + name + '\'' + ", remarks='" + comment + '\'' + ", primaryKeys='" + primaryKeys + '\'' +