我們都知道mybatis generator自動生成的註釋沒什麼實際作用,而且還增加了代碼量。如果能將註釋從數據庫中撈取到,不僅能很大程度上增加代碼的可讀性,而且減少了後期手動加註釋的工作量。
1、首先定義註釋生成插件
MyCommentGenerator.java
package com.ilovey.mybatis.comment;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
/**
* mybatis generator生成註釋插件
* <p>
* Created by huhaichao on 2017/5/15.
*/
public class MyCommentGenerator extends DefaultCommentGenerator {
private 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());
}
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(" */");
}
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
}
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
}
public void addGetterComment(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
}
public void addSetterComment(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn) {
}
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
}
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
}
}
2、然後爲mybatisgenerator配置插件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="context1">
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!-- 使用自定義的插件 -->
<commentGenerator type="com.ilovey.mybatis.comment.MyCommentGenerator">
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF8"
userId="root" password="123456">
</jdbcConnection>
<javaModelGenerator targetPackage="com.ilovey.biz.entity.base"
targetProject="ilovey.biz/src/main/java"/>
<sqlMapGenerator targetPackage="com.ilovey.biz.mapper.base"
targetProject="ilovey.biz/src/main/resources"/>
<javaClientGenerator targetPackage="com.ilovey.biz.mapper.base"
targetProject="ilovey.biz/src/main/java" type="XMLMAPPER"/>
<table tableName="us_user_info" domainObjectName="UsUserInfo">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
</context>
</generatorConfiguration>
3、生成效果
package com.ilovey.biz.entity.base;
import java.io.Serializable;
import java.util.Date;
public class UsUserInfo implements Serializable {
/**
*
*/
private Integer id;
/**
* 用戶id
*/
private Integer userId;
/**
* 暱稱
*/
private String nickName;
/**
* 頭像
*/
private String headImage;
/**
* 手機號碼
*/
private String mobile;
/**
* 性別(0保密,1男,2女)
*/
private Integer sex;
/**
* 地區
*/
private Integer region;
/**
* 個性簽名
*/
private String signature;
/**
* 創建時間
*/
private Date createTime;
/**
* 更新時間
*/
private Date updateTime;
//setter 和 getter方法省略
}
這裏雖然消滅看不懂的英文註釋,改爲友好的中文註釋,但是生成的getter/setter方法仍然很長,經常還要生成toString/hashCode/Equals等方法,其實我們只需要幾個屬性定義了,卻不得不生成幾倍的代碼,大家有沒有因此而頭痛呢?
是不是想說有什麼方法可以解決嗎,請參考我的接下里的文章《mybatis generator插件系列--lombok插件 (減少百分之九十bean代碼)》
本文所有代碼及demo:https://gitee.com/chaocloud/generator-demo.git