Skip to content

Commit 872350a

Browse files
committed
ApiBoot Mybatis Enhance Codegen 初版发布.
1 parent 2e2fdcb commit 872350a

File tree

10 files changed

+907
-0
lines changed

10 files changed

+907
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright [2019] [恒宇少年 - 于起宇]
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
~
17+
-->
18+
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<parent>
23+
<artifactId>api-boot-maven-plugins</artifactId>
24+
<groupId>org.minbox.framework</groupId>
25+
<version>2.0.7.RC1</version>
26+
</parent>
27+
<modelVersion>4.0.0</modelVersion>
28+
<packaging>maven-plugin</packaging>
29+
<properties>
30+
<main.basedir>${basedir}/../../..</main.basedir>
31+
</properties>
32+
<artifactId>api-boot-mybatis-enhance-maven-codegen</artifactId>
33+
<dependencies>
34+
<!--Codegen-->
35+
<dependency>
36+
<groupId>com.mysema.codegen</groupId>
37+
<artifactId>codegen</artifactId>
38+
</dependency>
39+
<!--Code Builder-->
40+
<dependency>
41+
<groupId>com.gitee.hengboy</groupId>
42+
<artifactId>code-builder-core</artifactId>
43+
</dependency>
44+
<!--Mybatis Enhance Core-->
45+
<dependency>
46+
<groupId>com.gitee.hengboy</groupId>
47+
<artifactId>mybatis-enhance-core</artifactId>
48+
</dependency>
49+
<!--Mybatis Enhance Dsl-->
50+
<dependency>
51+
<groupId>com.gitee.hengboy</groupId>
52+
<artifactId>mybatis-enhance-dsl</artifactId>
53+
</dependency>
54+
<!--Lombok-->
55+
<dependency>
56+
<groupId>org.projectlombok</groupId>
57+
<artifactId>lombok</artifactId>
58+
</dependency>
59+
</dependencies>
60+
61+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
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;
19+
20+
import com.gitee.hengboy.builder.common.CodeBuilderProperties;
21+
import com.gitee.hengboy.builder.common.enums.DbTypeEnum;
22+
import com.gitee.hengboy.builder.core.database.DataBase;
23+
import com.gitee.hengboy.builder.core.database.DataBaseFactory;
24+
import com.gitee.hengboy.builder.core.database.model.Table;
25+
import org.apache.maven.plugin.AbstractMojo;
26+
import org.apache.maven.plugin.MojoExecutionException;
27+
import org.apache.maven.plugin.MojoFailureException;
28+
import org.apache.maven.plugins.annotations.Execute;
29+
import org.apache.maven.plugins.annotations.LifecyclePhase;
30+
import org.apache.maven.plugins.annotations.Mojo;
31+
import org.apache.maven.plugins.annotations.Parameter;
32+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.ClassBuilder;
33+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.ClassBuilderFactory;
34+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.impl.DynamicEntityClassBuilder;
35+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.impl.EntityClassBuilder;
36+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.wrapper.ClassBuilderWrapper;
37+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.tools.CamelTools;
38+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.writer.JavaClassWriter;
39+
import org.springframework.util.ObjectUtils;
40+
41+
import java.io.File;
42+
import java.util.Arrays;
43+
import java.util.List;
44+
45+
/**
46+
* ApiBoot Mybatis Enhance Codegen Maven Plugin
47+
* 1. Generating data entities
48+
* 2. Generating dynamic query entities
49+
* 3. Generate Mapper files
50+
*
51+
* @author:恒宇少年 - 于起宇
52+
* <p>
53+
* DateTime:2019-04-29 16:28
54+
* Blog:http://blog.yuqiyu.com
55+
* WebSite:http://www.jianshu.com/u/092df3f77bca
56+
* Gitee:https://gitee.com/hengboy
57+
* GitHub:https://github.com/hengboy
58+
*/
59+
@Mojo(name = "generator", defaultPhase = LifecyclePhase.COMPILE)
60+
@Execute(phase = LifecyclePhase.COMPILE)
61+
public class ApiBootMybatisEnhanceCodegen extends AbstractMojo {
62+
/**
63+
* file suffix
64+
*/
65+
private static final String FILE_SUFFIX = ".java";
66+
/**
67+
* Whether to execute automatically
68+
* Default not to execute
69+
*/
70+
@Parameter(defaultValue = "false", required = true)
71+
private boolean execute;
72+
/**
73+
* database type
74+
*/
75+
@Parameter(defaultValue = "MySQL")
76+
private DbTypeEnum dbType;
77+
/**
78+
* database name
79+
*/
80+
@Parameter(required = true)
81+
private String dbName;
82+
/**
83+
* database url
84+
* for example:jdbc:mysql://xxx.xx.xx.xxx:3306
85+
*/
86+
@Parameter(required = true)
87+
private String dbUrl;
88+
/**
89+
* database username
90+
*/
91+
@Parameter(required = true)
92+
private String dbUserName;
93+
/**
94+
* database password
95+
*/
96+
@Parameter(required = true)
97+
private String dbPassword;
98+
/**
99+
* database driver class name
100+
*/
101+
@Parameter
102+
private String dbDriverClassName;
103+
/**
104+
* table name pattern
105+
* Used to get a list of tables
106+
*/
107+
@Parameter(defaultValue = "%")
108+
private String tableNamePattern;
109+
/**
110+
* project base dir
111+
*/
112+
@Parameter(defaultValue = "${basedir}")
113+
private String projectBaseDir;
114+
115+
/**
116+
* generator class target dir
117+
*/
118+
@Parameter(defaultValue = "target.generated-sources.java")
119+
private String targetDir;
120+
/**
121+
* java file package name
122+
*/
123+
@Parameter(required = true)
124+
private String packageName;
125+
126+
@Override
127+
public void execute() throws MojoExecutionException, MojoFailureException {
128+
if (!execute) {
129+
getLog().warn("Automatic code generation is not turned on. If you need to generate entity classes, configure 【execute=true】");
130+
return;
131+
}
132+
// code builder properties
133+
CodeBuilderProperties codeBuilderProperties = CodeBuilderProperties.builder().dbType(dbType).dbName(dbName).dbUrl(dbUrl).dbUserName(dbUserName).dbPassword(dbPassword).dbDriverClassName(dbDriverClassName).build();
134+
135+
// get database instance by DbTypeEnum
136+
DataBase dataBase = DataBaseFactory.newInstance(codeBuilderProperties);
137+
138+
List<String> tableNames = getTableNames(dataBase);
139+
140+
tableNames.stream().forEach(tableName -> {
141+
142+
getLog().info("Execution table: 【" + tableName + "】 entity creation.");
143+
144+
// get table
145+
Table table = dataBase.getTable(tableName);
146+
147+
// execute the builders created
148+
// ClassBuilder implementation classes can be added
149+
Class<? extends ClassBuilder>[] builders = new Class[]{
150+
EntityClassBuilder.class,
151+
DynamicEntityClassBuilder.class
152+
};
153+
154+
// Encapsulated objects needed to perform generation
155+
ClassBuilderWrapper wrapper = ClassBuilderWrapper.builder().packageName(packageName).tableCamelName(CamelTools.upper(tableName)).table(table).build();
156+
157+
// execute generator
158+
Arrays.stream(builders).forEach(builderClass -> {
159+
ClassBuilder builder = ClassBuilderFactory.newInstance(builderClass, wrapper);
160+
if (!ObjectUtils.isEmpty(builder)) {
161+
// setting class prefix
162+
wrapper.setTableCamelName(builder.getDefaultPrefix() + wrapper.getTableCamelName());
163+
// class file path
164+
String classPath = getNewClassPath(wrapper.getTableCamelName());
165+
// class content
166+
String classContent = builder.getClassContent();
167+
// invoke content write
168+
JavaClassWriter.writeToJavaFile(classPath, classContent);
169+
}
170+
});
171+
});
172+
}
173+
174+
/**
175+
* get table name list
176+
*
177+
* @param dataBase data base instance
178+
* @return table name list
179+
*/
180+
private List<String> getTableNames(DataBase dataBase) {
181+
return dataBase.getTableNames(tableNamePattern);
182+
}
183+
184+
/**
185+
* get generator dir
186+
*
187+
* @return generator dir
188+
*/
189+
private String getGeneratorDir() {
190+
// java file base dir
191+
String baseDir = String.format("%s%s%s", projectBaseDir, File.separator, targetDir.replace(".", File.separator));
192+
// package dir
193+
String packageDir = String.format("%s%s%s", baseDir, File.separator, packageName.replace(".", File.separator));
194+
// create dirs
195+
File file = new File(packageDir);
196+
if (!file.exists()) {
197+
file.mkdirs();
198+
}
199+
return packageDir;
200+
}
201+
202+
/**
203+
* get generator class path
204+
*
205+
* @param entityClassName entity class name
206+
* @return class path
207+
*/
208+
private String getNewClassPath(String entityClassName) {
209+
return String.format("%s%s%s%s", getGeneratorDir(), File.separator, entityClassName, FILE_SUFFIX);
210+
}
211+
}
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.builder;
19+
20+
/**
21+
* Entity Class Builder
22+
*
23+
* @author:恒宇少年 - 于起宇
24+
* <p>
25+
* DateTime:2019-04-29 17:10
26+
* Blog:http://blog.yuqiyu.com
27+
* WebSite:http://www.jianshu.com/u/092df3f77bca
28+
* Gitee:https://gitee.com/hengboy
29+
* GitHub:https://github.com/hengboy
30+
*/
31+
public interface ClassBuilder {
32+
/**
33+
* get class content
34+
* use by generator class file
35+
*
36+
* @return class content
37+
*/
38+
String getClassContent();
39+
40+
/**
41+
* get class file default prefix
42+
*
43+
* @return
44+
*/
45+
String getDefaultPrefix();
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.builder;
19+
20+
import org.minbox.framework.api.boot.maven.plugin.mybatis.enhance.codegen.builder.wrapper.ClassBuilderWrapper;
21+
22+
import java.lang.reflect.Constructor;
23+
24+
/**
25+
* Class Builder Factory
26+
*
27+
* @author:恒宇少年 - 于起宇
28+
* <p>
29+
* DateTime:2019-04-30 09:00
30+
* Blog:http://blog.yuqiyu.com
31+
* WebSite:http://www.jianshu.com/u/092df3f77bca
32+
* Gitee:https://gitee.com/hengboy
33+
* GitHub:https://github.com/hengboy
34+
*/
35+
public class ClassBuilderFactory {
36+
/**
37+
* instantiation ClassBuilder sub class
38+
*
39+
* @param impClass sub class
40+
* @param wrapper ClassBuilder Wrapper
41+
* @return ClassBuilder Sub Instance
42+
*/
43+
public static ClassBuilder newInstance(Class<? extends ClassBuilder> impClass, ClassBuilderWrapper wrapper) {
44+
try {
45+
Constructor constructor = impClass.getDeclaredConstructor(ClassBuilderWrapper.class);
46+
return (ClassBuilder) constructor.newInstance(wrapper);
47+
} catch (Exception e) {
48+
e.printStackTrace();
49+
return null;
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)