ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-10-29 18:33:53  阅读:220  来源: 互联网

标签:代码生成 OutputFile xxxx String pathInfo builder xxx CURD plus


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

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 函数即可生成对应文件!!!

标签:代码生成,OutputFile,xxxx,String,pathInfo,builder,xxx,CURD,plus
来源: https://www.cnblogs.com/qxynotebook/p/15481775.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有