mybatis-plus最新版代码生成器(Swagger3)

2021-10-13

写项目想用mybatis-plus+swagger3,百度最新版代码生成器都是旧版的,且官网的配置过于简洁,所以手敲一份,在官网的基础上加了一堆配置,lombok,restful,mvc三层结构目录等等等等,记录备份

依赖支持

    <dependencies>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--代码生成器构建依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!--mybatis-plus-starter-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3.4</version>
        </dependency>
        <!--默认模板,不引入这个会报错-->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.3</version>
        </dependency>
        <!--swagger3-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
    </dependencies>

快速生成

旧版可以直接通过new对象调用方法,全局配置,包配置等需要创建三个对象,新版只用一个构建类即可完成所有配置,注意需要先调用xxxBuild()方法在调用其他方法

package com.wang;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;

import java.util.Collections;

/**
 * 代码生成器
 *
 * @author whc
 * @since 2021/10/13
 */

public class CodeGenerator {
    static final String URL = "jdbc:mysql://localhost:3306/m_manage?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=true";
    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");//获取项目路径
        FastAutoGenerator.create(URL, "root", "125803")
                //全局配置
                .globalConfig(builder -> {
                    builder.author("whc")
                            .outputDir(projectPath + "/src/main/java")//输出路径
                            .enableSwagger()//开启swagger3
                            .fileOverride()//覆盖文件
                            .disableOpenDir();//不打开文件夹
                })
                //包名配置
                .packageConfig(builder -> {
                    builder.parent("com.wang")
                            .moduleName("blog")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .entity("pojo")
                            .mapper("mapper")
                            //自定义输出路径,mapper.xml生成到resources目录下
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, projectPath + "/src/main/resources/mapper"));
                })
                //策略配置
                .strategyConfig(builder -> {
                    builder.addInclude("student")
                            .addTablePrefix("t_")//表前缀
                            .serviceBuilder().formatServiceFileName("%sService")//去掉Service的 "I" 前缀
                            .controllerBuilder().enableRestStyle()//restful开启
                            .enableHyphenStyle()//url改变 例如:index_id_1
                            .entityBuilder().enableLombok();//开启lombok
                })
                //执行
                .execute();
    }
}
  • 执行效果

交互式生成

package com.wang;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 代码生成器
 *
 * @author whc
 * @since 2021/10/13
 */

public class CodeGenerator {
    static final String URL = "jdbc:mysql://localhost:3306/m_manage?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=true";

    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");//获取项目路径
        FastAutoGenerator.create(URL, "root", "125803")
                //全局配置
                .globalConfig((scanner, builder) -> {
                    builder.author(scanner.apply("请输入作者名?"))
                            .outputDir(projectPath + "/src/main/java")//输出路径
                            .enableSwagger()//开启swagger3
                            .fileOverride()//覆盖文件
                            .disableOpenDir();//不打开文件夹
                })
                //包名配置
                .packageConfig((scanner, builder) -> {
                    builder.parent(scanner.apply("请输入包名?"))
                            .moduleName("blog")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .entity("pojo")
                            .mapper("mapper")
                            //自定义输出路径,mapper.xml生成到resources目录下
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, projectPath + "/src/main/resources/mapper"));
                })
                //策略配置
                .strategyConfig((scanner, builder) -> {
                    builder.addInclude(getTables(scanner.apply("请输入表名,多个英文逗号分隔?所有输入 all")))
                            .addTablePrefix("t_")//表前缀
                            .serviceBuilder().formatServiceFileName("%sService")//去掉Service的 "I" 前缀
                            .controllerBuilder().enableRestStyle()//restful开启
                            .enableHyphenStyle()//url改变 例如:index_id_1
                            .entityBuilder().enableLombok();//开启lombok
                })
                //执行
                .execute();
    }

    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}

  • 执行效果