日期 版本 作者 说明
2022-2-27 V-1.0 孔留锋 文档创建。

概要

简介

​ 基于RuoYi-Vue 3.8.1,框架自带的代码生成器,对有些业务来说无用的信息太多,而且生成的实体类中的字段需要和数据库字段强相关(驼峰命名对应),否则就要对生成的实体类进行改造,属性中添加@TableField主注解进行修改。需要对Mybatis-plus 原始生成器进行改造,适配符合架构所需。

改造

gene模块新建

  • IDEA 右键新建 maven 模块 取名 gene

修改gene 模块 POM文件

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
<parent>
<artifactId>single</artifactId>
<groupId>com.zzsn</groupId>
<version>3.8.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>gene</artifactId>

<description>
Mybatis-plus 原生 代码生成器
</description>

<dependencies>
<dependency>
<groupId>com.zzsn</groupId>
<artifactId>common</artifactId>
</dependency>

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
</dependency>

<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<!-- 阿里数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>

代码生成器

test 模块下新建

DataSourceInfo.calss

1
2
3
4
5
6
7
8
9
10
11
12
13
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DataSourceInfo {
//驱动名称
private String driverClassName;
//连接地址
private String url;
//数据库用户名
private String username;
//数据库密码
private String password;
}

Generator.class

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
@SpringBootTest
public class Generator {
private DataSourceInfo master = new DataSourceInfo("com.mysql.jdbc.Driver"
,"jdbc:mysql://192.168.10.1:3306/core_data_base?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai"
,"root","@123456.");
private DataSourceInfo slave = new DataSourceInfo("com.mysql.jdbc.Driver"
,"jdbc:mysql://192.168.10.1:3306/sentiment_data_base?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai"
,"root","@123456.");

@Test
public void codeGenerator() {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();

// 全局配置
GlobalConfig gc = this.getGlobalConfig(true);
mpg.setGlobalConfig(gc);

// 数据源配置
DataSourceConfig dsc = this.getDataSourceConfig(applets);
mpg.setDataSource(dsc);

// 包配置
PackageConfig pc = this.getPackageConfig("com.zzsn","sentiment");
mpg.setPackageInfo(pc);

// 自定义配置
InjectionConfig cfg = this.getInjectionConfig(pc);
mpg.setCfg(cfg);

//模板,
TemplateConfig templateConfig = this.getTemplateConfig(false);
mpg.setTemplate(templateConfig);

// 策略配置
StrategyConfig strategy = this.getStrategyConfig(pc
,new String[]{"sys_"}
,new String[]{"sys_test"
});
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}


public GlobalConfig getGlobalConfig(Boolean fileOverride) {
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");

//生成文件的输出目录
gc.setOutputDir(projectPath + "/src/main/java");

//TODO :是否覆盖已有文件
//是否覆盖已有文件,默认false
gc.setFileOverride(fileOverride);

//是否打开输出目录.默认值:true
//gc.setOpen(false)

//是否在xml中添加二级缓存配置,默认值:`false
//gc.isEnableCache("true");

//开发人员,开发人员
gc.setAuthor("kongliufeng");
gc.setOpen(false);

//开启 Kotlin 模式,默认值:false
//gc.setKotlin(true);

//开启 swagger2 模式 ,默认值:false
//gc.setSwagger2(true);

//开启 BaseResultMap,默认false
gc.setBaseResultMap(true);

//开启 baseColumnList,默认值:false
gc.setBaseColumnList(true);

//时间类型对应策略,默认值:TIME_PACK
//gc.setDateType(DateType.TIME_PACK);

//如下配置 %s 为占位符
//实体命名方式.默认值:null 例如:%sEntity 生成 UserEntity
//gc.setEntityName();

//mapper 命名方式,默认值:null 例如:%sDao 生成 UserDao
//gc.setMapperName();

//Mapper xml 命名方式,默认值:null 例如:%sDao 生成 UserDao
//gc.setXmlName();

//Mapper xml 命名方式,默认值:null 例如:%sDao 生成 UserDao.xml
//gc.setXmlName();

//service 命名方式,默认值:null 例如:%sBusiness 生成 UserBusiness
//gc.setServiceName();

//service impl 命名方式,默认值:null 例如:%sBusinessImpl 生成 UserBusinessImpl
//gc.setServiceImplName();

//controller 命名方式,默认值:null 例如:%sAction 生成 UserA
//gc.setControllerName();

//指定生成的主键的ID类型,默认值:null
gc.setIdType(IdType.ASSIGN_ID);
return gc;
}

public DataSourceConfig getDataSourceConfig(DataSourceInfo info) {
DataSourceConfig dsc = new DataSourceConfig();

//数据库信息查询类,默认由 dbType 类型决定选择对应数据库内置实现,
//实现 IDbQuery 接口自定义数据库查询 SQL 语句 定制化返回自己需要的内容
//dsc.setDbQuery();

//数据库类型,该类内置了常用的数据库类型【必须】
//dsc.setDbType(DbType.MYSQL);
if(info.getDriverClassName().startsWith("com.mysql")){
dsc.setDbType(DbType.MYSQL);
}else if(info.getDriverClassName().startsWith("oracle.jdbc")){
dsc.setDbType(DbType.ORACLE);
}

//数据库 schema name,例如 PostgreSQL 可指定为 public
//dsc.setSchemaName();

//类型转换,默认由 dbType 类型决定选择对应数据库内置实现
//实现 ITypeConvert 接口自定义数据库 字段类型 转换为自己需要的 java 类型,
// 内置转换类型无法满足可实现 IColumnType 接口自定义
// dsc.setTypeConvert();

//驱动连接的URL
dsc.setUrl(info.getUrl());
//驱动名称
dsc.setDriverName(info.getDriverClassName());
//数据库连接用户名
dsc.setUsername(info.getUsername());
//数据库连接密码
dsc.setPassword(info.getPassword());
return dsc;
}

public PackageConfig getPackageConfig(String parent ,String moduleName){
//String data = "master";
PackageConfig pc = new PackageConfig();
//父包名
pc.setParent(parent);

//模块名
pc.setModuleName(moduleName);

//Entity包名
pc.setEntity("domain");

//Service包名
//pc.setService("service."+data);

//Service Impl包名
//pc.setServiceImpl("service.impl"+data);

//Mapper包名
//pc.setMapper();

//Mapper XML包名
//pc.setXml();

//Controller包名
//pc.setController();

//路径配置信息
//pc.setPathInfo();
return pc;
}

public InjectionConfig getInjectionConfig(PackageConfig pc){

//map,自定义返回配置 Map 对象,该对象可以传递到模板引擎通过 cfg.xxx 引用

InjectionConfig cfg = new InjectionConfig() {
//注入自定义 Map 对象(注意需要setMap放进去)
@Override
public void initMap() {

}
};

//fileOutConfigList,自定义输出文件
//配置 FileOutConfig 指定模板文件、输出文件达到自定义文件生成目的
String projectPath = System.getProperty("user.dir");

//如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";

// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";

List<FileOutConfig> focList = new ArrayList<>();

focList.add(new FileOutConfig(templatePath) {
//这里重写了模板生成的路径,在templateConfig.setXml(null);
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 ,
// 如果你 Entity 设置了前后缀、
// 此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});

cfg.setFileOutConfigList(focList);


//fileCreate,自定义判断是否创建文件
//实现 IFileCreate 接口,该配置用于判断某个类是否需要覆盖创建,
// 当然你可以自己实现差异算法 merge 文件

return cfg;
}

public TemplateConfig getTemplateConfig(Boolean noGeneratorController){
TemplateConfig templateConfig = new TemplateConfig();
/**
* private String entity = "/templates/entity.java";
* private String entityKt = "/templates/entity.kt";
* private String service = "/templates/service.java";
* private String serviceImpl = "/templates/serviceImpl.java";
* private String mapper = "/templates/mapper.java";
* private String xml = "/templates/mapper.xml";
* private String controller = "/templates/controller.java";
*/

//entity,Java 实体类模板
//默认private String entity = "/templates/entity.java";
//templateConfig.setEntity();

//entityKt,Kotin 实体类模板
//默认private String entityKt = "/templates/entity.kt";
//templateConfig.setEntityKt();

//Service 类模板
//private String service = "/templates/service.java";
//templateConfig.setService();

//Service impl 实现类模板
//private String serviceImpl = "/templates/serviceImpl.java";
//templateConfig.setServiceImpl();

//mapper 模板
//private String mapper = "/templates/mapper.java";
//templateConfig.setMapper();

//mapper xml 模板
//private String xml = "/templates/mapper.xml";
templateConfig.setXml(null);

//controller 控制器模板
//private String controller = "/templates/controller.java";
if(noGeneratorController){
templateConfig.setController(null);
}
return templateConfig;
}

public StrategyConfig getStrategyConfig(PackageConfig pc,String[] tablePrefixs,String[] includes){
StrategyConfig strategy = new StrategyConfig();
//是否大写命名,默认false
//strategy.setCapitalMode(true);

//是否跳过视图,默认false
//strategy.setSkipView(true);

//数据库表映射到实体的命名策略
strategy.setNaming(NamingStrategy.underline_to_camel);

//数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
strategy.setColumnNaming(NamingStrategy.underline_to_camel);

//表前缀
//strategy.setTablePrefix(pc.getModuleName() + "_");
strategy.setTablePrefix(tablePrefixs);

//字段前缀,fieldPrefix
//strategy.setFieldPrefix();

//自定义继承的Entity类全称,带包名
//strategy.setSuperEntityClass();

//自定义基础的Entity类,公共字段
//strategy.setSuperEntityColumns();

//自定义继承的Mapper类全称,带包名
//strategy.setSuperMapperClass();

//自定义继承的Service类全称,带包名
//strategy.setSuperServiceClass();

//自定义继承的ServiceImpl类全称,带包名
//strategy.setSuperServiceImplClass();

//自定义继承的Controller类全称,带包名
//strategy.setSuperControllerClass();

/**
* enableSqlFilter(since 3.3.1)
*/
//默认激活进行sql模糊表名匹配,关闭之后likeTable与notLikeTable将失效
// ,include和exclude将使用内存过滤
//如果有sql语法兼容性问题的话,请手动设置为false
//已知无法使用:MyCat中间件,
//strategy.isEnableSqlFilter();

//include,需要包含的表名,
// 当enableSqlFilter为false时,允许正则表达式(与exclude二选一配置
strategy.setInclude(includes);

//自3.3.0起,模糊匹配表名(与notLikeTable二选一配置)
//strategy.getLikeTable();

//需要排除的表名,当enableSqlFilter为false时,允许正则表达式
strategy.setExclude();

//自3.3.0起,模糊排除表名
//strategy.getNotLikeTable();

//【实体】是否生成字段常量(默认 false)
//strategy.setEntityColumnConstant();

//【实体】是否为构建者模型(默认 false),自3.3.2开始更名为 chainModel
//strategy.setEntityBuilderModel();


//chainModel(since 3.3.2)

//【实体】是否为链式模型(默认 false)
//strategy.setChainModel();

//【实体】是否为lombok模型(默认 false)
//3.3.2以下版本默认生成了链式模型,
// 3.3.2以后,默认不生成,如有需要,请开启 chainMode
strategy.setEntityLombokModel(true);

//Boolean类型字段是否移除is前缀(默认 false)
//strategy.setEntityBooleanColumnRemoveIsPrefix();

//生成 @RestController 控制器
strategy.setRestControllerStyle(true);

//驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);

//entityTableFieldAnnotationEnable
strategy.setEntityTableFieldAnnotationEnable(true);

//乐观锁属性名称
//strategy.setVersionFieldName();

//逻辑删除属性名称
//strategy.setLogicDeleteFieldName();

//表填充字段
//strategy.setTableFillList();

return strategy;
}
}

试用方式

  • Generator.codeGenerator方法修改如下:
  • this.getPackageConfig(“com.zzsn”,”sentiment”)–》设置包名,模块名称
  • this.getTemplateConfig(false)–》指定是否生成controller类
  • this.getStrategyConfig()–》指定去除的前缀,指定生成的表名称
  • 运行Generator.codeGenerator方法
  • 生成完毕后会在gene模块下ser.mian 中生成文件,移动到各个模块即可