目录
一、文章前言
二、取消自动生成注释
三、自动生成序列化
四、自动生成toString
五、生成自定义注释
六、结果演示
七、工程源码
本篇文章讲述的是MyBatis Generator 逆向工程生成自定义注释、序列化、toString配置,在看到这篇文章之前,希望读者对MyBatis Generator 基本使用已经没有问题,因为本篇文章并不会讲述如何搭建MyBatis Generator环境
1.自动生成的注释很烦,而且一点用都没有,所以我们需要取消自动生成的注释
2.在context节点下配置
3.参考代码
1.当我们生成实体类以后,发现默认是没有toString和序列化,但是很多时候需要序列化对象,从而方便在网络上传输
3.参考代码
1.生成toString和生成序列化一样,同样在context节点下加入
2.参考代码
注意:在数据表中一定要有注释字段,以下以MySQL为例,因为会将comment字段注释提取为实体类的注释信息
1.MyBatis逆向工程默认使用的是org.mybatis.generator.api.CommentGenerator类的org.mybatis.generator.internal.DefaultCommentGenerator子类生成的注释,所以我们可以自己实现一个注释类
2.新建一个类,实现CommentGenerator接口
3.从org.mybatis.generator.internal.DefaultCommentGenerator把所有代码拷贝出来,复制到更改新建的类中,注意其中有一个@Override的类会报错,把@Override去掉即可,以下是拷贝过来的代码部分截图
4.我们生成注释的主要有以下几个地方:实体类属性、实体类getter方法、实体类setter方法、实体类静态常量属性、mapper.xml注释
5.实体类属性注释是addFieldComment(Field field,IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) 这个方法
6.实体类静态属性字段的方法名和上一个方法名一样,只是参数不同
7.实体类toString方法是addGeneralMethodComment(Method method, IntrospectedTable introspectedTable),这里没有选择生成任何代码,因为没有必要
8.实体类getter方法注释是addGetterComment方法
9.实体类setter注释是addSetterComment方法
10.实体类对应的mapper.xml注释是addComment(XmlElement xmlElement)方法,这里没有加入任何注释,如有需要参考 DefaultCommentGenerator
1.序列化和属性字段注释,如账户ID是从数据库comment字段中提取出来的
3.getter和setter方法
4.toString
5.mapper
1.maven依赖
mysql
mysql-connector-java
5.1.44
org.mybatis.generator
mybatis-generator-core
1.3.5
org.mybatis
mybatis
3.4.6
2.MyBatis逆向工程配置文件generatorConfig.xml
3.自定义注释类 MyCommentGenerator
/**
* Copyright 2006-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.codecoord.config;
import static org.mybatis.generator.internal.util.StringUtility.isTrue;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Properties;
import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.InnerEnum;
import org.mybatis.generator.api.dom.java.JavaElement;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.MergeConstants;
import org.mybatis.generator.config.PropertyRegistry;
import org.mybatis.generator.internal.util.StringUtility;
public class MyCommentGenerator implements CommentGenerator {
/** The properties. */
private Properties properties;
/** The suppress date. */
private boolean suppressDate;
/** The suppress all comments. */
private boolean suppressAllComments;
/** The addition of table remark's comments.
* If suppressAllComments is true, this option is ignored*/
private boolean addRemarkComments;
private SimpleDateFormat dateFormat;
public MyCommentGenerator() {
super();
properties = new Properties();
suppressDate = false;
suppressAllComments = false;
addRemarkComments = false;
}
public void addJavaFileComment(CompilationUnit compilationUnit) {
}
/**
* 实体类对应的mapper.xml注释,mapper类不加注释,如有需要参考 DefaultCommentGenerator
*/
public void addComment(XmlElement xmlElement) {
if (suppressAllComments) {
return;
}
}
public void addRootComment(XmlElement rootElement) {
}
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));
addRemarkComments = isTrue(properties
.getProperty(PropertyRegistry.COMMENT_GENERATOR_ADD_REMARK_COMMENTS));
String dateFormatString = properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_DATE_FORMAT);
if (StringUtility.stringHasValue(dateFormatString)) {
dateFormat = new SimpleDateFormat(dateFormatString);
}
}
protected void addJavadocTag(JavaElement javaElement,
boolean markAsDoNotDelete) {
javaElement.addJavaDocLine(" *"); //$NON-NLS-1$
StringBuilder sb = new StringBuilder();
sb.append(" * "); //$NON-NLS-1$
sb.append(MergeConstants.NEW_ELEMENT_TAG);
if (markAsDoNotDelete) {
sb.append(" do_not_delete_during_merge"); //$NON-NLS-1$
}
String s = getDateString();
if (s != null) {
sb.append(' ');
sb.append(s);
}
javaElement.addJavaDocLine(sb.toString());
}
protected String getDateString() {
if (suppressDate) {
return null;
} else if (dateFormat != null) {
return dateFormat.format(new Date());
} else {
return new Date().toString();
}
}
public void addClassComment(InnerClass innerClass,
IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**"); //$NON-NLS-1$
sb.append(" * This class corresponds to the database table "); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
innerClass.addJavaDocLine(sb.toString());
addJavadocTag(innerClass, false);
innerClass.addJavaDocLine(" */"); //$NON-NLS-1$
}
public void addModelClassComment(TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
if (suppressAllComments || !addRemarkComments) {
return;
}
StringBuilder sb = new StringBuilder();
topLevelClass.addJavaDocLine("/**"); //$NON-NLS-1$
String remarks = introspectedTable.getRemarks();
if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
topLevelClass.addJavaDocLine(" * Database Table Remarks:");
String[] remarkLines = remarks.split(System.getProperty("line.separator")); //$NON-NLS-1$
for (String remarkLine : remarkLines) {
topLevelClass.addJavaDocLine(" * " + remarkLine); //$NON-NLS-1$
}
}
topLevelClass.addJavaDocLine(" *"); //$NON-NLS-1$
topLevelClass
.addJavaDocLine(" * This class was generated by MyBatis Generator."); //$NON-NLS-1$
sb.append(" * This class corresponds to the database table "); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
topLevelClass.addJavaDocLine(sb.toString());
addJavadocTag(topLevelClass, true);
topLevelClass.addJavaDocLine(" */"); //$NON-NLS-1$
}
public void addEnumComment(InnerEnum innerEnum,
IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerEnum.addJavaDocLine("/**"); //$NON-NLS-1$
innerEnum
.addJavaDocLine(" * This enum was generated by MyBatis Generator."); //$NON-NLS-1$
sb.append(" * This enum corresponds to the database table "); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
innerEnum.addJavaDocLine(sb.toString());
addJavadocTag(innerEnum, false);
innerEnum.addJavaDocLine(" */"); //$NON-NLS-1$
}
/**
* 实体类字段注释
*/
public void addFieldComment(Field field,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
field.addJavaDocLine("/**"); //$NON-NLS-1$
// 核心代码 introspectedColumn.getRemarks() 就是获取字段注释
StringBuilder sb = new StringBuilder();
sb.append(" * " + introspectedColumn.getRemarks());
sb.append("\n\t * [email protected]");
sb.append("\n\t * " + LocalDateTime.now());
field.addJavaDocLine(sb.toString());
field.addJavaDocLine(" */");
}
/**
* 实体类的静态字段
*/
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * [email protected]");
sb.append("\n\t * " + LocalDateTime.now());
field.addJavaDocLine(sb.toString());
field.addJavaDocLine(" */");
}
/**
* 实体类toString方法
*/
public void addGeneralMethodComment(Method method,
IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
/* StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**"); //$NON-NLS-1$
method.addJavaDocLine(" * toString."); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
method.addJavaDocLine(sb.toString());
addJavadocTag(method, false);*/
//method.addJavaDocLine(" */");
}
/**
* 实体类getter方法注释
*/
public void addGetterComment(Method method,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**"); //$NON-NLS-1$
sb.append(" * " + introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" */"); //$NON-NLS-1$
}
/**
* 实体类setter注释
*/
public void addSetterComment(Method method,
IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**"); //$NON-NLS-1$
sb.append(" * " + introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString());
method.addJavaDocLine(" */"); //$NON-NLS-1$
}
public void addClassComment(InnerClass innerClass,
IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**"); //$NON-NLS-1$
innerClass
.addJavaDocLine(" * This class was generated by MyBatis Generator."); //$NON-NLS-1$
sb.append(introspectedTable.getFullyQualifiedTable());
innerClass.addJavaDocLine(sb.toString());
addJavadocTag(innerClass, markAsDoNotDelete);
innerClass.addJavaDocLine(" */"); //$NON-NLS-1$
}
}
4.逆向工程启动类
package com.codecoord.main;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class RunClass {
public static void main(String[] args) {
try {
List warnings = new ArrayList();
boolean overwrite = true;
File configFile = new File("src/main/resources/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
5.工程结构