Skip to content

Commit ae6924e

Browse files
committed
生成器代码优化.
1 parent 7b65f18 commit ae6924e

File tree

6 files changed

+164
-147
lines changed

6 files changed

+164
-147
lines changed

mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/AutoGenerator.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,23 @@ public class AutoGenerator {
6868
/**
6969
* 数据库表配置
7070
*/
71-
private StrategyConfig strategy;
71+
private StrategyConfig strategy = new StrategyConfig();
7272
/**
7373
* 包 相关配置
7474
*/
75-
private PackageConfig packageInfo;
75+
private PackageConfig packageInfo = new PackageConfig();
7676
/**
7777
* 模板 相关配置
7878
*/
79-
private TemplateConfig template;
79+
private TemplateConfig template = new TemplateConfig();
8080
/**
8181
* 全局 相关配置
8282
*/
83-
private GlobalConfig globalConfig;
83+
private GlobalConfig globalConfig = new GlobalConfig();
8484
/**
8585
* 模板引擎
8686
*/
87-
private AbstractTemplateEngine templateEngine;
87+
private AbstractTemplateEngine templateEngine = new VelocityTemplateEngine(); // 为了兼容之前逻辑,采用 Velocity 引擎 【 默认 】
8888

8989
/**
9090
* 生成代码
@@ -98,10 +98,6 @@ public void execute() {
9898
injectionConfig.setConfig(config);
9999
}
100100
}
101-
if (null == templateEngine) {
102-
// 为了兼容之前逻辑,采用 Velocity 引擎 【 默认 】
103-
templateEngine = new VelocityTemplateEngine();
104-
}
105101
// 模板引擎初始化执行文件输出
106102
templateEngine.init(this.pretreatmentConfigBuilder(config)).mkdirs().batchOutput().open();
107103
logger.debug("==========================文件生成完成!!!==========================");

mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/PackageConfig.java

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.baomidou.mybatisplus.generator.config;
1717

1818

19+
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
1920
import com.baomidou.mybatisplus.core.toolkit.StringPool;
2021
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
2122
import lombok.Data;
@@ -90,4 +91,18 @@ public String getParent() {
9091
public String joinPackage(String subPackage) {
9192
return StringUtils.isBlank(parent) ? subPackage : (parent + StringPool.DOT + subPackage);
9293
}
94+
95+
public Map<String, String> initPackageInfo() {
96+
Map<String, String> initMap = CollectionUtils.newHashMapWithExpectedSize(7);
97+
initMap.put(ConstVal.MODULE_NAME, this.getModuleName());
98+
initMap.put(ConstVal.ENTITY, this.joinPackage(this.getEntity()));
99+
initMap.put(ConstVal.MAPPER, this.joinPackage(this.getMapper()));
100+
initMap.put(ConstVal.XML, this.joinPackage(this.getXml()));
101+
initMap.put(ConstVal.SERVICE, this.joinPackage(this.getService()));
102+
initMap.put(ConstVal.SERVICE_IMPL, this.joinPackage(this.getServiceImpl()));
103+
initMap.put(ConstVal.CONTROLLER, this.joinPackage(this.getController()));
104+
return initMap;
105+
}
106+
107+
93108
}

mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/StrategyConfig.java

+36
Original file line numberDiff line numberDiff line change
@@ -419,4 +419,40 @@ public Set<String> getSuperEntityColumns() {
419419
}
420420
return this.superEntityColumns;
421421
}
422+
423+
public void validate(){
424+
boolean isInclude = this.getInclude().size() > 0;
425+
boolean isExclude = this.getExclude().size() > 0;
426+
if (isInclude && isExclude) {
427+
throw new IllegalArgumentException("<strategy> 标签中 <include> 与 <exclude> 只能配置一项!");
428+
}
429+
if (this.getNotLikeTable() != null && this.getLikeTable() != null) {
430+
throw new IllegalArgumentException("<strategy> 标签中 <likeTable> 与 <notLikeTable> 只能配置一项!");
431+
}
432+
}
433+
434+
public boolean matchIncludeTable(String tableName) {
435+
return matchTable(tableName, this.getInclude());
436+
}
437+
438+
public boolean matchExcludeTable(String tableName) {
439+
return matchTable(tableName, this.getExclude());
440+
}
441+
442+
private boolean matchTable(String tableName, Set<String> matchTables) {
443+
return matchTables.stream().anyMatch(t -> tableNameMatches(t, tableName));
444+
}
445+
446+
/**
447+
* 表名匹配
448+
*
449+
* @param setTableName 设置表名
450+
* @param dbTableName 数据库表单
451+
* @return ignore
452+
*/
453+
private boolean tableNameMatches(String setTableName, String dbTableName) {
454+
return setTableName.equalsIgnoreCase(dbTableName)
455+
|| StringUtils.matches(setTableName, dbTableName);
456+
}
457+
422458
}

mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/TemplateConfig.java

+44-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@
1515
*/
1616
package com.baomidou.mybatisplus.generator.config;
1717

18+
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
19+
import com.baomidou.mybatisplus.core.toolkit.StringPool;
20+
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
1821
import lombok.AccessLevel;
1922
import lombok.Data;
2023
import lombok.Getter;
2124
import lombok.experimental.Accessors;
2225

26+
import java.io.File;
27+
import java.util.Map;
28+
2329
/**
2430
* 模板路径配置项
2531
*
@@ -48,7 +54,7 @@ public class TemplateConfig {
4854
public String getEntity(boolean kotlin) {
4955
return kotlin ? entityKt : entity;
5056
}
51-
57+
5258
/**
5359
* 禁用模板
5460
*
@@ -80,5 +86,41 @@ public TemplateConfig disable(TemplateType... templateTypes) {
8086
}
8187
return this;
8288
}
83-
89+
90+
public Map<String, String> initTemplate(GlobalConfig globalConfig, Map<String, String> packageInfo) {
91+
Map<String, String> initMap = CollectionUtils.newHashMapWithExpectedSize(6);
92+
String outputDir = globalConfig.getOutputDir();
93+
setPathInfo(initMap, packageInfo, this.getEntity(globalConfig.isKotlin()), outputDir, ConstVal.ENTITY_PATH, ConstVal.ENTITY);
94+
setPathInfo(initMap, packageInfo, this.getMapper(), outputDir, ConstVal.MAPPER_PATH, ConstVal.MAPPER);
95+
setPathInfo(initMap, packageInfo, this.getXml(), outputDir, ConstVal.XML_PATH, ConstVal.XML);
96+
setPathInfo(initMap, packageInfo, this.getService(), outputDir, ConstVal.SERVICE_PATH, ConstVal.SERVICE);
97+
setPathInfo(initMap, packageInfo, this.getServiceImpl(), outputDir, ConstVal.SERVICE_IMPL_PATH, ConstVal.SERVICE_IMPL);
98+
setPathInfo(initMap, packageInfo, this.getController(), outputDir, ConstVal.CONTROLLER_PATH, ConstVal.CONTROLLER);
99+
return initMap;
100+
}
101+
102+
private void setPathInfo(Map<String, String> pathInfo, Map<String, String> packageInfo, String template, String outputDir, String path, String module) {
103+
if (StringUtils.isNotBlank(template)) {
104+
pathInfo.put(path, joinPath(outputDir, packageInfo.get(module)));
105+
}
106+
}
107+
108+
/**
109+
* 连接路径字符串
110+
*
111+
* @param parentDir 路径常量字符串
112+
* @param packageName 包名
113+
* @return 连接后的路径
114+
*/
115+
private String joinPath(String parentDir, String packageName) {
116+
if (StringUtils.isBlank(parentDir)) {
117+
parentDir = System.getProperty(ConstVal.JAVA_TMPDIR);
118+
}
119+
if (!StringUtils.endsWith(parentDir, File.separator)) {
120+
parentDir += File.separator;
121+
}
122+
packageName = packageName.replaceAll("\\.", StringPool.BACK_SLASH + File.separator);
123+
return parentDir + packageName;
124+
}
125+
84126
}

0 commit comments

Comments
 (0)