Skip to content

Commit ec11afe

Browse files
committed
支持自定义配置生成实体类
1 parent aca8ee0 commit ec11afe

File tree

15 files changed

+875
-27
lines changed

15 files changed

+875
-27
lines changed

api-boot-project/api-boot-maven-plugins/api-boot-mybatis-enhance-maven-codegen/src/main/java/org/minbox/framework/api/boot/maven/plugin/mybatis/enhance/codegen/ApiBootMybatisEnhanceCodegen.java

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@
2929
import org.apache.maven.plugins.annotations.LifecyclePhase;
3030
import org.apache.maven.plugins.annotations.Mojo;
3131
import org.apache.maven.plugins.annotations.Parameter;
32+
import org.codehaus.plexus.util.FileUtils;
3233
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.ClassBuilder;
3334
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.ClassBuilderFactory;
3435
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.impl.DynamicEntityClassBuilder;
3536
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.impl.EntityClassBuilder;
3637
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.wrapper.ClassBuilderWrapper;
38+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.template.CodegenFile;
39+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.template.CodegenTemplate;
3740
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.tools.CamelTools;
3841
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.writer.JavaClassWriter;
3942
import org.springframework.util.ObjectUtils;
@@ -60,10 +63,6 @@
6063
@Mojo(name = "generator", defaultPhase = LifecyclePhase.COMPILE)
6164
@Execute(phase = LifecyclePhase.COMPILE)
6265
public class ApiBootMybatisEnhanceCodegen extends AbstractMojo {
63-
/**
64-
* file suffix
65-
*/
66-
private static final String FILE_SUFFIX = ".java";
6766
/**
6867
* Whether to execute automatically
6968
* Default not to execute
@@ -150,12 +149,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
150149
getLog().warn("Automatic code generation is not turned on. If you need to generate entity classes, configure 【execute=true】");
151150
return;
152151
}
152+
153153
// code builder properties
154154
CodeBuilderProperties codeBuilderProperties = CodeBuilderProperties.builder().dbType(dbType).dbName(dbName).dbUrl(dbUrl).dbUserName(dbUserName).dbPassword(dbPassword).dbDriverClassName(dbDriverClassName).build();
155155

156156
// get database instance by DbTypeEnum
157157
DataBase dataBase = DataBaseFactory.newInstance(codeBuilderProperties);
158158

159+
// load codegen.setting.json
160+
String codegenSetting = loadCodegenSetting();
161+
159162
List<String> tableNames = ObjectUtils.isEmpty(tables) ? getTableNames(dataBase) : tables;
160163

161164
tableNames.stream().forEach(tableName -> {
@@ -201,9 +204,55 @@ public void execute() throws MojoExecutionException, MojoFailureException {
201204
JavaClassWriter.writeToJavaFile(classPath, classContent);
202205
}
203206
});
207+
208+
// generator java file with codegen.setting.json
209+
if (!ObjectUtils.isEmpty(codegenSetting)) {
210+
// cover old data
211+
wrapper.setTableCamelName(className);
212+
wrapper.setPackageName(packageName);
213+
214+
CodegenTemplate codegenTemplate = new CodegenTemplate(wrapper, codegenSetting);
215+
216+
// formatter all template java file
217+
List<CodegenFile> files = codegenTemplate.formatJavaFiles();
218+
219+
if (!ObjectUtils.isEmpty(files)) {
220+
files.stream().forEach(file -> {
221+
getLog().info("generation file -> " + file.getFileName());
222+
// generator package dir & return full file path
223+
String fullFilePath = getNewClassPath(file.getFileName(), file.getPackageName());
224+
if (!StringUtils.isEmpty(file.getJavaContent()) && !StringUtils.isEmpty(fullFilePath)) {
225+
// invoke content write
226+
JavaClassWriter.writeToJavaFile(fullFilePath, file.getJavaContent());
227+
}
228+
});
229+
}
230+
}
204231
});
205232
}
206233

234+
/**
235+
* load codegen.setting.json parse to CodegenSetting entity
236+
*/
237+
private String loadCodegenSetting() {
238+
try {
239+
// formatter codegen.setting.json path
240+
String settingJsonPath = String.format("%s%s", EnhanceCodegenConstant.CLASSES_PATH.replace(EnhanceCodegenConstant.POINT, File.separator), EnhanceCodegenConstant.SETTING_JSON);
241+
242+
// read codegen.setting.json content
243+
File file = new File(projectBaseDir + settingJsonPath);
244+
String content = FileUtils.fileRead(file);
245+
246+
getLog().info("codegen.setting.json:");
247+
getLog().info(content);
248+
249+
return content;
250+
} catch (Exception e) {
251+
getLog().error(e);
252+
}
253+
return null;
254+
}
255+
207256
/**
208257
* get table name list
209258
*
@@ -240,28 +289,6 @@ private String getGeneratorDir(String prefixDir) {
240289
* @return class path
241290
*/
242291
private String getNewClassPath(String entityClassName, String prefixDir) {
243-
return String.format("%s%s%s%s", getGeneratorDir(prefixDir), File.separator, entityClassName, FILE_SUFFIX);
244-
}
245-
246-
/**
247-
* 递归删除目录下的所有文件及子目录下所有文件
248-
*
249-
* @param dir 将要删除的文件目录
250-
* @return boolean Returns "true" if all deletions were successful.
251-
* If a deletion fails, the method stops attempting to
252-
* delete and returns "false".
253-
*/
254-
private static boolean deleteDir(File dir) {
255-
if (dir.isDirectory()) {
256-
String[] children = dir.list();
257-
for (int i = 0; i < children.length; i++) {
258-
boolean success = deleteDir(new File(dir, children[i]));
259-
if (!success) {
260-
return false;
261-
}
262-
}
263-
}
264-
// 目录此时为空,可以删除
265-
return dir.delete();
292+
return String.format("%s%s%s%s", getGeneratorDir(prefixDir), File.separator, entityClassName, EnhanceCodegenConstant.JAVA_SUFFIX);
266293
}
267294
}

api-boot-project/api-boot-maven-plugins/api-boot-mybatis-enhance-maven-codegen/src/main/java/org/minbox/framework/api/boot/maven/plugin/mybatis/enhance/codegen/EnhanceCodegenConstant.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,16 @@ public interface EnhanceCodegenConstant {
4141
* timestamp default value
4242
*/
4343
String CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
44+
/**
45+
* classes path
46+
*/
47+
String CLASSES_PATH = ".target.classes.";
48+
/**
49+
* codegen.setting.json
50+
*/
51+
String SETTING_JSON = "codegen.setting.json";
52+
/**
53+
* java file suffix
54+
*/
55+
String JAVA_SUFFIX = ".java";
4456
}

api-boot-project/api-boot-maven-plugins/api-boot-mybatis-enhance-maven-codegen/src/main/java/org/minbox/framework/api/boot/maven/plugin/mybatis/enhance/codegen/builder/impl/EntityClassBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,5 @@ else if (JavaTypeEnum.TYPE_STRING.getShortName().equals(column.getJavaType())) {
178178
}
179179
return !StringUtils.isEmpty(defaultValue) ? String.format(pattern, defaultValue) : defaultValue;
180180
}
181+
181182
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.expression;
19+
20+
import com.alibaba.fastjson.JSON;
21+
import com.gitee.hengboy.builder.core.database.model.Column;
22+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.EnhanceCodegenConstant;
23+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.wrapper.ClassBuilderWrapper;
24+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.setting.CodegenSetting;
25+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.template.model.Template;
26+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.template.variable.CodegenTemplateVariable;
27+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.tools.DateFormatTools;
28+
import org.springframework.util.ObjectUtils;
29+
30+
import java.util.Iterator;
31+
import java.util.List;
32+
33+
/**
34+
* variable expression
35+
*
36+
* @author 恒宇少年 - 于起宇
37+
* <p>
38+
* DateTime:2019-06-04 10:07
39+
* Blog:http://blog.yuqiyu.com
40+
* WebSite:http://www.jianshu.com/u/092df3f77bca
41+
* Gitee:https://gitee.com/hengboy
42+
* GitHub:https://github.com/hengboy
43+
*/
44+
public class VariableExpression {
45+
/**
46+
* #template.package.name
47+
*/
48+
private static final String TEMPLATE_PACKAGE_NAME = "#%s.name";
49+
/**
50+
* #template.package.position
51+
*/
52+
private static final String TEMPLATE_PACKAGE_POSITION = "#%s.position";
53+
54+
/**
55+
* init variable map
56+
*
57+
* @param wrapper class builder wrapper
58+
* @param codegenSettingJson codegen setting json
59+
*/
60+
public static CodegenSetting initVariable(ClassBuilderWrapper wrapper, String codegenSettingJson) {
61+
CodegenTemplateVariable.VARIABLES.put(CodegenTemplateVariable.NOW, DateFormatTools.formatTime(System.currentTimeMillis(), DateFormatTools.YYYY_MM_DD_HH_MM_SS));
62+
CodegenTemplateVariable.VARIABLES.put(CodegenTemplateVariable.DESC, wrapper.getTable().getRemark());
63+
CodegenTemplateVariable.VARIABLES.put(CodegenTemplateVariable.ENTITY_NAME, wrapper.getTableCamelName());
64+
CodegenTemplateVariable.VARIABLES.put(CodegenTemplateVariable.ENTITY_POSITION, wrapper.getPackageName() + EnhanceCodegenConstant.POINT + wrapper.getTableCamelName());
65+
List<Column> pks = wrapper.getTable().getPrimaryKeys();
66+
if (!ObjectUtils.isEmpty(pks) && pks.size() > 0) {
67+
Column pk = pks.get(0);
68+
CodegenTemplateVariable.VARIABLES.put(CodegenTemplateVariable.ENTITY_ID_TYPE, pk.getJavaType());
69+
}
70+
// parse CodegenSetting
71+
CodegenSetting codegenSetting = JSON.parseObject(codegenSettingJson, CodegenSetting.class);
72+
List<Template> templates = codegenSetting.getTemplates();
73+
if (!ObjectUtils.isEmpty(templates)) {
74+
templates.stream().forEach(template -> {
75+
// entity name
76+
// template java suffix
77+
String templateName = String.format("%s%s", wrapper.getTableCamelName(), template.getJavaSuffix());
78+
// entity package name
79+
// .
80+
// template package
81+
// .
82+
// template name
83+
String templatePosition = String.format("%s%s%s%s%s", wrapper.getPackageName(), EnhanceCodegenConstant.POINT, template.getPackageName(), EnhanceCodegenConstant.POINT, templateName);
84+
85+
CodegenTemplateVariable.VARIABLES.put(String.format(TEMPLATE_PACKAGE_NAME, template.getPackageName()), templateName);
86+
CodegenTemplateVariable.VARIABLES.put(String.format(TEMPLATE_PACKAGE_POSITION, template.getPackageName()), templatePosition);
87+
});
88+
}
89+
90+
// replace variable
91+
codegenSettingJson = convertVariable(codegenSettingJson);
92+
// parse CodegenSetting
93+
return JSON.parseObject(codegenSettingJson, CodegenSetting.class);
94+
}
95+
96+
/**
97+
* 遍历转换
98+
*
99+
* @param content 内容
100+
* @return converter after content
101+
*/
102+
private static String convertVariable(String content) {
103+
Iterator<String> iterator = CodegenTemplateVariable.VARIABLES.keySet().iterator();
104+
while (iterator.hasNext()) {
105+
String key = iterator.next();
106+
content = content.replace(key, CodegenTemplateVariable.VARIABLES.get(key));
107+
}
108+
return content;
109+
}
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.setting;
19+
20+
import lombok.Data;
21+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.template.model.Template;
22+
23+
import java.util.List;
24+
25+
/**
26+
* codegen setting json entity
27+
*
28+
* @author 恒宇少年 - 于起宇
29+
* <p>
30+
* DateTime:2019-06-03 15:07
31+
* Blog:http://blog.yuqiyu.com
32+
* WebSite:http://www.jianshu.com/u/092df3f77bca
33+
* Gitee:https://gitee.com/hengboy
34+
* GitHub:https://github.com/hengboy
35+
*/
36+
@Data
37+
public class CodegenSetting {
38+
/**
39+
* note list
40+
*/
41+
private List<String> notes;
42+
/**
43+
* template list
44+
*/
45+
private List<Template> templates;
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.template;
19+
20+
import lombok.Data;
21+
22+
/**
23+
* codegen file
24+
*
25+
* @author 恒宇少年 - 于起宇
26+
* <p>
27+
* DateTime:2019-06-03 15:36
28+
* Blog:http://blog.yuqiyu.com
29+
* WebSite:http://www.jianshu.com/u/092df3f77bca
30+
* Gitee:https://gitee.com/hengboy
31+
* GitHub:https://github.com/hengboy
32+
*/
33+
@Data
34+
public class CodegenFile {
35+
/**
36+
* file path
37+
*/
38+
private String packageName;
39+
/**
40+
* file name
41+
*/
42+
private String fileName;
43+
/**
44+
* file content
45+
*/
46+
private String javaContent;
47+
}

0 commit comments

Comments
 (0)