自定义mybatis-generator生成mapper代码
我是基于tk.mybatis.mapper.generator.MapperPlugin插件进行一个微小的拓展,大多数还是基于MapperPlugin逻辑进行处理,这里非常感谢tk.mybatis作者为我们提供源码基础。
tk.mybatis.mapper-generator源码地址https://repo1.maven.org/maven2/tk/mybatis/mapper-generator/1.1.5/
通过继承PluginAdapter类,开发自定义插件,生成自定义的中文注解的java实体类,dao接口和mapper映射文件
导入mybatis.generator包
<!-- mybatis逆向工程 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
引入tk.mybatis.mapper.generator.MapperPlugin相关类
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.*;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.config.MergeConstants;
import org.mybatis.generator.internal.util.StringUtility;
import java.text.MessageFormat;
import java.util.Properties;
import java.util.Set;
public class MapperCommentGenerator implements CommentGenerator {
//开始的分隔符,例如mysql为`,sqlserver为[
private String beginningDelimiter = "";
//结束的分隔符,例如mysql为`,sqlserver为]
private String endingDelimiter = "";
//强制生成注解
private boolean forceAnnotation;
//是否生成swagger注解
private boolean needsSwagger;
public MapperCommentGenerator() {
super();
}
@Override
public void addJavaFileComment(CompilationUnit compilationUnit) {
return;
}
/**
* xml中的注释
*
* @param xmlElement
*/
@Override
public void addComment(XmlElement xmlElement) {
xmlElement.addElement(new TextElement("<!--"));
StringBuilder sb = new StringBuilder();
sb.append(" WARNING - ");
sb.append(MergeConstants.NEW_ELEMENT_TAG);
xmlElement.addElement(new TextElement(sb.toString()));
xmlElement.addElement(new TextElement("-->"));
}
@Override
public void addRootComment(XmlElement rootElement) {
return;
}
@Override
public void addConfigurationProperties(Properties properties) {
String beginningDelimiter = properties.getProperty("beginningDelimiter");
if (StringUtility.stringHasValue(beginningDelimiter)) {
this.beginningDelimiter = beginningDelimiter;
}
String endingDelimiter = properties.getProperty("endingDelimiter");
if (StringUtility.stringHasValue(endingDelimiter)) {
this.endingDelimiter = endingDelimiter;
}
String forceAnnotation = properties.getProperty("forceAnnotation");
if (StringUtility.stringHasValue(forceAnnotation)) {
this.forceAnnotation = "TRUE".equalsIgnoreCase(forceAnnotation);
}
String needsSwagger = properties.getProperty("needsSwagger");
if (StringUtility.stringHasValue(needsSwagger)) {
this.needsSwagger = "TRUE".equalsIgnoreCase(needsSwagger);
}
}
public String getDelimiterName(String name) {
StringBuilder nameBuilder = new StringBuilder();
nameBuilder.append(beginningDelimiter);
nameBuilder.append(name);
nameBuilder.append(endingDelimiter);
return nameBuilder.toString();
}
/**
* 删除标记
*
* @param javaElement
* @param markAsDoNotDelete
*/
protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {
StringBuilder sb = new StringBuilder();
sb.append(" * ");
sb.append(MergeConstants.NEW_ELEMENT_TAG);
if (markAsDoNotDelete) {
sb.append(" do_not_delete_during_merge");
}
javaElement.addJavaDocLine(sb.toString());
}
/**
* Example使用
*
* @param innerClass
* @param introspectedTable
*/
@Override
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
}
@Override
public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
}
/**
* 给字段添加数据库备注
*
* @param field
* @param introspectedTable
* @param introspectedColumn
*/
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
field.addJavaDocLine("/**");
StringBuilder sb = new StringBuilder();
sb.append(" * ");
sb.append(introspectedColumn.getRemarks());
field.addJavaDocLine(sb.toString());
field.addJavaDocLine(" */");
}
//添加注解
if (field.isTransient()) {
//@Column
field.addAnnotation("@Transient");
}
for (IntrospectedColumn column : introspectedTable.getPrimaryKeyColumns()) {
if (introspectedColumn == column) {
field.addAnnotation("@Id");
break;
}
}
String column = introspectedColumn.getActualColumnName();
if (StringUtility.stringContainsSpace(column) || introspectedTable.getTableConfiguration().isAllColumnDelimitingEnabled()) {
column = introspectedColumn.getContext().getBeginningDelimiter()
+ column
+ introspectedColumn.getContext().getEndingDelimiter();
}
if (!column.equals(introspectedColumn.getJavaProperty())) {
//@Column
field.addAnnotation("@Column(name = \"" + getDelimiterName(column) + "\")");
} else if (StringUtility.stringHasValue(beginningDelimiter) || StringUtility.stringHasValue(endingDelimiter)) {
field.addAnnotation("@Column(name = \"" + getDelimiterName(column) + "\")");
} else if (forceAnnotation) {
field.addAnnotation("@Column(name = \"" + getDelimiterName(column) + "\")");
}
if (introspectedColumn.isIdentity()) {
if ("JDBC".equals(introspectedTable.getTableConfiguration().getGeneratedKey().getRuntimeSqlStatement())) {
field.addAnnotation("@GeneratedValue(generator = \"JDBC\")");
} else {
field.addAnnotation("@GeneratedValue(strategy = GenerationType.IDENTITY)");
}
} else if (introspectedColumn.isSequenceColumn()) {
//在 Oracle 中,如果需要是 SEQ_TABLENAME,那么可以配置为 select SEQ_{1} from dual
String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();
String sql = MessageFormat.format(introspectedTable.getTableConfiguration().getGeneratedKey().getRuntimeSqlStatement(), tableName, tableName.toUpperCase());
field.addAnnotation("@GeneratedValue(strategy = GenerationType.IDENTITY, generator = \"" + sql + "\")");
}
// region swagger注解
if (this.needsSwagger) {
String remarks = introspectedColumn.getRemarks();
if (remarks == null) {
remarks = "";
}
String swaggerAnnotation = "@ApiModelProperty(value = \"%s\" da )";
field.addAnnotation("@ApiModelProperty(\"" + remarks.replaceAll("\r", "").replaceAll("\n", "") + "\")");
}
// endregion
}
/**
* Example使用
*
* @param field
* @param introspectedTable
*/
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
}
@Override
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
}
/**
* @param method
* @param introspectedTable
*/
@Override
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
}
/**
* getter方法注释
*
* @param method
* @param introspectedTable
* @param introspectedColumn
*/
@Override
public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
StringBuilder sb = new StringBuilder();
method.addJavaDocLine("/**");</