下载mybatis-generato源码:https://github.com/mybatis/generator.git
自定义生成实体注释
package org.mybatis.generator.internal;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
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.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.XmlElement;
import java.util.Date;
/**
* 自定义生成实体注释
*
* @author duxuefu
*/
public class MyCommentGenerator extends DefaultCommentGenerator {
private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
//类上添加注释
@Override
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
topLevelClass.addJavaDocLine("/**");
String remark = introspectedTable.getRemarks();
if (StringUtils.isNotBlank(remark)) {
topLevelClass.addJavaDocLine(" * " + remark);
}
topLevelClass.addJavaDocLine(" * ");
topLevelClass.addJavaDocLine(" * " + "@author MyBatis Generator");
topLevelClass.addJavaDocLine(" * " + "@date " + DateFormatUtils.format(new Date(), DEFAULT_DATE_FORMAT));
topLevelClass.addJavaDocLine(" */");
}
//属性上添加注释
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
String remark = introspectedColumn.getRemarks();
if (StringUtils.isNotBlank(remark)) {
field.addJavaDocLine("/**");
field.addJavaDocLine(" * " + remark);
field.addJavaDocLine(" */");
}
}
//get方法添加注释
@Override
public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
}
//setter方法添加注释
@Override
public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
}
@Override
public void addComment(XmlElement xmlElement) {
}
}
自定义setter 和getter方法,此处用 Lombok 代替
package org.mybatis.generator.internal;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
/**
* 类描述
*
* @author duxuefu
*/
public class IngoreSetterAndGetterPlugin extends PluginAdapter {
private static final Logger logger = LoggerFactory.getLogger(IngoreSetterAndGetterPlugin.class);
@Override
public boolean validate(List<String> warnings) {
return true;
}
@Override
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
//该代码表示在生成class的时候,向topLevelClass添加一个@Setter和@Getter注解
topLevelClass.addAnnotation("@Data");
topLevelClass.addImportedType("lombok.Data");
return super.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
}
//该方法在生成每一个属性的getter方法时候调用,如果我们不想生成getter,直接返回false即可;
@Override
public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
return false;
}
//该方法在生成每一个属性的setter方法时候调用,如果我们不想生成setter,直接返回false即可;
@Override
public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
return false;
}
}
在generatorConfig.xml中的使用
<generatorConfiguration>
... 此处省略 ...
<context id="localMySQL">
<plugin type="org.mybatis.generator.internal.IngoreSetterAndGetterPlugin" />
<!-- 需要修改源码 -->
<commentGenerator type="org.mybatis.generator.internal.MyCommentGenerator">
<!-- 文件编码 -->
<property name="javaFileEncoding" value="UTF-8" />
<!-- 忽略所有注解,使用自定义的注解生成 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
... 此处省略 ...
</context>
</generatorConfiguration>