Mybatis反向生成+Swagger自动注释

Mybatis反向生成可以采用mybatis-generator工具
工具的调用方式我选择采用run-with-java 即使用java main函数调用,这种方式的有点是相比maven-plugin调用,省去了jdbc driver包定位的问题,同时方便添加自定义插件,本文就采用了自定义的注释插件来获取数据库字段注释,形成field注释。
同时引入的公用插件有

  • com.github.misterchangray.mybatis.generator.plugin 用于生成swagger注释
  • com.itfsw 该插件包整合了20多个插件,常用的包括lombok支持、批量插入等。

pom.xml

        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.5
        
        
        
            com.github.misterchangray.mybatis.generator.plugins
            myBatisGeneratorPlugins
            1.2
        
        
        
            com.itfsw
            mybatis-generator-plugin
            1.3.5
        

反向代理规则文件 mybatis-generator.xml






    
        
        
            
            
        
        
        
            
        
        
        
            
            
            
            
            
            
            
            
            
            
            
        
        
        
            
            
        
        
        
            
            
            
            
        

        

        
            
            
        
        
        
        
        

        
        
            
        

        
        
            
            
            
            
        

        
        
            
        

        
        
            
        

        
        

最后在java类main函数调用
这里需要注意 File 路径默认以idea工程根目录为 初始目录,因此一下代码需要将mybatis-generator.xml放在项目根目录,而非resource目录

package com.yao.dataforsea_bg.db;

/**
 * @Author: wpy
 * @Email: [email protected]
 * @Date: 2020-03-31 21:06
 */
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Generator {
    public static void main( String[] args ) throws Exception {
        List warnings = new ArrayList<>();
        File configFile = new File("./mybatis-generator.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(true);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
}

最后如果仔细看mybatis-generator可以发现,我使用了一个自定义的commentGenerator,它的作用是使用数据库remark作为model类field注释,去除原始生成注释包含的不必要信息,它包括定义接口实现两个类

  1. 父类实现接口
/**
 * @Author: wpy
 * @Email: [email protected]
 * @Date: 2020-03-31 21:01
 */
public class EmptyCommentGenerator 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 b) {

    }

    @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 xmlElement) {

    }

}
  1. 子类集成父类
/**
 * @Author: wpy
 * @Email: [email protected]
 * @Date: 2020-03-31 20:57
 */
public class MySQLCommentGenerator extends EmptyCommentGenerator {
    private Properties properties;

    public MySQLCommentGenerator() {
        properties = new Properties();
    }

    @Override
    public void addConfigurationProperties(Properties properties) {
        // 获取自定义的 properties
        this.properties.putAll(properties);
    }

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        String author = properties.getProperty("author");
        String dateFormat = properties.getProperty("dateFormat", "yyyy-MM-dd");
        SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormat);

        // 获取表注释
        String remarks = introspectedTable.getRemarks();

        topLevelClass.addJavaDocLine("/**");
        topLevelClass.addJavaDocLine(" * " + remarks);
        topLevelClass.addJavaDocLine(" *");
        topLevelClass.addJavaDocLine(" * @author " + author);
        topLevelClass.addJavaDocLine(" * @date " + dateFormatter.format(new Date()));
        topLevelClass.addJavaDocLine(" */");
    }

    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        // 获取列注释
        String remarks = introspectedColumn.getRemarks();
        field.addJavaDocLine("/**");
        field.addJavaDocLine(" * " + remarks);
        field.addJavaDocLine(" */");
    }
}

你可能感兴趣的:(swagger)