mybatis-plus 代码生成工具-CURD 实践

工欲善其事,必先利其器!!!

1、pom 依赖

dao 模块 Pom 依赖

<!--  如果项目引入了,要去掉
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>
-->
<!-- 不用mybatis-spring-boot-starter,使用 mybatis-plus-boot-starter -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3.4</version>
</dependency>
 
<!-- mybatis-plus 自动生成代码 依赖-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.5.1</version>
</dependency>
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.30</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.17</version>
</dependency>

2、代码生成类

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.HashMap;
import java.util.Map;

    public class CodeGenerator {

        public static void main(String[] args) {

            String moduleNameOfService = "xxxx";
            String moduleNameOfDao = "xxxx";

            String servicePackageName = "xxxx";
            String serviceImplPackageName = "xxxx";
            String entityPackageName = "xxxx";
            String mapperPackageName = "xxxx";

            // 代码生成器
            FastAutoGenerator
                .create("jdbc:mysql://xxx:xxx/xxx?useUnicode=true","root", "xxxx")
                .globalConfig(builder -> {
                    builder.author("xxxx") // 设置作者
                        .fileOverride(); // 覆盖已生成文件
                })
                .packageConfig(builder -> {
                    String projectPath = System.getProperty("user.dir") + "/";

                    /**
                     * Java源文件的路径
                     */
                    Map<OutputFile, String> pathInfo = new HashMap<>();
                    String sourcePath = "/src/main/java/";
                    //entity 路径
                    pathInfo.put(OutputFile.entity, projectPath + moduleNameOfDao + sourcePath + entityPackageName.replaceAll("\.", "\/"));
                    //mapper 路径
                    pathInfo.put(OutputFile.mapper, projectPath + moduleNameOfDao + sourcePath + mapperPackageName.replaceAll("\.", "\/"));
                    //mapper xml文件 路径
                    pathInfo.put(OutputFile.mapperXml, projectPath + moduleNameOfDao + "/src/main/resources/xxx");
                    //service 路径
                    pathInfo.put(OutputFile.service, projectPath + moduleNameOfService + sourcePath + servicePackageName.replaceAll("\.", "\/"));
                    //service impl 路径
                    pathInfo.put(OutputFile.serviceImpl, projectPath + moduleNameOfService + sourcePath + serviceImplPackageName.replaceAll("\.", "\/"));

                    /**
                     * 类文件里边的包路径:package xxx.xxx.xxx
                     */
                    builder.parent(StringUtils.EMPTY).entity(entityPackageName)
                        .mapper(mapperPackageName)
                        .service(servicePackageName)
                        .serviceImpl(serviceImplPackageName)
                        .pathInfo(pathInfo);

                })
                .strategyConfig(builder -> {
                    /**
                     * 设置需要生成的表名
                     */
                    builder.addInclude("table1", "table2");
                    //mapper注解生效
                    builder.mapperBuilder().enableMapperAnnotation();
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
                .execute();
        }
    }

运行 main 函数即可生成对应文件!!!

Read the fucking manual and source code
原文地址:https://www.cnblogs.com/qxynotebook/p/15481775.html