Mybatis-plus最新代码生成器配置

package com.zuijin.vue2blog.generator;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
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.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.function.ConverterFileName;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Collections;

/**
 * ClassName:    Generator
 * CreateBy:     IntelliJ IDEA
 * Author:       醉瑾
 * Date:         2022-04-16
 * Description : mp 代码生成器
 */
public class Generator {
    /*
     * 数据源配置
     * */
    private static final String URL = "jdbc:mysql://localhost:3306/vue2blog?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";
    private static final String username = "root";
    private static final String password = "admin";

    private static final String projectPath = System.getProperty("user.dir");

    public static void main(String[] args) {
        System.out.println(projectPath);
        FastAutoGenerator.create(URL, username, password)
                // 全局配置
                .globalConfig(builder -> {
                    builder.author("醉瑾")   // 作者
                            .fileOverride()    // 覆盖已生成文件
                            .outputDir(projectPath + "/src/main/java")     // 指定输出目录
                            .enableSwagger()   // 开启 swagger 的 api
                            .disableOpenDir()  // 禁止打开输出目录
                            .dateType(DateType.ONLY_DATE) // 时间策略
                            .commentDate("yyyy-MM-dd") //注释日期
                            .build();
                })
                // 包配置
                .packageConfig(builder -> {
                    builder.parent("com.zuijin.vue2blog")   // 父包名
                            .moduleName("")                 // 父包模块名
                            .entity("entity")               // Entity 包名
                            .service("service")             // service 包名
                            .serviceImpl("service.impl")    //
                            .mapper("mapper")               //
                            .xml("mapper.xml")              // MapperXML 包名
                            .controller("controller")       //
                            .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + "/src/main/resources/mapper")) // 自定义某个文件的路径
                            .build();
                })
                // 策略配置
                .strategyConfig(builder -> {
                    builder.addTablePrefix("%")
                            .entityBuilder()  // 实体类配置
                            .enableTableFieldAnnotation()
                            .idType(IdType.AUTO)
                            .controllerBuilder() // controller 接口配置
                            .enableHyphenStyle() // 开启驼峰转连字符
                            .enableRestStyle()
                            .serviceBuilder() // service 配置
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .mapperBuilder()  //mapper 配置
                            .superClass(BaseMapper.class)
                            .enableMapperAnnotation()
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapperXml")
                            .build();
                })
                // 模板配置
                .templateEngine(new VelocityTemplateEngine()) // 默认
                .templateConfig(builder -> {
                    builder.controller("/templates/controller.java") // 自定义 controller 模板
                            .build();
                })
                .execute();
    }
}

个性化自定义模板在下包中
在这里插入图片描述
复制文件后自定义修改即可
例如:

package ${package.Controller};

import $!{package.Service}.$!{table.serviceName};
import org.springframework.web.bind.annotation.RequestMapping;
#if(${swagger})
import io.swagger.annotations.Api;
#end
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

#if(${superControllerClassPackage})
import ${superControllerClassPackage};
#end

/**
 * <p>
 * $!{table.comment} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
#if(${swagger})
@Api(tags = "${table.comment}")
#end
#if(${restControllerStyle})
@RestController
#else
@Controller
#end
@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen.replace("-","/")}#else${table.entityPath}#end")
#if(${kotlin})
class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}()#end

#else
#if(${superControllerClass})
public class ${table.controllerName} extends ${superControllerClass} {
#else
public class ${table.controllerName} {
#end
    @Resource
    private ${table.serviceName} ${table.entityPath}Service;
}
#end

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心醉瑶瑾前

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值