##1.导入maven依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
##2.创建generatorConfig.xml文件 并修改文件路径的和MySQL的配置
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
password="postgres">
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
##3.自定义mybatis逆向项工程
实现IntrospectedTableMyBatis3SimpleImpl.calculateXmlMapperGenerator()这个方法是mybatis成成xml的方法 再这个基础上通过模板引擎freemarker 生成我们 controller,service和serviceImpl代码
public class InsoIntrospectedTable extends IntrospectedTableMyBatis3SimpleImpl {
/**
* XML的生成方法
* @param javaClientGenerator
* @param warnings
* @param progressCallback
*/
@Override
protected void calculateXmlMapperGenerator(AbstractJavaClientGenerator javaClientGenerator, List
//这个添加自定模板生成方法
this.xmlMapperGenerator = new MySimpleXMLMapperGenerator();
this.initializeAbstractGenerator(this.xmlMapperGenerator, warnings, progressCallback);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class MySimpleXMLMapperGenerator extends SimpleXMLMapperGenerator {
static FileUrl fileUrl;
@Override
protected XmlElement getSqlMapElement() {
FullyQualifiedTable table = this.introspectedTable.getFullyQualifiedTable();
this.progressCallback.startTask(Messages.getString("Progress.12", new String[]{table.toString()}));
XmlElement answer = new XmlElement("mapper");
String namespace = this.introspectedTable.getMyBatis3SqlMapNamespace();
answer.addAttribute(new Attribute("namespace", namespace));
this.context.getCommentGenerator().addRootComment(answer);
this.addResultMapElement(answer);
this.addDeleteByPrimaryKeyElement(answer);
this.addInsertElement(answer);
this.addUpdateByPrimaryKeyElement(answer);
this.addSelectByPrimaryKeyElement(answer);
this.addSelectAllElement(answer);
//this.addCountListElement(answer);
// this.addQueryPageListElement(answer);
//这个读取配置文件 controller,service和serviceImpl 生成的路径配置
getProperties();
fileUrl.setBeanurl(introspectedTable.getBaseRecordType());
try {
genDAO( introspectedTable.getTableConfiguration().getDomainObjectName(),"Integer","id");
genController( introspectedTable.getTableConfiguration().getDomainObjectName(),"Integer","id");
genService( introspectedTable.getTableConfiguration().getDomainObjectName(),"Integer","id");
genServiceImpl( introspectedTable.getTableConfiguration().getDomainObjectName(),"Integer","id");
} catch (Exception e) {
e.printStackTrace();
}
return answer;
}
//Controller生成
public static void genController(String name ,String primaryType, String primaryKey) throws Exception {
Map
paramMap.put("controllerUrl", fileUrl.getControllerurl());
paramMap.put("serviceUrl", fileUrl.getServiceurl());
paramMap.put("url", fileUrl.getBeanurl());
paramMap.put("name", name);
paramMap.put("primaryType", primaryType);
paramMap.put("primaryKey", primaryKey);
paramMap.put("capitalName", name);
genFile(paramMap,fileUrl.getControllerurl(),"Controller","controller.vm");
}
//Service生成
public static void genService(String name ,String primaryType, String primaryKey) throws Exception {
Map
paramMap.put("serviceUrl", fileUrl.getServiceurl());
paramMap.put("url", fileUrl.getBeanurl());
paramMap.put("name", name);
paramMap.put("primaryType", primaryType);
paramMap.put("primaryKey", primaryKey);
genFile(paramMap,fileUrl.getServiceurl(),"Service","service.vm");
}
//ServiceImpl生成
public static void genServiceImpl(String name ,String primaryType, String primaryKey) throws Exception {
Map
paramMap.put("serviceImplUrl", fileUrl.getServiceImplurl());
paramMap.put("serviceUrl", fileUrl.getServiceurl());
paramMap.put("mapperUrl", fileUrl.getDaourl());
paramMap.put("url", fileUrl.getBeanurl());
paramMap.put("name", name);
paramMap.put("primaryType", primaryType);
paramMap.put("primaryKey", primaryKey);
genFile(paramMap,fileUrl.getServiceImplurl(),"ServiceImpl","serviceImpl.vm");
}
//genDAO生成
public static void genDAO(String name ,String primaryType, String primaryKey) throws Exception {
Map
paramMap.put("mapperUrl", fileUrl.getDaourl());
paramMap.put("url", fileUrl.getBeanurl());
paramMap.put("name", name);
paramMap.put("primaryType", primaryType);
paramMap.put("primaryKey", primaryKey);
genFile(paramMap,fileUrl.getDaourl(),"Dao","mapper.vm");
}
//获取模板
public static void genFile(Map
//创建一个合适的Configration对象
freemarker.template.Configuration configuration = new freemarker.template.Configuration(Configuration.VERSION_2_3_23 );
configuration.setDirectoryForTemplateLoading(new File(".\\src\\main\\resources\\template"));
configuration.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23));
configuration.setDefaultEncoding("UTF-8"); //这个一定要设置,不然在生成的页面中 会乱码
//获取或创建一个模版。
Template template = configuration.getTemplate(filetemplate);
File file =new File(".\\src\\main\\java\\"+fileurl.replace(".","\\\\"));
if(!file.exists()){
file.mkdirs();
}
Writer writer = new OutputStreamWriter(new FileOutputStream(".\\src\\main\\java\\"+fileurl.replace(".","\\\\")+"\\"+paramMap.get("name")+filesuffix+".java" ),"UTF-8");
template.process(paramMap, writer);
System.out.println("恭喜,生成成功~~");
}
public static void getProperties(){
Properties properties = new Properties();
InputStream in = MySimpleJavaClientGenerator.class.getResourceAsStream("/application.properties");//加载 application.properties资源文件,如果该文件在包内则加包名
try {
properties.load(in);
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
fileUrl=new FileUrl();
fileUrl.setControllerurl(properties.getProperty("geneator.controller"));
fileUrl.setDaourl(properties.getProperty("geneator.dao"));
fileUrl.setServiceImplurl(properties.getProperty("geneator.serviceImpl"));
fileUrl.setServiceurl(properties.getProperty("geneator.service"));
}
}
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
上述的模板
https://github.com/weijinhua6897/generationcode/tree/master/src/main/resources/template路径拷贝
在application.properties 文件中 controller,service和serviceImpl路径配置文件
geneator.controller=test.controller
geneator.service=test.service
geneator.serviceImpl=test.serviceImpl
geneator.dao=test.dao
1
2
3
4
##在mybatis中使用自定义的代码
此处换成自定MySimpleXMLMapperGenerator路径
##将数据库的字段注释添加mybatis 逆向工程的bean对象中
public class MyCommentGenerator implements CommentGenerator{
/**
* properties配置文件
*/
private Properties properties;
/**
* properties配置文件
*/
private Properties systemPro;
/*
* 父类时间
*/
private boolean suppressDate;
/**
* 父类所有注释
*/
private boolean suppressAllComments;
/**
* 当前时间
*/
private String currentDateStr;
public MyCommentGenerator() {
super();
properties = new Properties();
systemPro = System.getProperties();
suppressDate = false;
suppressAllComments = false;
currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
}
/**
* Java类的类注释
*/
@Override
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
sb.append(" ");
sb.append(getDateString());
innerClass.addJavaDocLine(sb.toString().replace("\n", " "));
innerClass.addJavaDocLine(" */");
}
/**
* 为类添加注释
*/
@Override
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**");
sb.append(" * ");
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
innerClass.addJavaDocLine(sb.toString().replace("\n", " "));
sb.setLength(0);
sb.append(" * @author ");
sb.append(systemPro.getProperty("user.name"));
sb.append(" ");
sb.append(currentDateStr);
innerClass.addJavaDocLine(" */");
}
/**
* Mybatis的Mapper.xml文件里面的注释
*/
@Override
public void addComment(XmlElement xmlElement) {
}
/**
*
*@Title addConfigurationProperties
*@Description: 从该配置中的任何属性添加此实例的属性CommentGenerator配置。
* 这个方法将在任何其他方法之前被调用。
*@Author fendo
*@Date 2017年10月5日 下午3:45:58
*@return
*@throws
*/
@Override
public void addConfigurationProperties(Properties properties) {
this.properties.putAll(properties);
suppressDate = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
suppressAllComments = isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
}
/**
*
*@Title getDateString
*@Description: 此方法返回格式化的日期字符串以包含在Javadoc标记中和XML注释。 如果您不想要日期,则可以返回null在这些文档元素中。
*@Author fendo
*@Date 2017年10月5日 下午3:45:58
*@return
*@throws
*/
protected String getDateString() {
String result = null;
if (!suppressDate) {
result = currentDateStr;
}
return result;
}
/**
*
*@Title addJavadocTag
*@Description: 此方法为其添加了自定义javadoc标签。
*@Author fendo
*@Date 2017年10月5日 下午3:49:05
*@param javaElement
*@param markAsDoNotDelete
*@throws
*/
protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {
javaElement.addJavaDocLine(" *");
StringBuilder sb = new StringBuilder();
sb.append(" * ");
sb.append(MergeConstants.NEW_ELEMENT_TAG);
if (markAsDoNotDelete) {
sb.append(" do_not_delete_during_merge");
}
String s = getDateString();
if (s != null) {
sb.append(' ');
sb.append(s);
}
javaElement.addJavaDocLine(sb.toString());
}
/**
* 为枚举添加注释
*/
@Override
public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerEnum.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
innerEnum.addJavaDocLine(sb.toString().replace("\n", " "));
innerEnum.addJavaDocLine(" */");
}
/**
* Java属性注释
*/
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
field.addJavaDocLine(sb.toString().replace("\n", " "));
field.addJavaDocLine(" */");
}
/**
* 为字段添加注释
*/
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedColumn.getRemarks());
field.addJavaDocLine(sb.toString().replace("\n", " "));
field.addJavaDocLine(" */");
}
/**
* 普通方法的注释,这里主要是XXXMapper.java里面的接口方法的注释
*/
@Override
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
method.addJavaDocLine("/**");
addJavadocTag(method, false);
method.addJavaDocLine(" */");
}
/**
* 给getter方法加注释
*/
@Override
public void addGetterComment(Method method, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
// method.addJavaDocLine("/**");
// StringBuilder sb = new StringBuilder();
// sb.append(" * ");
// sb.append(introspectedColumn.getRemarks());
// method.addJavaDocLine(sb.toString().replace("\n", " "));
// sb.setLength(0);
// sb.append(" * @return ");
// sb.append(introspectedColumn.getActualColumnName());
// sb.append(" ");
// sb.append(introspectedColumn.getRemarks());
// method.addJavaDocLine(sb.toString().replace("\n", " "));
// method.addJavaDocLine(" */");
}
/**
* 给Java文件加注释,这个注释是在文件的顶部,也就是package上面。
*/
@Override
public void addJavaFileComment(CompilationUnit compilationUnit) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
compilationUnit.addFileCommentLine("/*");
compilationUnit.addFileCommentLine("*");
compilationUnit.addFileCommentLine("* "+compilationUnit.getType().getShortName()+".java");
compilationUnit.addFileCommentLine("* Copyright(C) 2017-2020 fendo公司");
compilationUnit.addFileCommentLine("* @date "+sdf.format(new Date())+"");
compilationUnit.addFileCommentLine("*/");
}
/**
* 为模型类添加注释
*/
@Override
public void addModelClassComment(TopLevelClass arg0, IntrospectedTable arg1) {
}
/**
* 为调用此方法作为根元素的第一个子节点添加注释。
*/
@Override
public void addRootComment(XmlElement arg0) {
}
/**
* 给setter方法加注释
*/
@Override
public void addSetterComment(Method method, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
// method.addJavaDocLine("/**");
// StringBuilder sb = new StringBuilder();
// sb.append(" * ");
// sb.append(introspectedColumn.getRemarks());
// method.addJavaDocLine(sb.toString().replace("\n", " "));
// Parameter parm = method.getParameters().get(0);
// sb.setLength(0);
// sb.append(" * @param ");
// sb.append(parm.getName());
// sb.append(" ");
// sb.append(introspectedColumn.getRemarks());
// method.addJavaDocLine(sb.toString().replace("\n", " "));
// method.addJavaDocLine(" */");
}
}
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
换成上面的 MyCommentGenerator类的路径
##最后
public static void main(String[] args) throws Exception {
List
boolean overwrite = true;
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(
new File(GeneratorConfiguration.class.getClassLoader().getResource("generatorConfig.xml").getFile()));
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
调用这段代码生成成功
源码地址
https://github.com/weijinhua6897/generationcod
---------------------
作者:留恋3
来源:CSDN
原文:https://blog.csdn.net/TIANHUAXIAN2/article/details/81220053
版权声明:本文为博主原创文章,转载请附上博文链接!