MyBatis Plus 代码生成器3.5.0 相关配置

参考资料:

MyBatis Plus 官方文档

https://mp.baomidou.com/guide/generator.html

狂神的MyBatis Plus教程

https://www.bilibili.com/video/BV17E411N7KN?p=16&t=302

依赖

<!-- 代码生成器 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.5.0</version>
</dependency>
<!-- 模板引擎 -->
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.3</version>
</dependency>

MyBatis Plus代码生成器配置

        近期在学习MyBatis Plus,发现官方文档中的Java代码不可用,不少配置是用了Builder,查看了下接口以及实现类之间的关系,大致使用方式如下:

package com.nori.demo.code;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.IFill;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.fill.Property;

public class CodeGenerator {

    public static void main(String[] args) {
        // 数据源配置
        DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig
                .Builder("jdbc:mysql://localhost:3306/db_mineanniversary?serverTimezone=Asia/Shanghai", "root", "123123");

        // 全局配置
        GlobalConfig.Builder globalConfigBuilder = new GlobalConfig.Builder();
        // 代码生成目录
        String projectPath = System.getProperty("user.dir");
        globalConfigBuilder.outputDir(projectPath + "/src/main/java");
        // 作者
        globalConfigBuilder.author("Nori");
        // 结束时是否打开文件夹
        globalConfigBuilder.openDir(true);
        // 是否覆盖旧的文件
        globalConfigBuilder.fileOverride();
        // 实体属性Swagger2注解
        //globalConfigBuilder.enableSwagger();

        // 包配置,如模块名、实体、mapper、service、controller等
        PackageConfig.Builder packageConfigBuilder = new PackageConfig.Builder();
        packageConfigBuilder.moduleName("core");
        packageConfigBuilder.parent("com.nori.demo");
        packageConfigBuilder.entity("entity");
        packageConfigBuilder.mapper("mapper");
        packageConfigBuilder.service("service");
        //packageConfigBuilder.serviceImpl("");
        packageConfigBuilder.controller("controller");

        // 策略配置
        StrategyConfig.Builder strategyConfigBuilder = new StrategyConfig.Builder();
        // 设置需要映射的表名
        strategyConfigBuilder.addInclude("t_user");
        // 下划线转驼峰
        strategyConfigBuilder.entityBuilder().naming(NamingStrategy.underline_to_camel);
        strategyConfigBuilder.entityBuilder().columnNaming(NamingStrategy.underline_to_camel);
        // entity的Lombok
        strategyConfigBuilder.entityBuilder().enableLombok();
        // 逻辑删除
        strategyConfigBuilder.entityBuilder().logicDeleteColumnName("deleted");
        strategyConfigBuilder.entityBuilder().logicDeletePropertyName("deleted");
        // 自动填充
        // 创建时间
        IFill gmtCreate = new Property("gmt_create", FieldFill.INSERT);
        // 更新时间
        IFill gmtModified = new Property("gmt_modified", FieldFill.INSERT_UPDATE);
        strategyConfigBuilder.entityBuilder().addTableFills(gmtCreate, gmtModified);
        // 乐观锁
        strategyConfigBuilder.entityBuilder().versionColumnName("version");
        strategyConfigBuilder.entityBuilder().versionPropertyName("version");
        // 使用Restful风格的Controller
        strategyConfigBuilder.controllerBuilder().enableRestStyle();
        // 将请求地址转换为驼峰命名,如 http://localhost:8080/hello_id_2
        strategyConfigBuilder.controllerBuilder().enableHyphenStyle();

        // 自定义配置
        //InjectionConfig.Builder injectionConfigBuilder = new InjectionConfig.Builder();

        // 模板引擎配置
        TemplateConfig.Builder templateConfigBuilder = new TemplateConfig.Builder();
        // 如果模板引擎是 freemarker
        //String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
        //String templatePath = "/templates/mapper.xml.vm";
        //templateConfigBuilder.entity()

        // 创建代码生成器对象,加载配置
        AutoGenerator autoGenerator = new AutoGenerator(dataSourceConfigBuilder.build());
        autoGenerator.global(globalConfigBuilder.build());
        autoGenerator.packageInfo(packageConfigBuilder.build());
        autoGenerator.strategy(strategyConfigBuilder.build());
        //autoGenerator.injection(injectionConfigBuilder.build());
        //autoGenerator.template();

        // 执行
        autoGenerator.execute();
    }

}
上一篇:Electron 打包问题:electron-builder 下载各种依赖出错(推荐)


下一篇:Errors occurred during the build. Errors running builder 终版解决方案