Mybatis-Plus代码生成器新版配置

2021-12-27
3314
23
9

温馨提示:本文最后更新于 2023-07-07,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

适用版本:mybatis-plus-generator 3.5.1 及其以上版本,对历史版本不兼容。我只需要生成daomodelmapper,提供定制生成模板代码示例

官方教程:https://baomidou.com/pages/779a6e/

pom.xml引入依赖

<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>

复制模板文件到项目

https://github.com/baomidou/generator/tree/develop/mybatis-plus-generator/src/main/resources/templates
image.png

生成代码main方法代码示例

需要service等可以放入对应模板和指定模板builder.service("/generator/service.java")设置

package cn.ximiyun.love_box;

import cn.hutool.setting.dialect.Props;
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.po.LikeTable;
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.engine.VelocityTemplateEngine;

import java.util.Collections;

/**
 * MyBatis-Plus自动生成代码类
 * Created by GMQ on 2021/12/27 14:55
 */
public class CodeGenerator {

    public static void main(String[] args) {

        String projectPath = System.getProperty("user.dir");
        Props application = new Props(projectPath + "/src/main/resources/application.properties");

        String os = "love_box";
        String packageName = "cn.ximiyun." + os;
        String dataSourceName = application.getStr("spring.datasource.username");
        String dataSourcePassword = application.getStr("spring.datasource.password");
        String dataSourceUrl = application.getStr("spring.datasource.url");
        //需要生成的表
        String[] tables = new String[]{"base_dict_conf"};
        //是否生成model
        boolean isModel = true;
        //是否生成dao
        boolean isDao = true;
        //是否生成Mapper
        boolean isMapper = true;

        FastAutoGenerator.create(dataSourceUrl, dataSourceName, dataSourcePassword)
                .globalConfig(builder -> {
                    builder.author("GMQ") // 设置作者
                            .fileOverride() // 覆盖已生成文件
                            .disableOpenDir()
                            .dateType(DateType.ONLY_DATE)
                            .outputDir(projectPath + "/src/main/java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(packageName) // 设置父包名
                            .entity("model")
                            .mapper("dao")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, projectPath + "/src/main/resources/mapper/" + os + "/")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables)// 设置需要生成的表名
//                            .addTablePrefix(tables)//表前缀
                                .entityBuilder()
                                .naming(NamingStrategy.underline_to_camel)
                                .columnNaming(NamingStrategy.underline_to_camel)
                                .idType(IdType.ASSIGN_ID)
                                .formatFileName("%sModel")
                                .mapperBuilder()
                                .enableMapperAnnotation()
                                .formatMapperFileName("%sDao")
                                .mapperBuilder()
                                .formatXmlFileName("%sMapper").build();
                })
                .templateConfig(builder -> {
                    if (isModel) {
                        builder.entity("/generator/entity.java");
                    }else {
                        builder.entity(null);
                    }
                    if (isDao) {
                        builder.mapper("/generator/mapper.java");
                    }else {
                        builder.mapper(null);
                    }
                    if (isMapper) {
                        builder.mapperXml("/generator/mapper.xml");
                    }else {
                        builder.mapperXml(null);
                    }
                    builder.controller(null);
                    builder.service(null);
                    builder.serviceImpl(null);
                    builder.build();
                })
                .templateEngine(new VelocityTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
                .execute();

    }

}

image.png
image.png


评论

Polar.
最重要的,是拥有跟随内心与直觉的勇气

文章目录