Merge pull request #56 from font-C/main

fixed: codegen supports Schema。
This commit is contained in:
Michael Yang 2023-06-14 10:57:52 +08:00 committed by GitHub
commit d8ee920a34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 88 additions and 23 deletions

View File

@ -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");
```

View File

@ -87,8 +87,9 @@ public class Generator {
private List<Table> buildTables() throws SQLException {
StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
String schemaName = strategyConfig.getGenerateSchema();
List<Table> 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"});
}
}
}

View File

@ -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()
*/

View File

@ -62,6 +62,10 @@ public class StrategyConfig {
*/
private Map<String, ColumnConfig> columnConfigMap;
/**
* 需要生成的表在哪个模式下
*/
private String generateSchema;
/**
* 生成哪些表白名单
*/
@ -72,6 +76,27 @@ public class StrategyConfig {
*/
private Set<String> unGenerateTables;
/**
* 设置要生成的模式
*
* @return
*/
public String getGenerateSchema() {
return generateSchema;
}
/**
* 获取要生成的模式
*
* @param generateSchema
* @return
*/
public StrategyConfig setGenerateSchema(String generateSchema) {
this.generateSchema = generateSchema;
return this;
}
/**
* 获取表配置
*/

View File

@ -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;
}

View File

@ -29,7 +29,7 @@ public abstract class JdbcDialect implements IDialect {
public void buildTableColumns(Table table, GlobalConfig globalConfig, DatabaseMetaData dbMeta, Connection conn) throws SQLException {
Map<String, String> 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);
}

View File

@ -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);
}

View File

@ -24,6 +24,7 @@ import java.util.stream.Collectors;
public class Table {
private String schema;
private String name;
private String comment;
private Set<String> 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 + '\'' +