MP多模块代码生成器模板

从正式进入项目组到接手练习做项目,一直想写一个多模块的代码生成器,方便组内其他成员去自动生成三层架构和实体类等代码,避免重复繁琐的操作。又因为这几天改Bug改到自闭,一直没有时间将其实践起来。因为今天是周末所以早早就起来,结合官网的单模块代码生成器,以及一上午仅仅找到一篇类似的博客进行改写,完成了多模块代码生成的模板。以下直接放代码,记录一下,方便以后工作直接套用。

代码

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
package com.studio.utils;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.io.File;
import java.sql.Driver;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
* <h3>mpgenerator</h3>
* <p></p>
*
* @author : zhengyue
* @date : 2020-03-13 17:13
**/
public class CodeGenerator {
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}

public static void main(String[] args) {
// String [] tableNames = new String[]{"cms_user"};
String[] tableNames = scanner("表名,多个英文逗号分割").split(",");

String [] modules = new String[]{"studio_pojo", "studio_dao", "studio_service", "studio_controller"};//项目模块名,需自定义
for (String module : modules) {
moduleGenerator(module,tableNames);
}
}

private static void moduleGenerator(String module,String [] tableNames){

GlobalConfig globalConfig = getGlobalConfig(module);// 全局配置

DataSourceConfig dataSourceConfig = getDataSourceConfig();// 数据源配置

PackageConfig packageConfig = getPackageConfig(module);// 包配置

StrategyConfig strategyConfig = getStrategyConfig(tableNames);// 策略配置

TemplateConfig templateConfig = getTemplateConfig(module);// 配置模板

InjectionConfig cfgConfig = getCfgConfig(); //自定义导出设置

new AutoGenerator()
.setGlobalConfig(globalConfig)
.setDataSource(dataSourceConfig)
.setPackageInfo(packageConfig)
.setStrategy(strategyConfig)
.setTemplate(templateConfig)
.setCfg(cfgConfig)
.execute();

}

private static InjectionConfig getCfgConfig() {
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};

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) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return new File("studio_dao").getAbsolutePath()+ "/src/main/resources/mapper/" + tableInfo.getEntityName()
+ "Mapper" + StringPool.DOT_XML;
}
});

cfg.setFileOutConfigList(focList);
return cfg;
}

private static TemplateConfig getTemplateConfig(String module) {
TemplateConfig templateConfig = new TemplateConfig();
//studio_pojo", "studio_dao", "studio_service", "studio_controller
if ("studio_pojo".equals(module)){
templateConfig.setEntity(new TemplateConfig().getEntity(false))
.setMapper(null)//mapper模板
.setXml(null)
.setService(null)
.setServiceImpl(null)
.setController(null);//service模块不生成controller代码
} else if ("studio_dao".equals(module)){//web模块只生成controller代码
templateConfig.setEntity(null)
.setMapper(new TemplateConfig().getMapper())
.setXml(null)
.setService(null)
.setServiceImpl(null)
.setController(null);
} else if ("studio_service".equals(module)){//web模块只生成controller代码
templateConfig.setEntity(null)
.setMapper(null)
.setXml(null)
.setService(new TemplateConfig().getService())
.setServiceImpl(new TemplateConfig().getServiceImpl())
.setController(null);
} else if ("studio_controller".equals(module)){//web模块只生成controller代码
templateConfig.setEntity(null)
.setMapper(null)
.setXml(null)
.setService(null)
.setServiceImpl(null)
.setController(new TemplateConfig().getController());
} else {
throw new IllegalArgumentException("参数匹配错误,请检查");
}
return templateConfig;
}

private static StrategyConfig getStrategyConfig(String[] tableNames) {
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig
//驼峰命名
.setCapitalMode(true)
//lombok
.setEntityLombokModel(true)
//restful
.setRestControllerStyle(true)
.setNaming(NamingStrategy.underline_to_camel)
.setColumnNaming(NamingStrategy.underline_to_camel)
//继承父类
.setSuperEntityClass("com.studio.entity.BaseEntity")
//排除父类的属性
.setSuperEntityColumns(new String[]{"id", "create_id", "update_id", "create_time", "update_time", "is_del"})
.setInclude(tableNames);
return strategyConfig;
}

private static PackageConfig getPackageConfig(String module) {
PackageConfig packageConfig = new PackageConfig();
String packageName = "com.studio";//不同模块 代码生成具体路径自定义指定
//studio_pojo", "studio_dao", "studio_service", "studio_controller
packageConfig.setParent(packageName)
.setEntity("entity")
.setMapper("mapper")
.setService("service")
.setController("controller");
return packageConfig;
}

private static DataSourceConfig getDataSourceConfig() {
String dbUrl = "jdbc:mysql://*.*.*.*:3306/software_studio?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=utf8&allowPublicKeyRetrieval=true";
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL)
.setDriverName(Driver.class.getName())
.setUsername("root")
.setPassword("123456")
.setUrl(dbUrl);
return dataSourceConfig;
}

private static GlobalConfig getGlobalConfig(String module) {
GlobalConfig globalConfig = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
globalConfig.setOpen(false)//new File(module).getAbsolutePath()得到模块根目录路径,因事Maven项目,代码指定路径自定义调整
.setOutputDir(new File(module).getAbsolutePath()+"/src/main/java") //生成文件的输出目录
.setFileOverride(false)//是否覆盖已有文件
.setActiveRecord(false)
.setIdType(IdType.ID_WORKER)
.setAuthor("zhengyue");
return globalConfig;
}
}

效果图展示

Yoyou
Yoyou

如果你觉得有帮助,慷慨如你,可以扫描下面的二维码赞赏一下