依赖引入
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>latest-freemarker-version</version>
</dependency>
构建代码
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import org.apache.poi.xwpf.usermodel.TOC;
public class GenerationUtil {
private final static String CONTROLLER_PATH = "E:/sts_workspace/test/console";
private final static String EXCEPT_CONTROLLER_PATH = "E:/sts_workspace/test/core";
private final static String PARENT_PACKAGE = "com.sapr";
private final static String CONTROLLER_PACKAGE = "console.controller.business";
private final static String ENTITY_PACKAGE = "core.datasource.entity.business";
private final static String SERVICE_PACKAGE = "core.service.business";
private final static String SERVICEIMPL_PACKAGE = "core.service.impl.business";
private final static String MAPPER_PACKAGE = "core.repository.business";
private final static String REQUEST_PACKAGE = "console.request.business";
private final static String PARAM_PACKAGE = "console.param.business";
private final static String VO_PACKAGE = "console.vo.business";
private final static String DTO_PACKAGE = "console.vo.business";
private final static String MAPPERXML_PACKAGE = "business";
private final static String TABLE_PREFIX = "t_";
private final static String DATASOURCE_DRIVER = "com.mysql.cj.jdbc.Driver";
private final static String DATASOURCE_URL = "jdbc:mysql://127.0.0.1:3306/test?useSSL=false&useUnicode=true"
+ "&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&nullCatalogMeansCurrent=true&autoReconnect=true&serverTimezone=GMT%2B8&allowMultiQueries=true";
private final static String DATASOURCE_USERNAME = "root";
private final static String DATASOURCE_PASSWORD = "root";
private final static String TABLENAME = "t_test";
public static void main(String[] args) {
}
public static String toCamelString() {
String camelStr = new String("");
String[] split = TABLENAME.replaceAll(TABLE_PREFIX, "").split("_");
for (int i = 0; i < split.length; i++) {
String concat = split[i].substring(0, 1).toUpperCase().concat(split[i].substring(1));
camelStr = camelStr.concat(concat);
}
return camelStr;
}
public static void gererateController() {
AutoGenerator mpg = getAutoGenerator();
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController("templates/controller.java");
templateConfig.setService("");
templateConfig.setEntity("");
templateConfig.setMapper("");
templateConfig.setXml("");
templateConfig.setServiceImpl("");
mpg.setTemplate(templateConfig);
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setSuperEntityColumns("id");
strategy.setInclude(TABLENAME);
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(TABLE_PREFIX);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new VelocityTemplateEngine());
mpg.execute();
}
public static void gererateExceptController() {
AutoGenerator mpg = getAutoGenerator();
PackageConfig pc = new PackageConfig();
pc.setParent(PARENT_PACKAGE);
pc.setEntity(ENTITY_PACKAGE);
pc.setMapper(MAPPER_PACKAGE);
pc.setService(SERVICE_PACKAGE);
pc.setServiceImpl(SERVICEIMPL_PACKAGE);
mpg.setPackageInfo(pc);
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
}
};
String templatePath = "/templates/mapper.xml.vm";
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return CONTROLLER_PATH + "/src/main/resources/mapper/" + MAPPERXML_PACKAGE + "/"
+ tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController("");
templateConfig.setEntity("");
templateConfig.setMapper("templates/mapper.java");
templateConfig.setService("templates/service.java");
templateConfig.setServiceImpl("templates/serviceImpl.java");
templateConfig.setXml("");
mpg.setTemplate(templateConfig);
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setSuperEntityColumns("id");
strategy.setInclude(TABLENAME);
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(TABLE_PREFIX);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new VelocityTemplateEngine());
mpg.execute();
}
public static void generateRequest() {
AutoGenerator mpg = getAutoGenerator();
GlobalConfig config = mpg.getGlobalConfig();
config.setEntityName(toCamelString().concat("Request"));
mpg.setGlobalConfig(config);
mpg.setPackageInfo(mpg.getPackageInfo().setEntity(REQUEST_PACKAGE));
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController("");
templateConfig.setService("");
templateConfig.setEntity("templates/Entity.java");
templateConfig.setMapper("");
templateConfig.setXml("");
templateConfig.setServiceImpl("");
mpg.setTemplate(templateConfig);
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setSuperEntityColumns("id");
strategy.setInclude(TABLENAME);
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(TABLE_PREFIX);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new VelocityTemplateEngine());
mpg.execute();
}
public static void generateVO() {
AutoGenerator mpg = getAutoGenerator();
GlobalConfig config = mpg.getGlobalConfig();
config.setEntityName(toCamelString().concat("VO"));
mpg.setGlobalConfig(config);
mpg.setPackageInfo(mpg.getPackageInfo().setEntity(VO_PACKAGE));
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController("");
templateConfig.setService("");
templateConfig.setEntity("templates/Entity.java");
templateConfig.setMapper("");
templateConfig.setXml("");
templateConfig.setServiceImpl("");
mpg.setTemplate(templateConfig);
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setSuperEntityColumns("id");
strategy.setInclude(TABLENAME);
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(TABLE_PREFIX);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new VelocityTemplateEngine());
mpg.execute();
}
public static void generateDTO() {
AutoGenerator mpg = getAutoGenerator();
GlobalConfig config = mpg.getGlobalConfig();
config.setEntityName(toCamelString().concat("DTO"));
mpg.setGlobalConfig(config);
mpg.setPackageInfo(mpg.getPackageInfo().setEntity(DTO_PACKAGE));
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController("");
templateConfig.setService("");
templateConfig.setEntity("templates/Entity.java");
templateConfig.setMapper("");
templateConfig.setXml("");
templateConfig.setServiceImpl("");
mpg.setTemplate(templateConfig);
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setSuperEntityColumns("id");
strategy.setInclude(TABLENAME);
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(TABLE_PREFIX);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new VelocityTemplateEngine());
mpg.execute();
}
public static void generateParam() {
AutoGenerator mpg = getAutoGenerator();
GlobalConfig config = mpg.getGlobalConfig();
config.setEntityName(toCamelString().concat("Param"));
mpg.setGlobalConfig(config);
mpg.setPackageInfo(mpg.getPackageInfo().setEntity(PARAM_PACKAGE));
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController("");
templateConfig.setService("");
templateConfig.setEntity("templates/Entity.java");
templateConfig.setMapper("");
templateConfig.setXml("");
templateConfig.setServiceImpl("");
mpg.setTemplate(templateConfig);
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setSuperEntityColumns("id");
strategy.setInclude(TABLENAME);
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(TABLE_PREFIX);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new VelocityTemplateEngine());
mpg.execute();
}
public static void generateEntity() {
AutoGenerator mpg = getAutoGenerator();
GlobalConfig config = mpg.getGlobalConfig();
config.setEntityName(toCamelString().concat("Entity"));
mpg.setGlobalConfig(config);
mpg.setPackageInfo(mpg.getPackageInfo().setEntity(ENTITY_PACKAGE));
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController("");
templateConfig.setService("");
templateConfig.setEntity("templates/Entity.java");
templateConfig.setMapper("");
templateConfig.setXml("");
templateConfig.setServiceImpl("");
mpg.setTemplate(templateConfig);
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setSuperEntityClass("");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setInclude(TABLENAME);
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(TABLE_PREFIX);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new VelocityTemplateEngine());
mpg.execute();
}
private static AutoGenerator getAutoGenerator() {
AutoGenerator mpg = new AutoGenerator();
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(CONTROLLER_PATH + "/src/main/java");
gc.setAuthor("author");
gc.setOpen(false);
gc.setServiceName(toCamelString().concat("Service"));
mpg.setGlobalConfig(gc);
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(DATASOURCE_URL);
dsc.setDriverName(DATASOURCE_DRIVER);
dsc.setUsername(DATASOURCE_USERNAME);
dsc.setPassword(DATASOURCE_PASSWORD);
mpg.setDataSource(dsc);
PackageConfig pc = new PackageConfig();
pc.setParent(PARENT_PACKAGE);
pc.setController(CONTROLLER_PACKAGE);
pc.setEntity(ENTITY_PACKAGE);
pc.setMapper(MAPPER_PACKAGE);
pc.setService(SERVICE_PACKAGE);
pc.setServiceImpl(SERVICEIMPL_PACKAGE);
mpg.setPackageInfo(pc);
return mpg;
}
}