逆向工程类

package com.wzxl.assess.base.utils.base;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
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.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
* 代码生成器工具类
* 【使用】运行main方法,在代码编辑器的控制台中输入数据库表前缀(例子:"sys_"),再输入对应表名,ok!
* 【注意】此代码生成器只生成后台domain,mapper,service,controller,mapper.xml层代码,不生成前台js,html代码
* 【补充】:
* java.version Java 运行时环境版本
* java.vendor Java 运行时环境供应商
* java.vendor.url Java 供应商的 URL
* java.home Java 安装目录
* java.vm.specification.version Java 虚拟机规范版本
* java.vm.specification.vendor Java 虚拟机规范供应商
* java.vm.specification.name Java 虚拟机规范名称
* java.vm.version Java 虚拟机实现版本
* java.vm.vendor Java 虚拟机实现供应商
* java.vm.name Java 虚拟机实现名称
* java.specification.version Java 运行时环境规范版本
* java.specification.vendor Java 运行时环境规范供应商
* java.specification.name Java 运行时环境规范名称
* java.class.version Java 类格式版本号
* java.class.path Java 类路径
* java.library.path 加载库时搜索的路径列表
* java.io.tmpdir 默认的临时文件路径
* java.compiler 要使用的 JIT 编译器的名称
* java.ext.dirs 一个或多个扩展目录的路径
* os.name 操作系统的名称
* os.arch 操作系统的架构
* os.version 操作系统的版本
* file.separator 文件分隔符(在 UNIX 系统中是“/”)
* path.separator 路径分隔符(在 UNIX 系统中是“:”)
* line.separator 行分隔符(在 UNIX 系统中是“/n”)
* sys.name 用户的账户名称
* sys.home 用户的主目录
* sys.dir 用户的当前工作目录
*
* @author zhangjiao
*/
public class CodeGeneratorUtil {

public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();


/*************************数据源配置*************************/
DataSourceConfig dsc = new DataSourceConfig();
// 【新】 在连接后面必须要设置时区,否则查询迟出来的时间会慢一个小时
// dsc.setUrl("jdbc:mysql://localhost:3306/smbms?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
dsc.setUrl("jdbc:mysql://192.168.31.190:3306/bigdata_visual?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
// dsc.setSchemaName("public");
// 【新】JDBC连接Mysql6 【旧】com.mysql.jdbc.Driver
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("Gg3fgMc8zt");
mpg.setDataSource(dsc);
/*************************数据源配置*************************/


/*************************全局配置*************************/
GlobalConfig gc = new GlobalConfig();

//当前项目的路径
String projectPath = getProjectPath();


gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("spx");
gc.setOpen(false);
gc.setEntityName("%sDTO"); // %s 占位符 默认会把、用表名替换掉 阿里开发规范:表对应的实体 " 表名 + DO "
// gc.setSwagger2(true); 实体属性 Swagger2 注解
mpg.setGlobalConfig(gc);
/*************************全局配置*************************/


/*************************包配置*************************/
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("包名"));
pc.setParent("com.wzxl.assess.modules");
pc.setEntity("domain"); // 【自定义】 设置输出的 DO 位置为domain
mpg.setPackageInfo(pc);
/*************************包配置*************************/


/*************************自定义配置*************************/
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// 获取系统运行时输入的模块名
String[] include = mpg.getStrategy().getInclude();
String enttityName = include[0].substring(include[0].indexOf("_") + 1);
enttityName = enttityName.substring(0, 1).toUpperCase() + enttityName.substring(1);
// 全局自定义属性,这个主要是在【自定义模板】时模板文件里能够使用的【变量名】
Map<String, Object> map = new HashMap<>();
map.put("basePackageName", pc.getParent());
map.put("entityName", enttityName);
this.setMap(map);
}
};

// 如果模板引擎是 velocity
String templatePath = "/vm/Mapper.xml.vm";
// 【Mapper】自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 重新生成EntityName: 去掉后面的DO
final String entityName = tableInfo.getEntityName();
String mapperName = entityName.substring(0, entityName.indexOf("DTO"));
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + mapperName + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
/*************************自定义配置*************************/


/*************************配置模板*************************/
TemplateConfig templateConfig = new TemplateConfig();
// 指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
templateConfig.setXml(null); // 【使用自定义输出配置必须选为null 否则会在mqpper接口的包下面 默认产生一个 xml的包】
mpg.setTemplate(templateConfig);
/*************************配置模板*************************/


/*************************策略配置*************************/
// 数据库表配置,通过该配置,可指定需要生成哪些表或者排除哪些表
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude(scanner("表名").split(","));
strategy.setTablePrefix(pc.getModuleName() + "_"); // 【表】表名前缀
strategy.setNaming(NamingStrategy.underline_to_camel); // 【类名】下划线驼峰命名
strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 【字段】下划线驼峰命名
//strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity"); // 实体集成的超级基类
//strategy.setSuperEntityColumns("id");
//strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
strategy.setEntityLombokModel(true); // 是否使用lombok的@Accessors注解 【@Accessors(chain = true)】 所有字段的setter方法返回 当前实体而不是 void
strategy.setRestControllerStyle(true);// 是否启用restController模式-->我们前后台分离必须要使用这种模式
mpg.setStrategy(strategy);
/*************************策略配置*************************/


/*************************模板引擎*************************/
mpg.setTemplateEngine(new VelocityTemplateEngine()); // 【默认】
/*************************模板引擎*************************/


// 生成代码文件
mpg.execute();
}

/**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder tips = new StringBuilder();
tips.append("请输入" + tip + ":");
System.out.println(tips.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}

/**
* 获取当前项目的路径
*
* @return
*/
private static String getProjectPath() {
String projectPath = "";
File file = new File("");
try {
projectPath = file.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
return projectPath;
}
}
原文地址:https://www.cnblogs.com/spx88/p/14581373.html