• Java系列 - 新版MybatisPlus代码生成


    最新版本的 mybatis-plus-generator 已经到了 3.5.2.

    3.5.0以下版本使用方法,网上已经很多相关的教程。

    本文针对的是 mybatis-plus-generator >= 3.5.0

    <!--  Mybatis-plus -->
     <dependency>
         <groupId>com.baomidou</groupId>
         <artifactId>mybatis-plus-generator</artifactId>
         <version>3.5.1</version>
     </dependency>
     <dependency>
         <groupId>org.apache.velocity</groupId>
         <artifactId>velocity-engine-core</artifactId>
         <version>2.3</version>
     </dependency>
     
     <!--  Swagger2 接口文档 可选 -->
     <dependency>
         <groupId>io.springfox</groupId>
         <artifactId>springfox-swagger-ui</artifactId>
         <version>2.9.2</version>
     </dependency>
     <dependency>
         <groupId>io.springfox</groupId>
         <artifactId>springfox-swagger2</artifactId>
         <version>2.9.2</version>
     </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    生成器

    package com.cool.reggie.config;
    
    import com.baomidou.mybatisplus.annotation.FieldFill;
    import com.baomidou.mybatisplus.generator.FastAutoGenerator;
    import com.baomidou.mybatisplus.generator.config.*;
    import com.baomidou.mybatisplus.generator.config.po.TableField;
    import com.baomidou.mybatisplus.generator.config.po.TableInfo;
    import com.baomidou.mybatisplus.generator.config.rules.DateType;
    import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
    import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
    import com.baomidou.mybatisplus.generator.fill.Column;
    import com.baomidou.mybatisplus.generator.function.ConverterFileName;
    import org.jetbrains.annotations.NotNull;
    
    import java.util.Collections;
    
    /**
     * 代码生成器
     * mybatis-plus-generator >= 3.5.0
     *
     * @author ifredom
     * @date 2022/07/01
     */
    public class AutoCodeGenerator {
        /**
         * 项目根目录
         */
        private static final String PROJECT_ROOT_PATH = System.getProperty("user.dir");
    
        public static void main(String[] args) {
            // 1、数据源配置
            DataSourceConfig.Builder datasourceBuilder = new DataSourceConfig.Builder(
                    "jdbc:mysql://localhost:3306/reggie?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC",
                    "root",
                    "123456"
            );
    
            // 2、创建代码生成器对象
            FastAutoGenerator generator = FastAutoGenerator.create(datasourceBuilder);
    
            // 3、全局配置
            generator.globalConfig(builder -> {
                // 设置作者
                builder.author("1950735817@qq.com")
                        // 定义生成的实体类中日期类型
                        .dateType(DateType.ONLY_DATE)
                        // 注释时间策略。
                        .commentDate("yyyy-MM-dd")
                        // 生成后是否打开资源管理器:否
                        .disableOpenDir()
                        // 重新生成时是否覆盖原文件:是
                        .fileOverride()
                        // 开启 swagger 模式。如果开启,需要导入 swagger 的 pom 依赖
                        .enableSwagger()
                        // 覆盖已生成文件
                        .fileOverride()
                        // 指定输出目录
                        .outputDir(PROJECT_ROOT_PATH + "/src/main/java");
            });
    
            // 4、包配置
            generator.packageConfig(builder -> {
                // 父包名. ===========1.手动修改设置===========
                builder.parent("com.cool.reggie.modules")
                        // 将要生成的模块名称. ===========2.手动修改设置===========
                        .moduleName("employee")
                        // 设置生成的 控制层 文件夹名称
                        .controller("controller")
                        // 设置生成的 实体类 文件夹名称
                        .entity("entity")
                        // 设置生成的 服务层 文件夹名称
                        .service("service")
                        // 设置生成的 映射层 文件夹名称
                        .mapper("mapper")
                        // mapper.xml 文件路径。单模块下,其他文件路径默认即可。 ;
                        .pathInfo(Collections.singletonMap(OutputFile.mapperXml, PROJECT_ROOT_PATH + "/src/main/resources/mapper"));
            });
    
            // 5、策略配置
            generator.strategyConfig(builder -> {
                // 希望将数据库中的哪张表生成. 如果不设置,则会将所有表都生成。 ===========3.手动修改设置。===========
                builder.addInclude("ta_employee")
                        // 过滤表前缀,生成的类名会去掉这个前缀
                        .addTablePrefix("ta_", "sf_", "sk_")
    
                        // 第一阶段
                        // 是否生成 entity:是
                        .entityBuilder()
                        // 开启lombok
                        .enableLombok()
                        // 设置生成的实体类名称. 默认配置不带 Entity 后缀,我习惯添加上
                        .convertFileName(entityName -> entityName + "Entity")
                        // 开启实体时字段注解。 会在生成的实体类的字段上,添加注解: @TableField("nickname")
                        .enableTableFieldAnnotation()
                        // 逻辑删除字段名。(与数据库中字段对应)
                        .logicDeleteColumnName("is_delete")
                        // 逻辑删除属性名。(定义在实体中的属性)。 会在生成的实体类的字段上,添加注解:@TableLogic
                        .logicDeletePropertyName("isDelete")
                        // 会在实体类的该字段上追加注解[@TableField(value = "create_time", fill = FieldFill.INSERT)]
                        .addTableFills(new Column("create_time", FieldFill.INSERT))
                        // 会在实体类的该字段上追加注解[@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)]
                        .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE))
    
                        // 第二阶段
                        .mapperBuilder()
                        // 开启 @Mapper 注解。 会在生成的 mapper 类上,添加注解:@Mapper
                        .enableMapperAnnotation()
                        // 启用 BaseResultMap 生成。 会在 mapper.xml文件生成[通用查询映射结果]配置。
                        .enableBaseResultMap()
                        // 启用 BaseColumnList。 会在mapper.xml文件生成[通用查询结果列 ]配置
                        .enableBaseColumnList()
    
                        // 第三阶段
                        .serviceBuilder()
                        // 设置生成的实体类名称。 默认配置名称前有个I,我习惯添去掉
                        .convertServiceFileName(serviceName -> serviceName + "Service")
                        // 第四阶段
                        .controllerBuilder()
                        // 开启 @RestController 注解。 会在生成的 Controller 类上,添加注解:@RestController
                        .enableRestStyle()
    
                        // 开启驼峰转连字符
                        .enableHyphenStyle();
            });
    
            // 6. 模板引擎配置,默认为 Velocity , 可选模板引擎 Freemarker 或 Beetl
            // generator.templateEngine(new FreemarkerTemplateEngine());
    
            generator.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
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132

    如果 不设置 addInclude(“ta_employee”),那么会将所有表都生成,并在同一个目录模块下。
    在这里插入图片描述
    最终效果:

    在这里插入图片描述

    ------ 如果文章对你有用,感谢右上角 >>>点赞 | 收藏 <<<

  • 相关阅读:
    CTF 全讲解:[SWPUCTF 2021 新生赛]jicao
    Android gradle编译错误OOM:java heap space的解决办法
    ChatGPT⼊门到精通(7):GPT3.5与 4.0区别
    C#验证码
    博客网页制作基础大二dw作业 web课程设计网页制作 个人网页设计与实现 我的个人博客网页开发
    php实战案例记录(16)php://input输入流
    【FreeRTOS】FreeRTOS删除任务vTaskDelete()
    从零学算法(LCR 178)
    批量调整视频饱和度和色度,提升你的视频剪辑效率!
    React-hooks有哪些用法?
  • 原文地址:https://blog.csdn.net/win7583362/article/details/125556048