让 MyBatis Generator 用数据库注释作 Java 注释,并支持附加注解

这两天详细了解了 MyBatis Generator
这款工具,它生成 POJO 以及 Mapper 的功能还是比较实用的,而且由于生成的代码也是工程代码的一部分,自定义起来相对库来说也会方便一些,因此我打算将其用于公司的工程中。但是,MyBatis Generator 也是有一些不足的,其默认生成的 Java 注释实在惨不忍睹,包含了大量重复的无效信息;此外,我想实现在数据库中写一次注释,在 Java 代码中复用这种效果,同时还需要在生成 Java POJO 的过程中附加上公司自研 RPC 框架的注解。这两点功能 MyBatis Generator 默认提供的注释生成器是不能很好的支持的。因此需要自定义一个注释生成器来实现这些特定的需求

实现

想要自己定义注释生成器,先要找到 MyBatis Generator 提供的注释生成器接口。这个接口是 org.mybatis.generator.api.CommentGenerator
其默认实现为 org.mybatis.generator.internal.DefaultCommentGenerator
(默认实现也是唯一实现),其实我翻了一翻 DefaultCommentGenerator
的代码,发现其默认是支持将数据库注释作为生成的 Java 类的注释的,只是功能默认是关闭的,但是附加注解,默认实现是不支持的,我们就来自己实现吧。由于 MyBatis Generator 并没有为我们提供抽象类级别的生成器,只有一个顶层借口,而且顶层接口包含了过量的方法,大部分我们都用不到,因此们需要自己实现一个抽象类,来屏蔽不需要的方法接口,然后让自定义注释生成器继承抽象类(当然也可以直接继承默认实现,但我觉得那不是好的选择)。

拿起键盘就是干!哈哈,敲代码吧( 工程
在我的 GitHub 上):

package io.github.since1986.mybatis.comment.generator;

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.XmlElement;

import java.util.Properties;
import java.util.Set;

// 抽象类,屏蔽掉接口中过多的不需要实现的方法,不需要的什么都不做就好了
public abstract class AbstractCommentGenerator implements CommentGenerator {

    @Override
    public void addConfigurationProperties(Properties properties) {

    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {

    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {

    }

    @Override
    public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {

    }

    @Override
    public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {

    }

    @Override
    public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {

    }

    @Override
    public void addJavaFileComment(CompilationUnit compilationUnit) {

    }

    @Override
    public void addComment(XmlElement xmlElement) {

    }

    @Override
    public void addRootComment(XmlElement rootElement) {

    }

    @Override
    public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> imports) {

    }

    @Override
    public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {

    }

    @Override
    public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> imports) {

    }

    @Override
    public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {

    }

    @Override
    public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> imports) {

    }
}

package io.github.since1986.mybatis.comment.generator;

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.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.config.PropertyRegistry;
import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

// 这个是我们具体的实现,可以借鉴默认实现的一些办法去写
public class CommentWithAnnotationCommentGenerator extends AbstractCommentGenerator {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommentWithAnnotationCommentGenerator.class);

    private static final String SUPPRESS_ALL_ANNOTATIONS = "suppressAllAnnotations";
    private static final String FIELD_ANNOTATION_FULLY_QUALIFIED_NAMES = "fieldAnnotationFullyQualifiedNames";
    private static final String CLASS_ANNOTATION_FULLY_QUALIFIED_NAMES = "classAnnotationFullyQualifiedNames";

    private static final String FIELD_COMMENT_TEMPLATE = "/** %s */";
    private static final String CLASS_COMMENT_TEMPLATE = "/** %s */";

    private Properties properties = new Properties();

    // 保留官方的重要设置属性,这样更符合直觉
    private boolean suppressAllComments;

    private boolean suppressAllAnnotations;
    private List<String> fieldAnnotationFullyQualifiedNames = new LinkedList<>();
    private List<String> classAnnotationFullyQualifiedNames = new LinkedList<>();

    @Override
    public void addConfigurationProperties(Properties properties) {
        this.properties.putAll(properties);
        suppressAllComments = StringUtility.isTrue(this.properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));
        suppressAllAnnotations = StringUtility.isTrue(this.properties.getProperty(SUPPRESS_ALL_ANNOTATIONS));
        fieldAnnotationFullyQualifiedNames.addAll(Util.toList(this.properties.getProperty(FIELD_ANNOTATION_FULLY_QUALIFIED_NAMES)));
        classAnnotationFullyQualifiedNames.addAll(Util.toList(this.properties.getProperty(CLASS_ANNOTATION_FULLY_QUALIFIED_NAMES)));
    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        if (!suppressAllComments && Util.isNotBlank(introspectedColumn.getRemarks())) {
            LOGGER.debug(String.format("field = %s, column = %s, columnRemarks = %s", field.getName(), introspectedColumn.getActualColumnName(), introspectedColumn.getRemarks()));
            field.addJavaDocLine(String.format(FIELD_COMMENT_TEMPLATE, introspectedColumn.getRemarks()));
        }
        if (!suppressAllAnnotations) {
            LOGGER.debug(String.format("fieldAnnotationFullyQualifiedNames = %s]", fieldAnnotationFullyQualifiedNames));
            fieldAnnotationFullyQualifiedNames.forEach(fieldAnnotationFullyQualifiedName -> {
                field.addJavaDocLine(Util.atAnnotationName(fieldAnnotationFullyQualifiedName));
            });
        } else {
            LOGGER.debug(String.format("suppressAllComments = %b, suppressAllAnnotations = %b", suppressAllComments, suppressAllAnnotations));
        }
    }

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        if (!suppressAllComments && Util.isNotBlank(introspectedTable.getRemarks())) {
            LOGGER.debug(String.format("class = %s, table = %s, tableRemarks = %s", topLevelClass.getType(), introspectedTable.getFullyQualifiedTable(), introspectedTable.getRemarks()));
            topLevelClass.addJavaDocLine(String.format(CLASS_COMMENT_TEMPLATE, introspectedTable.getRemarks()));
        }
        if (!suppressAllAnnotations) {
            LOGGER.debug(String.format("classAnnotationFullyQualifiedNames = %s]", classAnnotationFullyQualifiedNames));
            classAnnotationFullyQualifiedNames.forEach(classAnnotationFullyQualifiedName -> {
                topLevelClass.addJavaDocLine(Util.atAnnotationName(classAnnotationFullyQualifiedName));
                topLevelClass.addImportedType(new FullyQualifiedJavaType(classAnnotationFullyQualifiedName));
            });
            fieldAnnotationFullyQualifiedNames.forEach(fieldAnnotationFullyQualifiedName -> {
                topLevelClass.addImportedType(new FullyQualifiedJavaType(fieldAnnotationFullyQualifiedName));
            });
        } else {
            LOGGER.debug(String.format("suppressAllComments = %b, suppressAllAnnotations = %b", suppressAllComments, suppressAllAnnotations));
        }
    }

    static class Util {

        static boolean isBlank(String string) {
            return string == null || string.trim().length() == 0;
        }

        static boolean isNotBlank(String string) {
            return !isBlank(string);
        }

        static List<String> toList(String commaSeparatedString) {
            List<String> list = new LinkedList<>();
            assert isNotBlank(commaSeparatedString);
            StringTokenizer stringTokenizer = new StringTokenizer(commaSeparatedString, ",");
            String token;
            while (stringTokenizer.hasMoreTokens()) {
                token = stringTokenizer.nextToken();
                list.add(token);
            }
            if (list.size() == 0) {
                list.add(commaSeparatedString);
            }
            return list;
        }

        static String atAnnotationName(String annotationFullyQualifiedName) {
            int lastIndexOfDot = annotationFullyQualifiedName.lastIndexOf(".");
            assert lastIndexOfDot != -1;
            String atAnnotationName = annotationFullyQualifiedName.substring(lastIndexOfDot + 1);
            assert isNotBlank(atAnnotationName);
            return String.format("@%s", atAnnotationName);
        }
    }
}

上边两个类是我们自定义注释生成器的所有核心内容,我在实现的时候踩了一个小小的坑,在注释生成器接口中有几个 addFieldAnnotation
addClassAnnotation
命名中包含注解字眼的方法,我一开始想当然的以为附加注解需要在这里面实现,后来才发现,两者没什么关系

/**
     * Adds a @Generated annotation to a class.
     *
     * @param innerClass
     *            the class
     * @param introspectedTable
     *            the introspected table
     * @param imports
     *   the comment generator may add a required imported type to this list
     * 
     * @since 1.3.6
     */
     void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable,
                 Set<FullyQualifiedJavaType> imports);

上边是 addClassAnnotation
的注释,可以看到,其根 @Generated
有关。我们自己想要附加注解,实际上还是在附加注释那一步以字符串的形式附加上去的,与这几个 *Annotation
方法无关。

使用

<?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>
    <properties resource="package/to/your/data_source.properties"/>
    <context id="context_mysql" defaultModelType="flat" targetRuntime="MyBatis3">
        <property name="javaFileEncoding" value="UTF-8"/>

        <!-- CommentWithAnnotationCommentGenerator -->
        <commentGenerator type="io.github.since1986.mybatis.comment.generator.CommentWithAnnotationCommentGenerator">
            <property name="fieldAnnotationFullyQualifiedNames" value="one.your.customer.FieldAnnotationClass,another.your.customer.FieldAnnotationClass"/>
            <property name="classAnnotationFullyQualifiedNames" value="one.your.customer.ClassAnnotationClass,another.your.customer.ClassAnnotationClass"/>
        </commentGenerator>

        <jdbcConnection driverClass="${driverClass}" connectionURL="${connectionURL}" userId="${userId}" password="${password}">
            <!-- better keep this -->
            <property name="useInformationSchema" value="true"/>
        </jdbcConnection>

        <javaModelGenerator targetPackage="your.model" targetProject="/path/to/your/project/src/main/java">
        </javaModelGenerator>


        <sqlMapGenerator targetPackage="your.mapper" targetProject="src/main/resources">
        </sqlMapGenerator>


        <javaClientGenerator targetPackage="your.mapper" type="ANNOTATEDMAPPER" targetProject="src/main/java">
        </javaClientGenerator>

        <table tableName="your_table" domainObjectName="YourDomain">
        </table>
    </context>
</generatorConfiguration>

在生成的 Java 中会将数据库注释作为注释,诠释了 DRY
原则,另外付加入了我们需要的框架注解,不用再手工处理重复劳动了,解放了生产力,可以早点下班回家,多休息休息了(做梦中…)

原文 

https://since1986.github.io/blog/bbefb134.html

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » 让 MyBatis Generator 用数据库注释作 Java 注释,并支持附加注解

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址