前言

    代码生成器,对于我们提升开发效率还是非常有价值的,快速生成代码和避免手误操作,能给我们减少了很多麻烦,又多了一点摸鱼的时间[偷笑]。
    以前我用的比较多的是easy code,但是需要安装插件,配置模版,所以多少还是有些不方便。
    自从项目改用mybatis-plus后,mybatis-plus-generator就成了它的替代品,很方便,也很灵活,个人推荐使用。

    下面我们使用一个Springboot多module的项目来做下演示。

使用教程

项目结构

下面的tree图,是我们本次演示的项目module和package结构,也是我们生成代码后的想要的效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
pro-mall
├── pom.xml
├── application
│   ├── pom.xml
│   └── src
├── common
│   ├── pom.xml
│   └── src
├── mapper
│   ├── pom.xml
│   └── src.main
│   ├── java
│   │   └── cn.soilove.expro
│   │   ├── mapper
│   │   │ └── user
│   │   │   └── UserMapper.java
│   │   └── model
│   │   └── user
│   │   └── User.java
│   └── resources
│   └── mapper
│   └── user
│   └── UserMapper.xml
├── service
│   ├── pom.xml
│   └── src.main
│   └── java
│   └── cn.soilove.expro
│   └── service
│   └── user
│   ├── UserService.java
│   └── impl
│   └── UserServiceImpl.java
├── web-admin
│   ├── pom.xml
│   └── src
├── web-api
│   ├── pom.xml
│   └── src
└── web-open-api
├── pom.xml
└── src
引入POM依赖

第一步,除开mybatis-plus正常引用的包,我们还需要引入generator和velocity,来提供代码和模版的生成。

1
2
3
4
5
6
7
8
9
10
11
12
13
<mybatis-plus.version>3.4.0</mybatis-plus.version>
<velocity.version>2.2</velocity.version>

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>${velocity.version}</version>
</dependency>
编写工具类

然后,编写工具类,直接运行生成想要的代码。下面的java工具类是通过官网代码示例改造而来,支持多module指定。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package cn.soilove.expro.framework.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.LikeTable;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
* MyBatisPlusGenerator 代码生成工具
*
* @author: Chen GuoLin
* @create: 2020-02-11 11:57
**/
public class MyBatisPlusGenerator {

/**
* 数据库链接信息
*/
private static final class mysql{
public static final String url = "jdbc:mysql://k.32e.co:3306/ex-pro?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&";
public static final String driver_name = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = "xxxxx";
}

/**
* module子模块相对路径
*/
private static final class module_path{
/**
* service类所在module
*/
public static final String service = "/service";
/**
* 表实体对象所在module
*/
public static final String entity = "/mapper";
/**
* mapper接口和xml文件所在module
*/
public static final String mapper = "/mapper";
}

/**
* 项目java包路径
*/
private static final String base_path = "cn.soilove.expro";

/**
* 类的作者注释
*/
private static final String author = "Chen GuoLin";


public static void main(String[] args) {
String projectPath = System.getProperty("user.dir");

String bizName = scanner("业务名称");
String[] tableNames = scanner("表名(多个英文逗号分隔)").split(",");

// 代码生成器
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator.setGlobalConfig(buildGlobalConfig());
autoGenerator.setDataSource(buildDataSourceConfig());
autoGenerator.setPackageInfo(buildPackageConfig(projectPath,bizName));
autoGenerator.setCfg(buildInjectionConfig(projectPath, bizName));
autoGenerator.setTemplate(buildTemplateConfig());
autoGenerator.setStrategy(buildStrategyConfig(tableNames));
autoGenerator.execute();
}

/**
* 读取控制台内容信息
*/
private static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
System.out.println(("请输入" + tip + ":"));
if (scanner.hasNext()) {
String next = scanner.next();
if (StringUtils.isNotEmpty(next)) {
return next;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}

/**
* 全局配置
* @return
*/
private static com.baomidou.mybatisplus.generator.config.GlobalConfig buildGlobalConfig() {
com.baomidou.mybatisplus.generator.config.GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setAuthor(author);
globalConfig.setOpen(false);
globalConfig.setSwagger2(false);
globalConfig.setBaseResultMap(false);
globalConfig.setFileOverride(true);
globalConfig.setDateType(DateType.ONLY_DATE);
globalConfig.setEntityName("%s");
globalConfig.setMapperName("%sMapper");
globalConfig.setXmlName("%sMapper");
globalConfig.setServiceName("%sService");
globalConfig.setServiceImplName("%sServiceImpl");
globalConfig.setControllerName("%sController");
return globalConfig;
}

/**
* 数据源配置
* @return
*/
private static DataSourceConfig buildDataSourceConfig() {
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setUrl(mysql.url);
dataSourceConfig.setDriverName(mysql.driver_name);
dataSourceConfig.setUsername(mysql.user);
dataSourceConfig.setPassword(mysql.password);
return dataSourceConfig;
}

/**
* 包配置
* @param projectPath
* @param bizName
* @return
*/
private static PackageConfig buildPackageConfig(String projectPath,String bizName) {
PackageConfig packageConfig = new PackageConfig();
packageConfig.setModuleName("");
packageConfig.setParent(base_path);
packageConfig.setEntity("model" + (StringUtils.isNotBlank(bizName) ? "." + bizName : ""));
packageConfig.setService("service"+(StringUtils.isNotBlank(bizName) ? "." + bizName : ""));
packageConfig.setServiceImpl("service"+(StringUtils.isNotBlank(bizName) ? "." + bizName : "")+".impl");
packageConfig.setMapper("mapper"+(StringUtils.isNotBlank(bizName) ? "." + bizName : ""));

Map<String, String> pathInfo = Maps.newHashMap();
pathInfo.put(ConstVal.ENTITY_PATH, calcPath4Entity(projectPath,bizName));
pathInfo.put(ConstVal.MAPPER_PATH, calcPath4Mapper(projectPath,bizName));
pathInfo.put(ConstVal.XML_PATH, calcPath4Xml(projectPath,bizName));
pathInfo.put(ConstVal.SERVICE_PATH, calcPath4Service(projectPath,bizName));
pathInfo.put(ConstVal.SERVICE_IMPL_PATH, calcPath4ServiceImpl(projectPath,bizName));
packageConfig.setPathInfo(pathInfo);
return packageConfig;
}

/**
* 设置路径 - model.java
* @param projectPath
* @param bizName
* @return
*/
private static String calcPath4Entity(String projectPath,String bizName){
return projectPath + module_path.entity + "/src/main/java/" + base_path.replaceAll("\\.","/") + "/model/" + bizName + "/";
}
/**
* 设置路径 - mapper.java
* @param projectPath
* @param bizName
* @return
*/
private static String calcPath4Mapper(String projectPath, String bizName){
return projectPath + module_path.mapper + "/src/main/java/" + base_path.replaceAll("\\.","/") + "/mapper/" + bizName + "/";
}
/**
* 设置路径 - service.java
* @param projectPath
* @param bizName
* @return
*/
private static String calcPath4Service(String projectPath, String bizName){
return projectPath + module_path.service + "/src/main/java/" + base_path.replaceAll("\\.","/") + "/service/" + bizName + "/";
}
/**
* 设置路径 - serviceImpl.java
* @param projectPath
* @param bizName
* @return
*/
private static String calcPath4ServiceImpl(String projectPath, String bizName){
return projectPath + module_path.service + "/src/main/java/" + base_path.replaceAll("\\.","/") + "/service/" + bizName + "/impl/";
}
/**
* 设置路径 - model.xml
* @param projectPath
* @param bizName
* @return
*/
private static String calcPath4Xml(String projectPath, String bizName){
return projectPath + module_path.mapper + "/src/main/resources/mapper/" + bizName + "/";
}

/**
* 模板配置
* @return
*/
private static TemplateConfig buildTemplateConfig() {
TemplateConfig templateConfig = new TemplateConfig();
//可以对controller、service、entity模板进行配置
//mapper.xml模板需单独配置
return templateConfig;
}

/**
* 策略配置
* @param tableNames
* @return
*/
private static StrategyConfig buildStrategyConfig(String[] tableNames) {
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
strategyConfig.setEntityLombokModel(true);
strategyConfig.setRestControllerStyle(true);
// 当表名中带*号时可以启用通配符模式
if (tableNames.length == 1 && tableNames[0].contains("*")) {
String[] likeStr = tableNames[0].split("_");
String likePrefix = likeStr[0] + "_";
strategyConfig.setLikeTable(new LikeTable(likePrefix));
} else {
strategyConfig.setInclude(tableNames);
}
// 自动填充时间字段
List<TableFill> tableFillList = new ArrayList<>();
tableFillList.add(new TableFill("create_time", FieldFill.INSERT));
tableFillList.add(new TableFill("update_time", FieldFill.INSERT_UPDATE));
strategyConfig.setTableFillList(tableFillList);
return strategyConfig;
}

/**
* 自定义配置
* @param projectPath
* @param bizName
* @return
*/
private static InjectionConfig buildInjectionConfig(String projectPath, String bizName) {
// 自定义配置
InjectionConfig injectionConfig = new InjectionConfig() {
@Override
public void initMap() {
// 可用于自定义属性
}
};
// 模板引擎是Velocity
String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "mapper/src/main/resources/mapper/" + bizName
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
injectionConfig.setFileOutConfigList(focList);
return injectionConfig;
}
}


最后,我们直接运行这个java类,在控制台输入 业务模块名称 和 表名,点击回车即可生成。