• 【SpringBoot学习】51、MybatisPlus 代码生成器、定制代码模板


    MybatisPlus 代码生成器、定制代码模板

    相关的 pom 版本

    <mybatis-plus-generator.version>3.5.2mybatis-plus-generator.version>
    <freemarker.version>2.3.31freemarker.version>
    
    
    <dependency>
        <groupId>com.baomidougroupId>
        <artifactId>mybatis-plus-generatorartifactId>
        <version>${mybatis-plus-generator.version}version>
    dependency>
    <dependency>
        <groupId>org.freemarkergroupId>
        <artifactId>freemarkerartifactId>
        <version>${freemarker.version}version>
    dependency>
    
    
    
    <dependency>
        <groupId>com.baomidougroupId>
        <artifactId>mybatis-plus-generatorartifactId>
    dependency>
    <dependency>
        <groupId>org.freemarkergroupId>
        <artifactId>freemarkerartifactId>
    dependency>
    
    • 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

    直接上代码

    package com.quickboot;
    
    import cn.hutool.core.io.FileUtil;
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.generator.FastAutoGenerator;
    import com.baomidou.mybatisplus.generator.config.OutputFile;
    import com.baomidou.mybatisplus.generator.config.rules.DateType;
    import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
    
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * MybatisPlus 代码生成器
     * https://baomidou.com/pages/981406/
     *
     * @author Tellsea
     * @date 2022/11/22
     */
    public class MybatisPlusGenerateTest {
    
        public static void main(String[] args) {
            // 参数
            String author = "Tellsea";
            String parentPackage = "com.quickboot";
            String mavenModuleName = "quick-boot-generate";
            String moduleName = "generate";
            String tableName = "sys_dict";
    
            FileUtil.del("D:\\Workspace\\IDEAWorkspace\\quick-boot\\quick-boot\\quick-boot-generate\\src\\main\\java\\com\\quickboot\\generate");
            FileUtil.del("D:\\Workspace\\IDEAWorkspace\\quick-boot\\quick-boot\\quick-boot-generate\\src\\main\\resources\\mapper\\generate");
    
            String rootPath = System.getProperty("user.dir") + "/" + mavenModuleName + "/src/main";
    
            FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/quick-boot", "root", "123456")
                    .globalConfig(builder -> {
                        builder.outputDir(rootPath + "/java")
                                .author(author)
                                .enableSwagger()
                                .dateType(DateType.ONLY_DATE)
                                .commentDate("yyyy-MM-dd");
                    })
                    .packageConfig(builder -> {
                        builder.parent(parentPackage)
                                .moduleName(moduleName)
                                .pathInfo(Collections.singletonMap(OutputFile.xml, rootPath + "/resources/mapper/" + moduleName))
                                .other("vo");
                    })
                    .templateConfig(builder -> {
                        builder.entity("/templates/java/entity.java")
                                .service("/templates/java/service.java")
                                .serviceImpl("/templates/java/serviceImpl.java")
                                .mapper("/templates/java/mapper.java")
                                .xml("/templates/java/mapper.xml")
                                .controller("/templates/java/controller.java");
                    })
                    .strategyConfig(builder -> {
                        builder.enableCapitalMode()
                                .enableSkipView()
                                .disableSqlFilter()
                                .addInclude(tableName)
                                // 实体类
                                .entityBuilder()
                                .disableSerialVersionUID()
                                .enableChainModel()
                                .enableLombok()
                                .naming(NamingStrategy.underline_to_camel)
                                .columnNaming(NamingStrategy.underline_to_camel)
                                //.addSuperEntityColumns("created_by", "created_time", "updated_by", "updated_time", "delete_status")
                                .idType(IdType.AUTO);
                        builder.controllerBuilder().enableRestStyle();
                    })
                    .injectionConfig(builder -> {
                        Map<String, String> customFile = new HashMap<>(16);
                        customFile.put("Vo.java", "/templates/java/vo.java.ftl");
                        customFile.put(".sql", "/templates/sql/sql.ftl");
                        customFile.put(".vue", "/templates/vue/index.vue.ftl");
                        customFile.put(".js", "/templates/js/js.ftl");
                        builder.customFile(customFile);
                    })
                    .templateEngine(new EnhanceFreemarkerTemplateEngine())
                    .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

    生成效果

    • 代码生成器支持自定义[DTO\VO等]模版
    package com.quickboot;
    
    import com.baomidou.mybatisplus.generator.config.OutputFile;
    import com.baomidou.mybatisplus.generator.config.po.TableInfo;
    import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
    
    import javax.validation.constraints.NotNull;
    import java.io.File;
    import java.util.Map;
    
    /**
     * 代码生成器支持自定义[DTO\VO等]模版
     *
     * @author Tellsea
     * @date 2022/11/23
     */
    public final class EnhanceFreemarkerTemplateEngine extends FreemarkerTemplateEngine {
    
        @Override
        protected void outputCustomFile(@NotNull Map<String, String> customFile, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
            String entityName = tableInfo.getEntityName();
            objectMap.put("entityVo", entityName + "Vo");
            String otherPath = this.getPathInfo(OutputFile.other);
            customFile.forEach((key, value) -> {
                String fileName = String.format(otherPath + File.separator + entityName + "%s", key);
                this.outputFile(new File(fileName), objectMap, value);
            });
        }
    }
    
    
    • 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
    • 代码模板从 MybatisPlus 的代码生成器 jar 包复制出来修改即可

  • 相关阅读:
    摩尔投票法(Java)
    【浮点数的存储】
    如何在 Spring Boot 中进行分布式追踪
    车载以太网TSN
    LeetCode 125.验证回文串
    IM同步服务
    聊一聊如何整合Microsoft.Extensions.DependencyInjection和Castle.Core(完结篇)
    Docker常用命令
    Hive UDF 札记
    QSqlRelationTableModel使用示例
  • 原文地址:https://blog.csdn.net/qq_38762237/article/details/128004911