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;

/**
 * 

* *

*代码生成器 * @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(",")); } }

你可能感兴趣的:(java,mysql,开发语言)