From 9a49c584cdbc717b663cd5a4a4509d8a2312da2f Mon Sep 17 00:00:00 2001
From: Font_C <1020331126@qq.com>
Date: Mon, 12 Jun 2023 19:21:13 +0800
Subject: [PATCH 1/5] =?UTF-8?q?fixed:=20codegen=20supports=20Schema?=
=?UTF-8?q?=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../com/mybatisflex/codegen/Generator.java | 10 ++++---
.../codegen/config/StrategyConfig.java | 27 ++++++++++++++++++-
.../mybatisflex/codegen/dialect/IDialect.java | 19 ++++++-------
.../codegen/dialect/JdbcDialect.java | 8 +++---
.../codegen/dialect/SqliteDialect.java | 4 +--
.../com/mybatisflex/codegen/entity/Table.java | 20 +++++++++++---
6 files changed, 65 insertions(+), 23 deletions(-)
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 547cd610..846e3153 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
@@ -85,8 +85,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)) {
@@ -97,6 +98,7 @@ public class Generator {
table.setGlobalConfig(globalConfig);
table.setTableConfig(strategyConfig.getTableConfig(tableName));
+ table.setSchema(schemaName);
table.setName(tableName);
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()) {
- 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/StrategyConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java
index 917053ac..d712f1c9 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
@@ -60,6 +60,10 @@ public class StrategyConfig {
*/
private Map columnConfigMap;
+ /**
+ * 需要生成的表在哪个模式下
+ */
+ private String generateSchema;
/**
* 生成哪些表,白名单。
*/
@@ -69,7 +73,28 @@ 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..449cd97b 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;
}
@@ -268,10 +277,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 +307,7 @@ public class Table {
@Override
public String toString() {
return "Table{" +
+ "schema'" + schema + '\'' +
"name='" + name + '\'' +
", remarks='" + comment + '\'' +
", primaryKeys='" + primaryKeys + '\'' +
From 43027dae3e16079efb35eca35df9d1309a306c38 Mon Sep 17 00:00:00 2001
From: Font_C <1020331126@qq.com>
Date: Mon, 12 Jun 2023 19:32:46 +0800
Subject: [PATCH 2/5] =?UTF-8?q?fixed:=20codegen=20supports=20Schema?=
=?UTF-8?q?=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../mybatisflex/codegen/config/GlobalConfig.java | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
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 3f31ef8f..8a005a8c 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
@@ -611,6 +611,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()
*/
From 58e66a86b9e036d255b94c7471cdb1043c0ed967 Mon Sep 17 00:00:00 2001
From: Font_C <1020331126@qq.com>
Date: Mon, 12 Jun 2023 19:45:58 +0800
Subject: [PATCH 3/5] =?UTF-8?q?fixed:=20codegen=20supports=20Schema?=
=?UTF-8?q?=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
docs/zh/others/codegen.md | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/docs/zh/others/codegen.md b/docs/zh/others/codegen.md
index 7130dbf9..740f513d 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 {
//设置表前缀和只生成哪些表
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");
```
From 874ebf1c462b5631fe61d407dedd23087f28d8a8 Mon Sep 17 00:00:00 2001
From: Font_C <1020331126@qq.com>
Date: Tue, 13 Jun 2023 15:16:54 +0800
Subject: [PATCH 4/5] =?UTF-8?q?=20Fixed=EF=BC=9A=20an=20error=20issue=20wh?=
=?UTF-8?q?en=20the=20table=20comment=20was=20empty=20after=20configuring?=
=?UTF-8?q?=20=E2=80=98setTableCommentFormat=E2=80=99.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../src/main/java/com/mybatisflex/codegen/entity/Table.java | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
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 449cd97b..6625a131 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
@@ -50,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) {
From d756b14424201ec92225e870949556589f4c238b Mon Sep 17 00:00:00 2001
From: Font_C <1020331126@qq.com>
Date: Tue, 13 Jun 2023 15:21:45 +0800
Subject: [PATCH 5/5] =?UTF-8?q?=20Fixed=EF=BC=9A=20an=20error=20issue=20wh?=
=?UTF-8?q?en=20the=20table=20comment=20was=20empty=20after=20configuring?=
=?UTF-8?q?=20=E2=80=98setTableCommentFormat=E2=80=99.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../src/main/java/com/mybatisflex/codegen/entity/Table.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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 6625a131..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
@@ -50,7 +50,7 @@ public class Table {
}
public String getComment() {
- if(StringUtil.isNotBlank(comment)) {
+ if (StringUtil.isNotBlank(comment)) {
return globalConfig.getJavadocConfig().formatTableComment(comment);
}
return null;