mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
fixed: codegen supports Schema。
This commit is contained in:
parent
6f30da2821
commit
9a49c584cd
@ -85,8 +85,9 @@ public class Generator {
|
|||||||
|
|
||||||
private List<Table> buildTables() throws SQLException {
|
private List<Table> buildTables() throws SQLException {
|
||||||
StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
|
StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
|
||||||
|
String schemaName = strategyConfig.getGenerateSchema();
|
||||||
List<Table> tables = new ArrayList<>();
|
List<Table> tables = new ArrayList<>();
|
||||||
try (ResultSet rs = getTablesResultSet()) {
|
try (ResultSet rs = getTablesResultSet(schemaName)) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
String tableName = rs.getString("TABLE_NAME");
|
String tableName = rs.getString("TABLE_NAME");
|
||||||
if (!strategyConfig.isSupportGenerate(tableName)) {
|
if (!strategyConfig.isSupportGenerate(tableName)) {
|
||||||
@ -97,6 +98,7 @@ public class Generator {
|
|||||||
table.setGlobalConfig(globalConfig);
|
table.setGlobalConfig(globalConfig);
|
||||||
table.setTableConfig(strategyConfig.getTableConfig(tableName));
|
table.setTableConfig(strategyConfig.getTableConfig(tableName));
|
||||||
|
|
||||||
|
table.setSchema(schemaName);
|
||||||
table.setName(tableName);
|
table.setName(tableName);
|
||||||
|
|
||||||
String remarks = rs.getString("REMARKS");
|
String remarks = rs.getString("REMARKS");
|
||||||
@ -114,11 +116,11 @@ public class Generator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected ResultSet getTablesResultSet() throws SQLException {
|
protected ResultSet getTablesResultSet(String schema) throws SQLException {
|
||||||
if (globalConfig.getStrategyConfig().isGenerateForView()) {
|
if (globalConfig.getStrategyConfig().isGenerateForView()) {
|
||||||
return dialect.getTablesResultSet(dbMeta, conn, new String[]{"TABLE", "VIEW"});
|
return dialect.getTablesResultSet(dbMeta, conn, schema, new String[]{"TABLE", "VIEW"});
|
||||||
} else {
|
} else {
|
||||||
return dialect.getTablesResultSet(dbMeta, conn, new String[]{"TABLE"});
|
return dialect.getTablesResultSet(dbMeta, conn, schema, new String[]{"TABLE"});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -60,6 +60,10 @@ public class StrategyConfig {
|
|||||||
*/
|
*/
|
||||||
private Map<String, ColumnConfig> columnConfigMap;
|
private Map<String, ColumnConfig> columnConfigMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 需要生成的表在哪个模式下
|
||||||
|
*/
|
||||||
|
private String generateSchema;
|
||||||
/**
|
/**
|
||||||
* 生成哪些表,白名单。
|
* 生成哪些表,白名单。
|
||||||
*/
|
*/
|
||||||
@ -69,7 +73,28 @@ public class StrategyConfig {
|
|||||||
* 不生成哪些表,黑名单。
|
* 不生成哪些表,黑名单。
|
||||||
*/
|
*/
|
||||||
private Set<String> unGenerateTables;
|
private Set<String> unGenerateTables;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置要生成的模式
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getGenerateSchema() {
|
||||||
|
return generateSchema;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取要生成的模式
|
||||||
|
*
|
||||||
|
* @param generateSchema
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public StrategyConfig setGenerateSchema(String generateSchema) {
|
||||||
|
this.generateSchema = generateSchema;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取表配置。
|
* 获取表配置。
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -17,6 +17,7 @@ package com.mybatisflex.codegen.dialect;
|
|||||||
|
|
||||||
import com.mybatisflex.codegen.config.GlobalConfig;
|
import com.mybatisflex.codegen.config.GlobalConfig;
|
||||||
import com.mybatisflex.codegen.entity.Table;
|
import com.mybatisflex.codegen.entity.Table;
|
||||||
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DatabaseMetaData;
|
import java.sql.DatabaseMetaData;
|
||||||
@ -27,29 +28,29 @@ public interface IDialect {
|
|||||||
|
|
||||||
IDialect DEFAULT = new JdbcDialect() {
|
IDialect DEFAULT = new JdbcDialect() {
|
||||||
@Override
|
@Override
|
||||||
String forBuildColumnsSql(String tableName) {
|
String forBuildColumnsSql(String schema, String tableName) {
|
||||||
return "SELECT * FROM " + tableName + " WHERE 1 = 2";
|
return "SELECT * FROM " + (StringUtil.isNotBlank(schema) ? schema + "." : "") + tableName + " WHERE 1 = 2";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
IDialect MYSQL = new JdbcDialect() {
|
IDialect MYSQL = new JdbcDialect() {
|
||||||
@Override
|
@Override
|
||||||
String forBuildColumnsSql(String tableName) {
|
String forBuildColumnsSql(String schema, String tableName) {
|
||||||
return "SELECT * FROM `" + tableName + "` WHERE 1 = 2";
|
return "SELECT * FROM `" + (StringUtil.isNotBlank(schema) ? schema + "`.`" : "") + tableName + "` WHERE 1 = 2";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
IDialect ORACLE = new JdbcDialect() {
|
IDialect ORACLE = new JdbcDialect() {
|
||||||
@Override
|
@Override
|
||||||
public String forBuildColumnsSql(String tableName) {
|
public String forBuildColumnsSql(String schema, String tableName) {
|
||||||
return "SELECT * FROM \"" + tableName + "\" WHERE rownum < 1";
|
return "SELECT * FROM \"" + (StringUtil.isNotBlank(schema) ? schema + "\".\"" : "") + tableName + "\" WHERE rownum < 1";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String[] types) throws SQLException {
|
public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException {
|
||||||
return dbMeta.getTables(conn.getCatalog(), dbMeta.getUserName(), null, types);
|
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;
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,7 +29,7 @@ public abstract class JdbcDialect implements IDialect {
|
|||||||
public void buildTableColumns(Table table, GlobalConfig globalConfig, DatabaseMetaData dbMeta, Connection conn) throws SQLException {
|
public void buildTableColumns(Table table, GlobalConfig globalConfig, DatabaseMetaData dbMeta, Connection conn) throws SQLException {
|
||||||
Map<String, String> columnRemarks = buildColumnRemarks(table, dbMeta, conn);
|
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)) {
|
try (Statement stm = conn.createStatement(); ResultSet rs = stm.executeQuery(sql)) {
|
||||||
|
|
||||||
ResultSetMetaData columnMetaData = rs.getMetaData();
|
ResultSetMetaData columnMetaData = rs.getMetaData();
|
||||||
@ -67,10 +67,10 @@ public abstract class JdbcDialect implements IDialect {
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String[] types) throws SQLException {
|
public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException {
|
||||||
return dbMeta.getTables(conn.getCatalog(), null, null, types);
|
return dbMeta.getTables(conn.getCatalog(), schema, null, types);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
abstract String forBuildColumnsSql(String tableName);
|
abstract String forBuildColumnsSql(String schema, String tableName);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -50,8 +50,8 @@ public class SqliteDialect implements IDialect {
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String[] types) throws SQLException {
|
public ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema, String[] types) throws SQLException {
|
||||||
return dbMeta.getTables(conn.getCatalog(), null, null, types);
|
return dbMeta.getTables(conn.getCatalog(), schema, null, types);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -24,6 +24,7 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
public class Table {
|
public class Table {
|
||||||
|
|
||||||
|
private String schema;
|
||||||
private String name;
|
private String name;
|
||||||
private String comment;
|
private String comment;
|
||||||
private Set<String> primaryKeys;
|
private Set<String> primaryKeys;
|
||||||
@ -32,6 +33,14 @@ public class Table {
|
|||||||
private GlobalConfig globalConfig;
|
private GlobalConfig globalConfig;
|
||||||
private TableConfig tableConfig;
|
private TableConfig tableConfig;
|
||||||
|
|
||||||
|
public String getSchema() {
|
||||||
|
return schema;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSchema(String schema) {
|
||||||
|
this.schema = schema;
|
||||||
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
@ -268,10 +277,14 @@ public class Table {
|
|||||||
|
|
||||||
tableAnnotation.append("@Table(value = \"").append(name).append("\"");
|
tableAnnotation.append("@Table(value = \"").append(name).append("\"");
|
||||||
|
|
||||||
|
if (StringUtil.isNotBlank(schema)) {
|
||||||
|
tableAnnotation.append(", schema = \"").append(schema).append("\"");
|
||||||
|
}
|
||||||
|
|
||||||
if (tableConfig != null) {
|
if (tableConfig != null) {
|
||||||
if (tableConfig.getSchema() != null) {
|
// if (tableConfig.getSchema() != null) {
|
||||||
tableAnnotation.append(", schema = \"").append(tableConfig.getSchema()).append("\"");
|
// tableAnnotation.append(", schema = \"").append(tableConfig.getSchema()).append("\"");
|
||||||
}
|
// }
|
||||||
if (tableConfig.getCamelToUnderline() != null) {
|
if (tableConfig.getCamelToUnderline() != null) {
|
||||||
tableAnnotation.append(", camelToUnderline = \"").append(tableConfig.getCamelToUnderline()).append("\"");
|
tableAnnotation.append(", camelToUnderline = \"").append(tableConfig.getCamelToUnderline()).append("\"");
|
||||||
}
|
}
|
||||||
@ -294,6 +307,7 @@ public class Table {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Table{" +
|
return "Table{" +
|
||||||
|
"schema'" + schema + '\'' +
|
||||||
"name='" + name + '\'' +
|
"name='" + name + '\'' +
|
||||||
", remarks='" + comment + '\'' +
|
", remarks='" + comment + '\'' +
|
||||||
", primaryKeys='" + primaryKeys + '\'' +
|
", primaryKeys='" + primaryKeys + '\'' +
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user