开发者

MyBatisPlus3.4.3版自动生成代码的使用过程

开发者 https://www.devze.com 2023-04-22 10:22 出处:网络 作者: 吳名氏
目录1 准备工作2 导入依赖3 创建CodeGeneratorTest类4 运行代码生成器5 数据库配置(DataSourceConfig)5.1 基础配置5.2 可选配置5.3 全局配置(GlobalConfig)5.4 包配置(PackageConfig)5.5 模板配置(TemplateConfig)5.
目录
  • 1 准备工作
  • 2 导入依赖
  • 3 创建CodeGeneratorTest类
  • 4 运行代码生成器
  • 5 数据库配置(DataSourceConfig)
    • 5.1 基础配置
    • 5.2 可选配置
    • 5.3 全局配置(GlobalConfig)
    • 5.4 包配置(PackageConfig)
    • 5.5 模板配置(TemplateConfig)
    • 5.6 注入配置(InjectionConfig)
    • 5.7 策略配置(StrategyConfig)
    • 5.8 Entity 策略配置
    • 5.9 Controller 策略配置
    • 5.10 Service 策略配置
    • 5.11 Mapper 策略配置

AutoGenerator 是 MyBATis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。

1 准备工作

创建springboot工程,这里省略。

2 导入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- mybatisplus依赖,注意不用引入mybatis依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3.4</version>
        </dependency>
        <!-- mysql依赖可换成对应版本 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-Java</artifactId>
            <version>8.0.26</version>
        </dependency>
        <!-- mybatis-plus代码生成器依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!-- 自动生成代码的模板引擎,mybatis-plus默认的-->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.3</version>
        </dependency>
        <!-- 由于生成的controller会用到web的注解-->
        <dependency>
            <groupId>org.springframework.boo编程客栈t</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

3 创建CodeGeneratorTest类

需要对

  • 数据源配置,(自己数据库对应的url,username,password等)
  • 全局配置, (作者信息,输出目录等)
  • 包配置,开发者_JAVA (Entity、Mapper、Mapper XML、Service、Controller 等各个模块的包命名等)
  • 策略配置, (配置生成那些表,怎么生成等)
  • 模板配置
  • 注入配置

注意以下路径需要修改为实制项目路径,要生成的数据库表名需要修改为实制的,数据库源url修改为对应的数据源

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import org.junit.Test;

import java.util.Collections;

/**
 * @author: wuKeFan
 * @date: 2022/2/24 10:39
 * @version 1.0
 */
public class CodeGeneratorTest {

    @Test
    public void run() {

        FastAutoGenerator.create(
                        //数据源配置,url需要修改
                        new DataSourceConfig.Builder("url","username","password")
                                .dbQuery(new MySqlQuery())
                                .schema("schema")
                                .typeConvert(new MySqlTypeConvert())
                                .keyWordsHandler(new MySqlKeyWordsHandler())
                )

                //全局配置
                .globalConfig(builder eHfseTJ-> {
                    builder.author("wuKeFan") // 设置作者
                            //.disableOpenDir()//禁止打开输出目录
                            //.enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(System.getProperty("user.dir")+"/src/main/java"); // 指定输出目录
                })

                //包配置
                .packageConfig(builder -> {
                    builder.parent("com.wkf.workrecord.tools.autocode") // 设置父包名,根据实制项目路径修改
                            .moduleName("web")      // 父包名路径下再新建的文件夹
                            .entity("entity")         // 后面这些是sys文件夹里新建的各分类文件夹
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .xml("mapper.xml")
                            .controller("controller")
                            //.other("other")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, System.getProperty("user.dir")+"/src/main/java/com/wkf/workrecord/tools/autocode/web/mapper/xml")); // 存放mapper.xml路径
                })

                //策略配置
                .strategyConfig(builder -> {
                    builder.addInclude("jm_fxy_apply_staging_plan_code") // 设置需要生成的表名
                            .addTablePrefix("jm_") // 设置过滤表前缀
                            .entityBuilder() //实体类配置
                            .enableLombok() //使用lombokwww.devze.com
                            .enableTableFieldAnnotation()//实体类字段注解
                            .controllerBuilder()//controller配置
                            .enableRestStyle()//开启restcontroller
     js                       .mapperBuilder()
                            .enableMapperAnnotation()//开启mapper注解
                            .enableBaseResultMap()//启用 BaseResultMap 生成
                            .enableBaseColumnList();//启用 BaseColumnList
                })
        //.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
        .execute();
    }
}

4 运行代码生成器

点击运行上面的main方法就能自动生成了

生成的效果图:

MyBatisPlus3.4.3版自动生成代码的使用过程

这样代码生成器就写好了,如果需要其他格式模板可以自行参考下面的配置,自行修改

5 数据库配置(DataSourceConfig)

5.1 基础配置

MyBatisPlus3.4.3版自动生成代码的使用过程

5.2 可选配置

MyBatisPlus3.4.3版自动生成代码的使用过程

5.3 全局配置(GlobalConfig)

MyBatisPlus3.4.3版自动生成代码的使用过程

5.4 包配置(PackageConfig)

MyBatisPlus3.4.3版自动生成代码的使用过程

5.5 模板配置(TemplateConfig)

MyBatisPlus3.4.3版自动生成代码的使用过程

5.6 注入配置(InjectionConfig)

MyBatisPlus3.4.3版自动生成代码的使用过程

5.7 策略配置(StrategyConfig)

MyBatisPlus3.4.3版自动生成代码的使用过程

5.8 Entity 策略配置

MyBatisPlus3.4.3版自动生成代码的使用过程

5.9 Controller 策略配置

MyBatisPlus3.4.3版自动生成代码的使用过程

5.10 Service 策略配置

MyBatisPlus3.4.3版自动生成代码的使用过程

5.11 javascriptMapper 策略配置

MyBatisPlus3.4.3版自动生成代码的使用过程

到此这篇关于MyBatisPlus3.4.3版自动生成代码的使用的文章就介绍到这了,更多相关MyBatisPlus自动生成代码内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号