package com.demo;
import com.arpa.generator.AutoGenerator;
import com.arpa.generator.InjectionConfig;
import com.arpa.generator.PathUtil;
import com.arpa.generator.config.*;
import com.arpa.generator.config.po.TableInfo;
import com.arpa.generator.config.rules.NamingStrategy;
import com.arpa.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* <p>代码生成类</p>
*
* @Date: 2021/04/08
* @Author: KKruye
*/
public class CodeGenerator {
/**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = PathUtil.getWebRootPath();
//java文件输出路径
gc.setOutputDir(projectPath + "/src/main/java");
//ftl文件输出路径
gc.setFtlOutputDir(projectPath + "/src/main/resources/templates");
// 是否覆盖同名文件,默认是false
gc.setFileOverride(false);
//作者
gc.setAuthor("KKruye");
gc.setOpen(false);
//实体属性 Swagger2 注解
gc.setSwagger2(true);
//生成entity
gc.setGenEntity(true);
//生成entityDTO
gc.setGenEntityDTO(true);
//生成entityVO
gc.setGenEntityVO(true);
//生成VUE页面
gc.setGenEntityVUE(false);
//生成Service
gc.setGenService(true);
//生成mapper
gc.setGenMapper(true);
//生成Controller
// gc.setGenController(true);
//生成RestController
gc.setGenRestController(true);
//生成mapper.xml中通用baseResultMap,baseColumnList
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/demo?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName("");
pc.setParent("com.demo");
pc.setEntity("domain.entity");
pc.setEntityDTO("domain.dto");
pc.setEntityVO("domain.vo");
pc.setEntityVUE("");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String controllerTemplatePath = "/templates/restController.java.ftl";
String templatePath = "/templates/mapper.xml.ftl";
String listPagePath = "/templates/list.ftl.ftl";
String formPagePath = "/templates/form.ftl.ftl";
// 自定义输出配置
// 自定义配置会被优先输出
List<FileOutConfig> focList = new ArrayList<>();
// 自定义mapper.xml输出路径
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
//自定义controller页面输出
// focList.add(new FileOutConfig(controllerTemplatePath) {
// @Override
// public String outputFile(TableInfo tableInfo) {
// return projectPath + // "/src/main/java/com/demo/controller/"+tableInfo.getEntityName()+"Controller.java";
// }
// });
//自定义list页面输出
// focList.add(new FileOutConfig(listPagePath) {
// @Override
// public String outputFile(TableInfo tableInfo) {
// return projectPath + "/src/main/resources/templates2/" + tableInfo.getControllerMappingHyphen()+ "/list.ftl";
// }
// });
//自定义form页面输出
// focList.add(new FileOutConfig(formPagePath) {
// @Override
// public String outputFile(TableInfo tableInfo) {
// return projectPath + "/src/main/resources/templates/" + tableInfo.getControllerMappingHyphen()+ "/form.ftl";
// }
// });
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
// TemplateConfig templateConfig = new TemplateConfig();
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity.java");
// templateConfig.setService();
// templateConfig.setController();
// templateConfig.setXml(null);
// mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
//数据库表映射到实体的命名策略
strategy.setNaming(NamingStrategy.underline_to_camel);
//数据库表字段映射到实体的命名策略
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//自定义继承的Entity类全称,带包名
strategy.setSuperEntityClass("com.demo.domain.entity.BasicEntity");
//生成字段注解
strategy.setEntityTableFieldAnnotationEnable(true);
//【实体】是否为lombok模型(默认 false)
strategy.setEntityLombokModel(true);
//【实体】逻辑删除字段
strategy.setLogicDeleteFieldName("is_deleted");
//Boolean类型字段是否移除is前缀
strategy.setEntityBooleanColumnRemoveIsPrefix(true);
strategy.setRestControllerStyle(false);
//表前缀
strategy.setTablePrefix("tms_", "sys_", "wms_", "base_", "ctms_");
// 公共父类
strategy.setSuperControllerClass("com.demo.controller.BaseController");
// 写于父类中的公共字段
strategy.setSuperEntityColumns("id", "is_deleted", "gmt_created", "created_by", "gmt_modified", "modified_by");
strategy.setInclude(scanner("要生成的表名,多个英文逗号分割").split(","));
// strategy.setExclude(scanner("要排除的表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
使用AutoGenerator生成代码,需要引入兼容版本的freemaker依赖
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
<scope>compile</scope>
</dependency>