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(","));
}

}
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐