mybatis-plus3.5.1,代码生成器集成(自定义模板)

package com.theiavis.workhour.common.utils.generatorCode;


import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *代码生成器
 * @author lyh
 * @since 2021-12-01
 */
public class GeneratorCode {
    public static String path= "C:\\Users\\dell G3-5\\Desktop\\项目路径\\work-hour-dev\\test\\";
    public static void main(String[] args) {
        // 配置模板
        new TemplateConfig.Builder().serviceImpl("/templates/serviceImpl.java");
        FastAutoGenerator.create( "jdbc:mysql://localhost:3306/work_hour_dev?serverTimezone=UTC",
                "root",
                "root")

                // 全局配置,生成的代码路径
                .globalConfig((scanner, builder) ->
                        builder.author(scanner.apply("请输入作者名称?")).fileOverride().outputDir(path))
                // 包配置

                .templateEngine(new FreemarkerTemplateEngine())
                .packageConfig((scanner, builder) -> builder.parent("com")//配置包路径
                        .entity("entity")
                        .service("service")
                        .serviceImpl("serviceImpl")
                        .controller("controller")
                        .mapper("mapper")
                        .xml("mapper")
                        .pathInfo(Collections.singletonMap(OutputFile.mapperXml,
                                path+"com\\resources\\mapper"))
                )
                // 策略配置
                .strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名,多个英文逗号分隔?所有输入 all")))
                        .controllerBuilder().enableRestStyle().enableHyphenStyle()
                        .entityBuilder().enableLombok().addTableFills(
                                new Column("create_time", FieldFill.INSERT)
                        ).build())

                .execute();
        new InjectionConfig.Builder()
                .beforeOutputFile((tableInfo, objectMap) -> {
                    System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size());
                })
                .customFile(Collections.singletonMap("mapper.xml", "/templates/mapper.xml.ftl"))
                .customFile(Collections.singletonMap("bean.java", "/templates/bean.java.ftl"))
                .customFile(Collections.singletonMap("service.java", "/templates/service.java.ftl"))
                .customFile(Collections.singletonMap("serviceImpl.java", "/templates/serviceImpl.java.ftl"))
                .customFile(Collections.singletonMap("mapper.java", "/templates/mapper.java.ftl"))
                .build();



    }



protected static List<String> getTables(String tables) {
    return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
}

}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值