• springboot项目初始化--mybatisplus代码生成和swagger配置


    1. mybatis代码生成,这里我使用加了两个数据库的表user和student作为测试表

    添加依赖

     
            
                com.baomidou
                mybatis-plus-generator
                3.5.1
            
            
            
                org.freemarker
                freemarker
                2.3.28
            
            
            
                org.apache.velocity
                velocity-engine-core
                2.3
            
            
                mysql
                mysql-connector-java
                8.0.27
            
    
            
                com.baomidou
                mybatis-plus-boot-starter
                3.5.2
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    写properties文件(后面测试代码用)

    server.port=8083
    #数据库配置
    spring.datasource.name=imocc_mall_datasource
    spring.datasource.username=root
    spring.datasource.password=123123
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db1?serverTimezone=Asia/Shanghai
    #mybatis.mapper-locations= classpath:mappers/*.xml
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    代码生成类(启动它就可以生成代码啦!!)

    package com.example.demo;
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.baomidou.mybatisplus.generator.FastAutoGenerator;
    import com.baomidou.mybatisplus.generator.config.OutputFile;
    import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    /**
     * @Author LMGD
     * @Date 2022/2/10 16:21
     */
    public class AutoGenerator {
    
        private static final String url = "jdbc:mysql://localhost:3306/db1?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8";
        private static final String username = "root";
        private static final String password = "123123";
    
        public static void main(String[] args) {
            List tables = new ArrayList<>();
            tables.add("student"); //数据库表名称,一个user表一个student表
            tables.add("user");
    
            FastAutoGenerator.create(url, username, password)
                    .globalConfig(builder -> {
                        builder.author("wangle")               //作者
                                .outputDir(System.getProperty("user.dir") + "\\src\\main\\java")    //输出路径(写到java目录)
                                .enableSwagger()           //开启swagger
                                .commentDate("yyyy-MM-dd")
                                .fileOverride();            //开启覆盖之前生成的文件
    
                    })
                    .packageConfig(builder -> {
                        builder.parent("com.example")    //这里修改你的包名
                                .moduleName("demo")
                                .entity("entity")
                                .service("service")
                                .serviceImpl("service.serviceImpl")
                                .controller("controller")
                                .mapper("mapper")
                                .xml("mapper")
                                .pathInfo(Collections.singletonMap(OutputFile.mapperXml, System.getProperty("user.dir") + "\\src\\main\\resources\\mapper"));
                    })
                    .strategyConfig(builder -> {
                        builder.addInclude(tables)
                                .addTablePrefix("p_")
                                .serviceBuilder()
                                .formatServiceFileName("%sService")
                                .formatServiceImplFileName("%sServiceImpl")
                                .entityBuilder()
                                .enableLombok()
                                .logicDeleteColumnName("deleted")
                                .enableTableFieldAnnotation()
                                .controllerBuilder()
                                .formatFileName("%sController")
                                .enableRestStyle()
                                .mapperBuilder()
                                .enableBaseResultMap()  //生成通用的resultMap
                                .superClass(BaseMapper.class)
                                .formatMapperFileName("%sMapper")
                                .enableMapperAnnotation()
                                .formatXmlFileName("%sMapper");
                    })
                    .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
                    .execute();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    1. swagger添加

    依赖

      
                io.springfox
                springfox-swagger2
                2.9.2
            
            
                io.springfox
                springfox-swagger-ui
                2.9.2
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    配置类

    package com.example.demo.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    
    @Configuration
    public class SpringFoxConfig {
    
        //访问http://localhost:8083/swagger-ui.html可以看到API文档
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("测试mybatisplus")
                    .description("")
                    .termsOfServiceUrl("")
                    .build();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    添加注解到启动类

    
    @EnableSwagger2
    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    基于Java+控制台实现教材管理系统
    【预约观看】Ambire 智能钱包 AMA 活动第四期即将举行
    Spring之@Qualifier注解简介及示例
    我说HashMap初始容量是16,面试官让我回去等通知
    【Bug】Ubuntu 有线设置打不开无反应
    Post请求出现Request header is too large
    点云从入门到精通技术详解100篇-基于三维点云的路况语义分割(续)
    ubuntu utopic unicorn static ip
    React之Hook
    tokenizers normalizers模块
  • 原文地址:https://blog.csdn.net/weixin_42821448/article/details/126042720