閱讀本文之前可先參考我的另一篇文章:
Mybatis-generator自動(dòng)生成代碼工具嵌入Maven項(xiàng)目中颗管,實(shí)現(xiàn)一鍵生成數(shù)據(jù)庫(kù)表對(duì)應(yīng)的java、xml文件
通過(guò)上文介紹的方法根據(jù)數(shù)據(jù)庫(kù)表生成實(shí)體類(lèi)如下所示
可以看到默認(rèn)的英文注釋說(shuō)明了類(lèi)屬性所對(duì)應(yīng)的數(shù)據(jù)庫(kù)字段,但是沒(méi)有拿到數(shù)據(jù)庫(kù)中定義的注釋重挑。
根據(jù)網(wǎng)上找到的資料以及閱讀Mybatis-generator源碼嗓化,發(fā)現(xiàn)可以通過(guò)自定義注釋生成器 CommentGenerator 接口的實(shí)現(xiàn)類(lèi)來(lái)達(dá)到我們的目的。
通過(guò)閱讀源碼可以看到谬哀,代碼生成注釋是通過(guò)DefaultCommentGenerator類(lèi)實(shí)現(xiàn)的刺覆,這個(gè)類(lèi)繼承了CommentGenerator接口,因此我們可以自己創(chuàng)建一個(gè)類(lèi)繼承CommentGenerator接口史煎,然后重寫(xiě)里面相關(guān)方法就可以了(可以復(fù)制DefaultCommentGenerator中的代碼過(guò)來(lái)谦屑,然后修改我們需要改動(dòng)的方法)。
下面介紹實(shí)現(xiàn)過(guò)程篇梭。
1氢橙、自定義一個(gè)CommentGenerator接口的實(shí)現(xiàn)類(lèi),下面是我的自定義實(shí)現(xiàn)類(lèi)代碼
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;
/**
* 自定義實(shí)現(xiàn) 注釋生成器 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));
}
/**
* 判斷傳入?yún)?shù)是否為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(" */");
}
/**
* 設(shè)置字段注釋
*/
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(" */");
}
/**
* 設(shè)置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);
//加入系統(tǒng)用戶(hù)
sb.append(" * @author ");
sb.append(systemPro.getProperty("user.name"));
method.addJavaDocLine(sb.toString().replace("\n", " "));
sb.setLength(0);
//是否加入時(shí)間戳
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(" */");
}
/**
* 設(shè)置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);
//加入系統(tǒng)用戶(hù)
sb.append(" * @author ");
sb.append(systemPro.getProperty("user.name"));
method.addJavaDocLine(sb.toString().replace("\n", " "));
sb.setLength(0);
//是否加入時(shí)間戳
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實(shí)現(xiàn)類(lèi)之后悍手,我們要使用它,這個(gè)也很簡(jiǎn)單袍患,只需要在配置文件中的myGeneratorConfig.xml中坦康,給commentGenerator元素設(shè)置type屬性值為我們自定義類(lèi)的全類(lèi)名就可以了
<!-- 這里的type里寫(xiě)的是你的實(shí)現(xiàn)類(lèi)的類(lèi)全類(lèi)名,注:如果不配置type屬性,則會(huì)使用默認(rèn)的CommentGenerator實(shí)現(xiàn)類(lèi)DefaultCommentGenerator -->
<commentGenerator type="com.xxx.xxx.util.generator.MyCommentGenerator">
<!-- 去除自動(dòng)生成的注釋 -->
<!-- <property name="suppressAllComments" value="true" /> -->
<!-- 是否生成注釋代時(shí)間戳-->
<property name="suppressDate" value="true"/>
</commentGenerator>
3、下圖是最終生成實(shí)體類(lèi)的效果
最后诡延,一般來(lái)說(shuō)我們只需要實(shí)現(xiàn)實(shí)體類(lèi)注釋的方法就可以滞欠,如果覺(jué)得要實(shí)現(xiàn)CommentGenerator中所有的接口太麻煩的話(huà),也可以讓自定義實(shí)現(xiàn)類(lèi)繼承DefaultCommentGenerator類(lèi)肆良,然后重寫(xiě)與實(shí)體類(lèi)屬性筛璧、getter、setter注釋相關(guān)的方法就可以了惹恃。