阅读本文之前可先参考我的另一篇文章:
Mybatis-generator自动生成代码工具嵌入Maven项目中,实现一键生成数据库表对应的java、xml文件
通过上文介绍的方法根据数据库表生成实体类如下所示
可以看到默认的英文注释说明了类属性所对应的数据库字段,但是没有拿到数据库中定义的注释。
根据网上找到的资料以及阅读Mybatis-generator源码,发现可以通过自定义注释生成器 CommentGenerator 接口的实现类来达到我们的目的。
通过阅读源码可以看到,代码生成注释是通过DefaultCommentGenerator类实现的,这个类继承了CommentGenerator接口,因此我们可以自己创建一个类继承CommentGenerator接口,然后重写里面相关方法就可以了(可以复制DefaultCommentGenerator中的代码过来,然后修改我们需要改动的方法)。
下面介绍实现过程。
1、自定义一个CommentGenerator接口的实现类,下面是我的自定义实现类代码
package com.xxx.xxx.util.generator;
import java.text.SimpleDateFormat;
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.Parameter;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.MergeConstants;
import org.mybatis.generator.config.PropertyRegistry;
/**
* 自定义实现 注释生成器 CommentGenerator 接口
* @author 北北
* @date 2018年1月17日上午10:22:11
*/
public class MyCommentGenerator implements CommentGenerator {
private Properties properties;
private Properties systemPro;
private boolean suppressDate;
private boolean suppressAllComments;
private String nowTime;
public MyCommentGenerator() {
super();
properties = new Properties();
systemPro = System.getProperties();
suppressDate = false;
suppressAllComments = false;
nowTime = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
}
public void addJavaFileComment(CompilationUnit compilationUnit) {
if (suppressAllComments) {
return;
}
return;
}
/**
* Adds a suitable comment to warn users that the element was generated, and
* when it was generated.
*/
public void addComment(XmlElement xmlElement) {
return;
}
public void addRootComment(XmlElement rootElement) {
// add no document level comments by default
return;
}
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));
}
/**
* 判断传入参数是否为true
* @author 北北
* @date 2018年2月2日下午3:55:06
* @param property
* @return
*/
private boolean isTrue(String property) {
if("true".equals(property)){
return true;
}
return false;
}
/**
* This method adds the custom javadoc tag for. You may do nothing if you do
* not wish to include the Javadoc tag - however, if you do not include the
* Javadoc tag then the Java merge capability of the eclipse plugin will
* break.
*
* @param javaElement
* the java element
*/
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());
}
/**
* This method returns a formated date string to include in the Javadoc tag
* and XML comments. You may return null if you do not want the date in
* these documentation elements.
*
* @return a string representing the current timestamp, or null
*/
protected String getDateString() {
String result = null;
if (!suppressDate) {
result = nowTime;
}
return result;
}
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(" */");
}
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(" */");
}
/**
* 设置字段注释
*/
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedColumn.getRemarks() + " " + introspectedColumn.getActualColumnName());
field.addJavaDocLine(sb.toString().replace("\n", " "));
field.addJavaDocLine(" */");
}
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(" */");
}
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
if (suppressAllComments) {
return;
}
method.addJavaDocLine("/**");
addJavadocTag(method, false);
method.addJavaDocLine(" */");
}
/**
* 设置getter方法注释
*/
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(" * @author ");
sb.append(systemPro.getProperty("user.name"));
method.addJavaDocLine(sb.toString().replace("\n", " "));
sb.setLength(0);
//是否加入时间戳
if(suppressDate){
sb.append(" * @date " + nowTime);
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(" */");
}
/**
* 设置setter方法注释
*/
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", " "));
sb.setLength(0);
//加入系统用户
sb.append(" * @author ");
sb.append(systemPro.getProperty("user.name"));
method.addJavaDocLine(sb.toString().replace("\n", " "));
sb.setLength(0);
//是否加入时间戳
if(suppressDate){
sb.append(" * @date " + nowTime);
method.addJavaDocLine(sb.toString().replace("\n", " "));
sb.setLength(0);
}
Parameter parm = method.getParameters().get(0);
sb.append(" * @param ");
sb.append(parm.getName());
sb.append(" ");
sb.append(introspectedColumn.getRemarks());
method.addJavaDocLine(sb.toString().replace("\n", " "));
method.addJavaDocLine(" */");
}
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
if (suppressAllComments) {
return;
}
StringBuilder sb = new StringBuilder();
innerClass.addJavaDocLine("/**");
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(nowTime);
innerClass.addJavaDocLine(" */");
}
}
2、有了自定义的CommentGenerator实现类之后,我们要使用它,这个也很简单,只需要在配置文件中的myGeneratorConfig.xml中,给commentGenerator元素设置type属性值为我们自定义类的全类名就可以了
<!-- 这里的type里写的是你的实现类的类全类名,注:如果不配置type属性,则会使用默认的CommentGenerator实现类DefaultCommentGenerator -->
<commentGenerator type="com.xxx.xxx.util.generator.MyCommentGenerator">
<!-- 去除自动生成的注释 -->
<!-- <property name="suppressAllComments" value="true" /> -->
<!-- 是否生成注释代时间戳-->
<property name="suppressDate" value="true"/>
</commentGenerator>
3、下图是最终生成实体类的效果
最后,一般来说我们只需要实现实体类注释的方法就可以,如果觉得要实现CommentGenerator中所有的接口太麻烦的话,也可以让自定义实现类继承DefaultCommentGenerator类,然后重写与实体类属性、getter、setter注释相关的方法就可以了。