栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot项目整合mybatis生成Criteria的逆向工程,并生成swagger文档注释

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot项目整合mybatis生成Criteria的逆向工程,并生成swagger文档注释

pom.xml

       
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.baomidou
            mybatis-plus-generator
            3.1.0
        

        
        
            io.springfox
            springfox-swagger2
            2.7.0
        
        
            io.springfox
            springfox-swagger-ui
            2.7.0
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        


        
        
            mysql
            mysql-connector-java
            8.0.15
        
        
        
            org.freemarker
            freemarker
            2.3.28
        


        
        
            org.projectlombok
            lombok
            1.18.2
        
        

        
              
                  com.baomidou
                  mybatis-plus
                  2.3
              
        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.7
        

        
        
            junit
            junit
            4.9
        
        
        
            log4j
            log4j
            1.2.17
        
        
        
            com.mchange
            c3p0
            0.9.5.2
        

        
            org.springframework
            spring-context
            4.3.10.RELEASE
        
        
            org.springframework
            spring-orm
            4.3.10.RELEASE
        

        
        
            org.apache.velocity
            velocity-engine-core
            2.0
        

        
        
            org.slf4j
            slf4j-api
            1.7.7
        
        
            org.slf4j
            slf4j-log4j12
            1.7.7
        

写一个配置数据源的文件generator.properties

jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://xxx.xxx.x.xxx:3306/db_name?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
jdbc.userId=root
jdbc.password=root

写一个配置generatorConfig.xml文件





    
    
        
        
        
        
        
        
        
        
        
        
        
            
            
            
            
        
        
        
            
            
        
        
        

        
        
        
        
        
        
        

写一个java配置类

public class CommentGenerator extends DefaultCommentGenerator {
    private boolean addRemarkComments = false;
    private static final String EXAMPLE_SUFFIX="Example";
    private static final String API_MODEL_PROPERTY_FULL_CLASS_NAME="io.swagger.annotations.ApiModelProperty";

    
    @Override
    public void addConfigurationProperties(Properties properties) {
        super.addConfigurationProperties(properties);
        this.addRemarkComments = StringUtility.isTrue(properties.getProperty("addRemarkComments"));
    }

    
    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable,
                                IntrospectedColumn introspectedColumn) {
        String remarks = introspectedColumn.getRemarks();
        //根据参数和备注信息判断是否添加备注信息
        if(addRemarkComments&& StringUtility.stringHasValue(remarks)){
//            addFieldJavaDoc(field, remarks);
            //数据库中特殊字符需要转义
            if(remarks.contains(""")){
                remarks = remarks.replace(""","'");
            }
            //给model的字段添加swagger注解
            field.addJavaDocLine("@ApiModelProperty(value = ""+remarks+"")");
        }
    }

    
    private void addFieldJavaDoc(Field field, String remarks) {
        //文档注释开始
        field.addJavaDocLine("");
    }

    @Override
    public void addJavaFileComment(CompilationUnit compilationUnit) {
        super.addJavaFileComment(compilationUnit);
        //只在model中添加swagger注解类的导入
        if(!compilationUnit.isJavaInterface()&&!compilationUnit.getType().getFullyQualifiedName().contains(EXAMPLE_SUFFIX)){
            compilationUnit.addimportedType(new FullyQualifiedJavaType(API_MODEL_PROPERTY_FULL_CLASS_NAME));
        }
    }

}

写一个代码生成器

public class Generator {
    public static void main(String[] args) throws Exception {
        //MBG 执行过程中的警告信息
        List warnings = new ArrayList();
        //当生成的代码重复时,覆盖原代码
        boolean overwrite = true;
        //读取我们的 MBG 配置文件
        InputStream is = Generator.class.getResourceAsStream("/generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(is);
        is.close();

        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        //创建 MBG
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        //执行生成代码
        myBatisGenerator.generate(null);
        //输出警告信息
        for (String warning : warnings) {
            System.out.println(warning);
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/703790.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号