mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat: add Generator.getTables() method
This commit is contained in:
parent
cecdff9d8d
commit
bb4d83f559
@ -23,7 +23,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.mybatis-flex</groupId>
|
<groupId>com.mybatis-flex</groupId>
|
||||||
<artifactId>mybatis-flex-core</artifactId>
|
<artifactId>mybatis-flex-core</artifactId>
|
||||||
<version>${mybatis-flex.version}</version>
|
<version>1.7.2</version>
|
||||||
<exclusions>
|
<exclusions>
|
||||||
<exclusion>
|
<exclusion>
|
||||||
<groupId>com.mybatis-flex</groupId>
|
<groupId>com.mybatis-flex</groupId>
|
||||||
|
|||||||
@ -34,6 +34,7 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 代码生成器。
|
* 代码生成器。
|
||||||
|
*
|
||||||
* @author michael
|
* @author michael
|
||||||
*/
|
*/
|
||||||
public class Generator {
|
public class Generator {
|
||||||
@ -42,8 +43,6 @@ public class Generator {
|
|||||||
protected GlobalConfig globalConfig;
|
protected GlobalConfig globalConfig;
|
||||||
protected IDialect dialect = IDialect.DEFAULT;
|
protected IDialect dialect = IDialect.DEFAULT;
|
||||||
|
|
||||||
protected DatabaseMetaData dbMeta = null;
|
|
||||||
|
|
||||||
public Generator(DataSource dataSource, GlobalConfig globalConfig) {
|
public Generator(DataSource dataSource, GlobalConfig globalConfig) {
|
||||||
this.dataSource = dataSource;
|
this.dataSource = dataSource;
|
||||||
this.globalConfig = globalConfig;
|
this.globalConfig = globalConfig;
|
||||||
@ -56,31 +55,38 @@ public class Generator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void generate() {
|
public void generate() {
|
||||||
|
generate(getTables());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void generate(List<Table> tables) {
|
||||||
|
if (tables == null || tables.isEmpty()) {
|
||||||
|
System.err.printf("table %s not found.%n", globalConfig.getGenerateTables());
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
System.out.printf("find tables: %s%n", tables.stream().map(Table::getName).collect(Collectors.toSet()));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Table table : tables) {
|
||||||
|
Collection<IGenerator> generators = GeneratorFactory.getGenerators();
|
||||||
|
for (IGenerator generator : generators) {
|
||||||
|
generator.generate(table, globalConfig);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("Code is generated successfully.");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<Table> getTables() {
|
||||||
try (Connection conn = dataSource.getConnection()) {
|
try (Connection conn = dataSource.getConnection()) {
|
||||||
|
DatabaseMetaData dbMeta = conn.getMetaData();
|
||||||
dbMeta = conn.getMetaData();
|
return buildTables(dbMeta, conn);
|
||||||
List<Table> tables = buildTables(conn);
|
|
||||||
|
|
||||||
if (tables.isEmpty()) {
|
|
||||||
System.err.printf("table %s not found.%n", globalConfig.getGenerateTables());
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
System.out.printf("find tables: %s%n", tables.stream().map(Table::getName).collect(Collectors.toSet()));
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Table table : tables) {
|
|
||||||
Collection<IGenerator> generators = GeneratorFactory.getGenerators();
|
|
||||||
for (IGenerator generator : generators) {
|
|
||||||
generator.generate(table, globalConfig);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
System.out.println("Code is generated successfully.");
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void buildPrimaryKey(Connection conn, Table table) throws SQLException {
|
protected void buildPrimaryKey(DatabaseMetaData dbMeta, Connection conn, Table table) throws SQLException {
|
||||||
try (ResultSet rs = dbMeta.getPrimaryKeys(conn.getCatalog(), null, table.getName())) {
|
try (ResultSet rs = dbMeta.getPrimaryKeys(conn.getCatalog(), null, table.getName())) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
String primaryKey = rs.getString("COLUMN_NAME");
|
String primaryKey = rs.getString("COLUMN_NAME");
|
||||||
@ -89,11 +95,11 @@ public class Generator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Table> buildTables(Connection conn) throws SQLException {
|
protected List<Table> buildTables(DatabaseMetaData dbMeta, Connection conn) throws SQLException {
|
||||||
StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
|
StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
|
||||||
String schemaName = strategyConfig.getGenerateSchema();
|
String schemaName = strategyConfig.getGenerateSchema();
|
||||||
List<Table> tables = new ArrayList<>();
|
List<Table> tables = new ArrayList<>();
|
||||||
try (ResultSet rs = getTablesResultSet(conn, schemaName)) {
|
try (ResultSet rs = getTablesResultSet(dbMeta, conn, 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)) {
|
||||||
@ -111,7 +117,7 @@ public class Generator {
|
|||||||
table.setComment(remarks);
|
table.setComment(remarks);
|
||||||
|
|
||||||
|
|
||||||
buildPrimaryKey(conn, table);
|
buildPrimaryKey(dbMeta, conn, table);
|
||||||
|
|
||||||
dialect.buildTableColumns(schemaName, table, globalConfig, dbMeta, conn);
|
dialect.buildTableColumns(schemaName, table, globalConfig, dbMeta, conn);
|
||||||
|
|
||||||
@ -122,7 +128,7 @@ public class Generator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected ResultSet getTablesResultSet(Connection conn, String schema) throws SQLException {
|
protected ResultSet getTablesResultSet(DatabaseMetaData dbMeta, Connection conn, String schema) throws SQLException {
|
||||||
if (globalConfig.getStrategyConfig().isGenerateForView()) {
|
if (globalConfig.getStrategyConfig().isGenerateForView()) {
|
||||||
return dialect.getTablesResultSet(dbMeta, conn, schema, new String[]{"TABLE", "VIEW"});
|
return dialect.getTablesResultSet(dbMeta, conn, schema, new String[]{"TABLE", "VIEW"});
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -130,7 +130,6 @@ public class Table {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addColumn(Column column) {
|
public void addColumn(Column column) {
|
||||||
|
|
||||||
//主键
|
//主键
|
||||||
if (primaryKeys != null && primaryKeys.contains(column.getName())) {
|
if (primaryKeys != null && primaryKeys.contains(column.getName())) {
|
||||||
column.setPrimaryKey(true);
|
column.setPrimaryKey(true);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user