|
@@ -31,6 +31,7 @@ import com.baomidou.mybatisplus.generator.InjectionConfig;
|
|
|
import com.baomidou.mybatisplus.generator.config.ConstVal;
|
|
|
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
|
|
|
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
|
|
|
+import com.baomidou.mybatisplus.generator.config.IDbQuery;
|
|
|
import com.baomidou.mybatisplus.generator.config.PackageConfig;
|
|
|
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
|
|
|
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
|
|
@@ -39,7 +40,6 @@ import com.baomidou.mybatisplus.generator.config.po.TableFill;
|
|
|
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
|
|
import com.baomidou.mybatisplus.generator.config.rules.DbType;
|
|
|
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
|
|
|
-import com.baomidou.mybatisplus.generator.config.rules.QuerySQL;
|
|
|
import com.baomidou.mybatisplus.toolkit.StringUtils;
|
|
|
|
|
|
/**
|
|
@@ -67,7 +67,7 @@ public class ConfigBuilder {
|
|
|
/**
|
|
|
* SQL语句类型
|
|
|
*/
|
|
|
- private QuerySQL querySQL;
|
|
|
+ private IDbQuery dbQuery;
|
|
|
private String superEntityClass;
|
|
|
private String superMapperClass;
|
|
|
/**
|
|
@@ -101,6 +101,7 @@ public class ConfigBuilder {
|
|
|
*/
|
|
|
private InjectionConfig injectionConfig;
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 在构造器中处理配置
|
|
@@ -156,6 +157,7 @@ public class ConfigBuilder {
|
|
|
return packageInfo;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 所有路径配置
|
|
@@ -167,14 +169,17 @@ public class ConfigBuilder {
|
|
|
return pathInfo;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public String getSuperEntityClass() {
|
|
|
return superEntityClass;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public String getSuperMapperClass() {
|
|
|
return superMapperClass;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 获取超类定义
|
|
@@ -186,14 +191,17 @@ public class ConfigBuilder {
|
|
|
return superServiceClass;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public String getSuperServiceImplClass() {
|
|
|
return superServiceImplClass;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public String getSuperControllerClass() {
|
|
|
return superControllerClass;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 表信息
|
|
@@ -210,6 +218,7 @@ public class ConfigBuilder {
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 模板路径配置信息
|
|
@@ -264,6 +273,7 @@ public class ConfigBuilder {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 处理数据源配置
|
|
@@ -273,9 +283,10 @@ public class ConfigBuilder {
|
|
|
*/
|
|
|
private void handlerDataSource(DataSourceConfig config) {
|
|
|
connection = config.getConn();
|
|
|
- querySQL = getQuerySQL(config.getDbType());
|
|
|
+ dbQuery = config.getDbQuery();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 处理数据库表 加载数据库表、列、注释相关数据集
|
|
@@ -288,6 +299,7 @@ public class ConfigBuilder {
|
|
|
tableInfoList = getTablesInfo(config);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 处理superClassName,IdClassType,IdStrategy配置
|
|
@@ -315,6 +327,7 @@ public class ConfigBuilder {
|
|
|
superControllerClass = config.getSuperControllerClass();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 处理表对应的类名称
|
|
@@ -361,6 +374,7 @@ public class ConfigBuilder {
|
|
|
return tableList;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 检查是否有
|
|
@@ -407,6 +421,7 @@ public class ConfigBuilder {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 获取所有的数据库表信息
|
|
@@ -429,37 +444,37 @@ public class ConfigBuilder {
|
|
|
Set<String> notExistTables = new HashSet<>();
|
|
|
PreparedStatement preparedStatement = null;
|
|
|
try {
|
|
|
- String tableCommentsSql = querySQL.getTableCommentsSql();
|
|
|
- if (QuerySQL.POSTGRE_SQL == querySQL) {
|
|
|
- tableCommentsSql = String.format(tableCommentsSql, dataSourceConfig.getSchemaname());
|
|
|
+ String tablesSql = dbQuery.tablesSql();
|
|
|
+ if (DbType.POSTGRE_SQL == dbQuery.dbType()) {
|
|
|
+ tablesSql = String.format(tablesSql, dataSourceConfig.getSchemaname());
|
|
|
}
|
|
|
//oracle数据库表太多,出现最大游标错误
|
|
|
- else if (QuerySQL.ORACLE == querySQL) {
|
|
|
+ else if (DbType.ORACLE == dbQuery.dbType()) {
|
|
|
if (isInclude) {
|
|
|
- StringBuilder sb = new StringBuilder(tableCommentsSql);
|
|
|
- sb.append(" WHERE ").append(querySQL.getTableName()).append(" IN (");
|
|
|
+ StringBuilder sb = new StringBuilder(tablesSql);
|
|
|
+ sb.append(" WHERE ").append(dbQuery.tableName()).append(" IN (");
|
|
|
for (String tbname : config.getInclude()) {
|
|
|
sb.append("'").append(tbname.toUpperCase()).append("',");
|
|
|
}
|
|
|
sb.replace(sb.length() - 1, sb.length(), ")");
|
|
|
- tableCommentsSql = sb.toString();
|
|
|
+ tablesSql = sb.toString();
|
|
|
} else if (isExclude) {
|
|
|
- StringBuilder sb = new StringBuilder(tableCommentsSql);
|
|
|
- sb.append(" WHERE ").append(querySQL.getTableName()).append(" NOT IN (");
|
|
|
+ StringBuilder sb = new StringBuilder(tablesSql);
|
|
|
+ sb.append(" WHERE ").append(dbQuery.tableName()).append(" NOT IN (");
|
|
|
for (String tbname : config.getExclude()) {
|
|
|
sb.append("'").append(tbname.toUpperCase()).append("',");
|
|
|
}
|
|
|
sb.replace(sb.length() - 1, sb.length(), ")");
|
|
|
- tableCommentsSql = sb.toString();
|
|
|
+ tablesSql = sb.toString();
|
|
|
}
|
|
|
}
|
|
|
- preparedStatement = connection.prepareStatement(tableCommentsSql);
|
|
|
+ preparedStatement = connection.prepareStatement(tablesSql);
|
|
|
ResultSet results = preparedStatement.executeQuery();
|
|
|
TableInfo tableInfo;
|
|
|
while (results.next()) {
|
|
|
- String tableName = results.getString(querySQL.getTableName());
|
|
|
+ String tableName = results.getString(dbQuery.tableName());
|
|
|
if (StringUtils.isNotEmpty(tableName)) {
|
|
|
- String tableComment = results.getString(querySQL.getTableComment());
|
|
|
+ String tableComment = results.getString(dbQuery.tableComment());
|
|
|
if (config.isSkipView() && "VIEW".equals(tableComment)) {
|
|
|
// 跳过视图
|
|
|
continue;
|
|
@@ -531,28 +546,6 @@ public class ConfigBuilder {
|
|
|
}
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * <p>
|
|
|
- * 判断主键是否为identity,目前仅对mysql进行检查
|
|
|
- * </p>
|
|
|
- *
|
|
|
- * @param results ResultSet
|
|
|
- * @return 主键是否为identity
|
|
|
- * @throws SQLException
|
|
|
- */
|
|
|
- private boolean isKeyIdentity(ResultSet results) throws SQLException {
|
|
|
- if (QuerySQL.MYSQL == this.querySQL) {
|
|
|
- String extra = results.getString("Extra");
|
|
|
- if ("auto_increment".equals(extra)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- } else if (QuerySQL.SQL_SERVER == this.querySQL) {
|
|
|
- int isIdentity = results.getInt("isIdentity");
|
|
|
- return 1 == isIdentity;
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* <p>
|
|
|
* 将字段信息与表信息关联
|
|
@@ -567,8 +560,8 @@ public class ConfigBuilder {
|
|
|
List<TableField> fieldList = new ArrayList<>();
|
|
|
List<TableField> commonFieldList = new ArrayList<>();
|
|
|
try {
|
|
|
- String tableFieldsSql = querySQL.getTableFieldsSql();
|
|
|
- if (QuerySQL.POSTGRE_SQL == querySQL) {
|
|
|
+ String tableFieldsSql = dbQuery.tableFieldsSql();
|
|
|
+ if (DbType.POSTGRE_SQL == dbQuery.dbType()) {
|
|
|
tableFieldsSql = String.format(tableFieldsSql, dataSourceConfig.getSchemaname(), tableInfo.getName());
|
|
|
} else {
|
|
|
tableFieldsSql = String.format(tableFieldsSql, tableInfo.getName());
|
|
@@ -577,13 +570,13 @@ public class ConfigBuilder {
|
|
|
ResultSet results = preparedStatement.executeQuery();
|
|
|
while (results.next()) {
|
|
|
TableField field = new TableField();
|
|
|
- String key = results.getString(querySQL.getFieldKey());
|
|
|
+ String key = results.getString(dbQuery.fieldKey());
|
|
|
// 避免多重主键设置,目前只取第一个找到ID,并放到list中的索引为0的位置
|
|
|
boolean isId = StringUtils.isNotEmpty(key) && key.toUpperCase().equals("PRI");
|
|
|
// 处理ID
|
|
|
if (isId && !haveId) {
|
|
|
field.setKeyFlag(true);
|
|
|
- if (isKeyIdentity(results)) {
|
|
|
+ if (dbQuery.isKeyIdentity(results)) {
|
|
|
field.setKeyIdentityFlag(true);
|
|
|
}
|
|
|
haveId = true;
|
|
@@ -591,11 +584,11 @@ public class ConfigBuilder {
|
|
|
field.setKeyFlag(false);
|
|
|
}
|
|
|
// 处理其它信息
|
|
|
- field.setName(results.getString(querySQL.getFieldName()));
|
|
|
- field.setType(results.getString(querySQL.getFieldType()));
|
|
|
+ field.setName(results.getString(dbQuery.fieldName()));
|
|
|
+ field.setType(results.getString(dbQuery.fieldType()));
|
|
|
field.setPropertyName(strategyConfig, processName(field.getName(), strategy));
|
|
|
field.setColumnType(dataSourceConfig.getTypeConvert().processTypeConvert(field.getType()));
|
|
|
- field.setComment(results.getString(querySQL.getFieldComment()));
|
|
|
+ field.setComment(results.getString(dbQuery.fieldComment()));
|
|
|
if (strategyConfig.includeSuperEntityColumns(field.getName())) {
|
|
|
// 跳过公共字段
|
|
|
commonFieldList.add(field);
|
|
@@ -621,6 +614,7 @@ public class ConfigBuilder {
|
|
|
return tableInfo;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 连接路径字符串
|
|
@@ -641,6 +635,7 @@ public class ConfigBuilder {
|
|
|
return parentDir + packageName;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 连接父子包名
|
|
@@ -657,6 +652,7 @@ public class ConfigBuilder {
|
|
|
return parent + "." + subPackage;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 处理字段名称
|
|
@@ -668,6 +664,7 @@ public class ConfigBuilder {
|
|
|
return processName(name, strategy, this.strategyConfig.getFieldPrefix());
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* <p>
|
|
|
* 处理表/字段名称
|
|
@@ -702,44 +699,34 @@ public class ConfigBuilder {
|
|
|
return propertyName;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * <p>
|
|
|
- * 获取当前的SQL类型
|
|
|
- * </p>
|
|
|
- *
|
|
|
- * @return DB类型
|
|
|
- */
|
|
|
- private QuerySQL getQuerySQL(DbType dbType) {
|
|
|
- for (QuerySQL qs : QuerySQL.values()) {
|
|
|
- if (qs.getDbType().equals(dbType.getValue())) {
|
|
|
- return qs;
|
|
|
- }
|
|
|
- }
|
|
|
- return QuerySQL.MYSQL;
|
|
|
- }
|
|
|
|
|
|
public StrategyConfig getStrategyConfig() {
|
|
|
return strategyConfig;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public ConfigBuilder setStrategyConfig(StrategyConfig strategyConfig) {
|
|
|
this.strategyConfig = strategyConfig;
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public GlobalConfig getGlobalConfig() {
|
|
|
return globalConfig;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public ConfigBuilder setGlobalConfig(GlobalConfig globalConfig) {
|
|
|
this.globalConfig = globalConfig;
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public InjectionConfig getInjectionConfig() {
|
|
|
return injectionConfig;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public ConfigBuilder setInjectionConfig(InjectionConfig injectionConfig) {
|
|
|
this.injectionConfig = injectionConfig;
|
|
|
return this;
|