Skip to content

Commit d551d9a

Browse files
committed
refactor: 增加对MSSQL的schemaName的支持
1 parent e86afa9 commit d551d9a

File tree

5 files changed

+68
-14
lines changed

5 files changed

+68
-14
lines changed

src/main/java/com/github/davidfantasy/mybatisplus/generatorui/GeneratorConfig.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.davidfantasy.mybatisplus.generatorui;
22

3+
import com.baomidou.mybatisplus.annotation.DbType;
34
import com.baomidou.mybatisplus.annotation.IdType;
45
import com.baomidou.mybatisplus.generator.config.ITypeConvert;
56
import com.baomidou.mybatisplus.generator.config.rules.DateType;
@@ -8,6 +9,8 @@
89
import lombok.Builder;
910
import lombok.Data;
1011

12+
import java.util.Objects;
13+
1114
@Builder
1215
@Data
1316
public class GeneratorConfig {
@@ -28,7 +31,7 @@ public class GeneratorConfig {
2831
private String jdbcUrl;
2932

3033
/**
31-
* 数据库schema,POSTGRE_SQL,ORACLE,DB2类型的数据库需要指定
34+
* 数据库schema,POSTGRE_SQL,ORACLE,DB2,MSSQL类型的数据库需要指定
3235
*/
3336
private String schemaName;
3437

@@ -47,21 +50,11 @@ public class GeneratorConfig {
4750
*/
4851
private String driverClassName = "com.mysql.cj.jdbc.Driver";
4952

50-
/**
51-
* PostgreSQL的schema name
52-
*/
53-
private String postgreSQLschema;
54-
5553
/**
5654
* 数据库时间类型与java class的对应策略
5755
*/
5856
private DateType dateType;
5957

60-
/**
61-
* 开启 ActiveRecord 模式
62-
*/
63-
private boolean activeRecord = false;
64-
6558
/**
6659
* 注入自定义模板参数
6760
*/
@@ -98,5 +91,4 @@ public DateType getDateType() {
9891
return dateType;
9992
}
10093

101-
10294
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.github.davidfantasy.mybatisplus.generatorui.dbquery;
2+
3+
import com.baomidou.mybatisplus.annotation.DbType;
4+
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
5+
import com.baomidou.mybatisplus.generator.config.IDbQuery;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Component;
8+
9+
import java.util.EnumMap;
10+
import java.util.Map;
11+
12+
@Component
13+
public class DbQueryHolder {
14+
15+
@Autowired
16+
private DataSourceConfig dataSourceConfig;
17+
18+
private final Map<DbType, IDbQuery> dbQueryMap = new EnumMap<>(DbType.class);
19+
20+
public DbQueryHolder() {
21+
dbQueryMap.put(DbType.SQL_SERVER, new SqlServerQuery());
22+
}
23+
24+
/**
25+
* 先查找本地是否有自定义的query实现,没有的话再去找mp内置的dbquery
26+
*/
27+
public IDbQuery getDbQuery(DbType dbType) {
28+
IDbQuery dbQuery = dbQueryMap.get(dbType);
29+
if (dbQuery != null) {
30+
return dbQuery;
31+
}
32+
return dataSourceConfig.getDbQuery();
33+
}
34+
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.davidfantasy.mybatisplus.generatorui.dbquery;
2+
3+
public class SqlServerQuery extends com.baomidou.mybatisplus.generator.config.querys.SqlServerQuery {
4+
5+
/**
6+
* 相对于MP原版增加了以SCHEMA作为过滤条件
7+
*/
8+
@Override
9+
public String tablesSql() {
10+
return "select a.TABLE_NAME,b.COMMENTS from INFORMATION_SCHEMA.TABLES a left join \n" +
11+
"(select cast(so.name as varchar(500)) as TABLE_NAME, cast(sep.value as varchar(500)) as COMMENTS from sysobjects so left JOIN sys.extended_properties sep on sep.major_id = so.id and sep.minor_id = 0 where (xtype = 'U' or xtype = 'v')) b \n" +
12+
"on a.TABLE_NAME = b.TABLE_NAME\n" +
13+
"where a.TABLE_SCHEMA = '%s'";
14+
}
15+
16+
}

src/main/java/com/github/davidfantasy/mybatisplus/generatorui/mbp/MbpGenerator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ private void configTemplate(TemplateConfig.Builder builder, List<String> choosed
139139
}
140140
}
141141

142+
//自定义模板参数配置
142143
private void configInjection(InjectionConfig.Builder builder, UserConfig userConfig, GenSetting genSetting) {
143144
//自定义参数
144145
builder.beforeOutputFile((tableInfo, objectMap) -> {
@@ -159,6 +160,9 @@ private void configInjection(InjectionConfig.Builder builder, UserConfig userCon
159160
controllerMethodsVar.put("hasMethod", true);
160161
}
161162
vars.put("controllerMethods", controllerMethodsVar);
163+
if(!StrUtil.isEmpty(generatorConfig.getSchemaName())){
164+
vars.put("schemaName", generatorConfig.getSchemaName()+".");
165+
}
162166
objectMap.putAll(vars);
163167
});
164168
//自定义文件生成

src/main/java/com/github/davidfantasy/mybatisplus/generatorui/service/DatabaseService.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.baomidou.mybatisplus.annotation.DbType;
44
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
55
import com.baomidou.mybatisplus.generator.config.IDbQuery;
6+
import com.github.davidfantasy.mybatisplus.generatorui.dbquery.DbQueryHolder;
67
import com.github.davidfantasy.mybatisplus.generatorui.dto.TableInfo;
78
import com.google.common.collect.Lists;
89
import org.springframework.beans.factory.annotation.Autowired;
@@ -22,8 +23,11 @@ public class DatabaseService {
2223
@Autowired
2324
private DataSourceConfig dataSourceConfig;
2425

26+
@Autowired
27+
private DbQueryHolder dbQueryHolder;
28+
2529
public List<TableInfo> getTablesFromDb() {
26-
IDbQuery dbQuery = dataSourceConfig.getDbQuery();
30+
IDbQuery dbQuery = dbQueryHolder.getDbQuery(dataSourceConfig.getDbType());
2731
List<Map<String, Object>> results = jdbcTemplate.queryForList(getTableSql());
2832
List<TableInfo> tableInfos = Lists.newArrayList();
2933
for (Map<String, Object> table : results) {
@@ -36,7 +40,7 @@ public List<TableInfo> getTablesFromDb() {
3640
}
3741

3842
public String getTableSql() {
39-
String tablesSql = dataSourceConfig.getDbQuery().tablesSql();
43+
String tablesSql = dbQueryHolder.getDbQuery(dataSourceConfig.getDbType()).tablesSql();
4044
String schema = dataSourceConfig.getSchemaName();
4145
if (schema == null) {
4246
schema = getDefaultSchema();
@@ -61,6 +65,9 @@ private String getDefaultSchema() {
6165
} else if (DbType.ORACLE == dbType) {
6266
//oracle 默认 schema=username
6367
schema = Objects.requireNonNull(dataSourceConfig.getUsername()).toUpperCase();
68+
} else if (DbType.SQL_SERVER == dbType) {
69+
//SQL_SERVER 2005以上 默认 schema=dbo
70+
schema = "dbo";
6471
}
6572
return schema;
6673
}

0 commit comments

Comments
 (0)