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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package cn.chnmuseum.party.common.mybatis;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
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.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 org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CodeGenerator {
// ############################ 自定义配置部分 start ############################
/**
* 框架包
*/
private static final String FRAMEWORK_PACKAGE = "cn.chnmuseum.party";
/**
* 生成的类路径
*/
private String projectPackagePath;
/**
* 项目主包路径
*/
private String parentPackage;
/**
* 包名称
*/
private String packageController = "controller";
/**
* 模块名称
*/
private String moduleName;
/**
* 作者
*/
private String author;
/**
* 主键数据库列名称
*/
private String pkIdColumnName = "ID";
/**
* 代码生成策略 true:All/false:SIMPLE
* 0. SIMPLE 生成最基本的代码
* 1. NORMAL 生成普通代码
* 2. ALL 生成所有的代码
*/
private GeneratorStrategy generatorStrategy = GeneratorStrategy.NORMAL;
/**
* 生成策略
*/
public enum GeneratorStrategy {
SIMPLE, NORMAL, ALL
}
/**
* 分页列表查询是否排序 true:有排序参数/false:无
*/
private boolean pageListOrder = false;
/**
* 是否生成validation校验,true:生成/false:不生成
*/
private boolean paramValidation = true;
/**
* 是否生成查询参数
*/
private boolean generatorPageParam;
/**
* 是否生成查询VO
*/
private boolean generatorQueryVo;
/**
* 是否生成Shiro RequiresPermissions 注解
*/
private boolean requiresPermissions = true;
/**
* 公共父包
*/
private String commonParentPackage;
/**
* 分页查询参数父类
*/
private String superPageParam;
/**
* 分页排序查询参数父类
*/
private String superPageOrderParam;
/**
* 分页工具类路径
*/
private String commonPageUtil;
/**
* 实体父类实体列表
*/
private String[] superEntityCommonColumns;
/**
* 公共id参数路径
*/
private String commonIdParam;
/**
* 公共结果集路径
*/
private String commonApiResult;
/**
* 公共排序枚举
*/
private String commonOrderEnum;
/**
* 公共排序查询参数
*/
private String commonOrderPageParam;
/**
* 公共分页对象
*/
private String commonPaging;
// ############################ 自定义配置部分 end ############################
/**
* 初始化变量
*/
public void init() {
this.commonParentPackage = FRAMEWORK_PACKAGE + ".common";
this.superPageParam = this.commonParentPackage + ".pagination.BasePageParam";
this.superPageOrderParam = FRAMEWORK_PACKAGE + ".pagination.BasePageOrderParam";
this.superEntityCommonColumns = new String[]{};
// 公共类包路径
this.commonIdParam = this.commonParentPackage + ".param.IdParam";
this.commonApiResult = this.commonParentPackage + ".api.ApiResult";
this.commonOrderEnum = this.commonParentPackage + ".enums.OrderEnum";
this.commonPaging = FRAMEWORK_PACKAGE + ".pagination.Paging";
this.commonPageUtil = FRAMEWORK_PACKAGE + ".pagination.PageUtil";
// 如果包路径为空,转换包名称路径
if (StringUtils.isNotBlank(parentPackage)) {
this.projectPackagePath = parentPackage.replaceAll("\\.", File.separator);
}
}
/**
* <p>
* MySQL 生成演示
* </p>
*/
public void generator(String projectPath, String tableName) {
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(projectPath + "/src/main/java");
gc.setIdType(IdType.ASSIGN_ID);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
gc.setFileOverride(true);
gc.setDateType(DateType.TIME_PACK);
// 不需要ActiveRecord特性的请改为false
gc.setActiveRecord(false);
gc.setSwagger2(true);
// 是否生成 kotlin 代码
gc.setKotlin(false);
// XML 二级缓存
gc.setEnableCache(false);
gc.setAuthor("Danny Lee");
gc.setOpen(false);
// 自定义文件命名,注意 %s 会自动填充表实体属性!
// gc.setEntityName("%s");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setControllerName("%sController");
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setUrl("jdbc:mysql://192.168.110.93:3306/chnmuseum-party?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&allowMultiQueries=true&useSSL=false&serverTimezone=Asia/Shanghai");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("password");
// 设置元数据自定义查询
dsc.setDbQuery(new MySQLQuery());
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName("party");
pc.setParent("cn.chnmuseum")
.setMapper("mapper")
.setService("service")
.setServiceImpl("service.impl")
.setController("web.controller")
.setEntity("model")
.setXml("mapper");
// 自定义需要填充的字段 数据库中的字段
List<TableFill> tableFillList = new ArrayList<>();
tableFillList.add(new TableFill("CREATE_TIME", FieldFill.INSERT));
tableFillList.add(new TableFill("UPDATE_TIME", FieldFill.INSERT_UPDATE));
tableFillList.add(new TableFill("VERSION", FieldFill.INSERT));
// 策略配置
StrategyConfig sc = new StrategyConfig();
//驼峰命名
sc.setCapitalMode(true);
//实体类是否带有lombok风格
sc.setEntityLombokModel(true);
//允许字段自动注解
sc.setEntityTableFieldAnnotationEnable(true);
sc.isEntityTableFieldAnnotationEnable();
//rest风格controller
sc.setRestControllerStyle(true);
//路径加分隔符
sc.setControllerMappingHyphenStyle(true);
sc.setVersionFieldName(GeneratorConstant.VERSION);
sc.setLogicDeleteFieldName(GeneratorConstant.DELETED);
// sc.setTablePrefix("M_");//表前缀
// 表名生成策略(下划线转驼峰命名)
sc.setNaming(NamingStrategy.underline_to_camel);
// 列名生成策略(下划线转驼峰命名)
sc.setColumnNaming(NamingStrategy.underline_to_camel);
// 自定义controller父类
sc.setSuperControllerClass("cn.chnmuseum.party.web.controller.base.BaseController");
//strategy.setSuperEntityColumns("id");//自定义基础的Entity类,公共字段
//自动填充设置
sc.setTableFillList(tableFillList);
//修改替换成你需要的表名,多个表名传数组,允许正则表达式(与exclude二选一配置)
sc.setInclude(tableName);
InjectionConfig injectionConfig = new InjectionConfig() {
@Override
public void initMap() {
// Set<String> tablePrefixSet = this.getConfig().getStrategyConfig().getInclude();
String entityName = tableName;
String camelTableName = underlineToCamel(entityName);
String pascalTableName = underlineToPascal(entityName);
String colonTableName = underlineToColon(entityName);
Map<String, Object> map = new HashMap<>();
map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
// 查询参数包路径
String paramPackage = parentPackage + StringPool.DOT + pc.getModuleName() + ".param";
map.put("paramPackage", paramPackage);
// 查询参数类路径
map.put("pageParamPath", paramPackage + StringPool.DOT + pascalTableName + "PageParam");
// 查询参数共公包路径
map.put("superPageParamPath", superPageParam);
map.put("superPageOrderParamPath", superPageOrderParam);
map.put("commonPageUtilPath", commonPageUtil);
// 查询参数共公包路径
map.put("idParamPath", commonIdParam);
// 响应结果包路径
String queryVoPackage = parentPackage + StringPool.DOT + pc.getModuleName() + ".vo";
map.put("queryVoPackage", queryVoPackage);
// 响应结果类路径
map.put("queryVoPath", queryVoPackage + StringPool.DOT + pascalTableName + "QueryVo");
// 实体对象名称
map.put("entityObjectName", camelTableName);
// service对象名称
map.put("serviceObjectName", camelTableName + "Service");
// mapper对象名称
map.put("mapperObjectName", camelTableName + "Mapper");
// 主键ID列名
map.put("pkIdColumnName", pkIdColumnName);
// 主键ID驼峰名称
map.put("pkIdCamelName", underlineToCamel(pkIdColumnName));
// 导入分页类
map.put("paging", commonPaging);
// 导入排序枚举
map.put("orderEnum", commonOrderEnum);
// ApiResult
map.put("apiResult", commonApiResult);
// 分页列表查询是否排序
map.put("pageListOrder", pageListOrder);
// 导入排序查询参数类
map.put("orderPageParamPath", commonOrderPageParam);
// 代码生成策略
map.put("generatorStrategy", generatorStrategy);
// 代码Validation校验
map.put("paramValidation", paramValidation);
// 冒号连接的表名称
map.put("colonTableName", colonTableName);
// 是否生成Shiro RequiresPermissions注解
map.put("requiresPermissions", requiresPermissions);
this.setMap(map);
}
};
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
focList.add(new FileOutConfig("/templates/controller.java.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/src/main/java/cn/wisenergy/chnmuseum/party/web/controller/" + tableInfo.getEntityName() + "Controller" + StringPool.DOT_JAVA;
}
});
injectionConfig.setFileOutConfigList(focList);
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController(null);
templateConfig.setXml(null);
templateConfig.setEntity("/templates/entity.java.vm");
new AutoGenerator()
.setGlobalConfig(gc)
.setDataSource(dsc)
.setCfg(injectionConfig)
.setPackageInfo(pc)
.setStrategy(sc)
.setTemplate(templateConfig)
.execute();
}
/**
* 下划线专程驼峰命名
* sys_user --> sysUser
*
* @param underline
* @return
*/
public static String underlineToCamel(String underline) {
if (StringUtils.isNotBlank(underline)) {
return NamingStrategy.underlineToCamel(underline);
}
return null;
}
/**
* 下划线转换成帕斯卡命名
* sys_user --> SysUser
*
* @param underline
* @return
*/
public static String underlineToPascal(String underline) {
if (StringUtils.isNotBlank(underline)) {
return NamingStrategy.capitalFirst(NamingStrategy.underlineToCamel(underline));
}
return null;
}
/**
* 下划线转换成冒号连接命名
* sys_user --> sys:user
*
* @param underline
* @return
*/
public static String underlineToColon(String underline) {
if (StringUtils.isNotBlank(underline)) {
String string = underline.toLowerCase();
return string.replaceAll("_", ":");
}
return null;
}
}