149 lines
5.8 KiB
Java
149 lines
5.8 KiB
Java
package com;
|
||
|
||
import com.baomidou.mybatisplus.annotation.DbType;
|
||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||
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.DateType;
|
||
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
|
||
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
public class CodeGenerator {
|
||
|
||
public static void main(String[] args) {
|
||
|
||
// 模块名称(包名)
|
||
String moduleName = "com.hzs.common.domain";
|
||
// 表名
|
||
String[] tables = {"SA_TICKET"};
|
||
// 生成实体是否继承统一父类
|
||
boolean superBool = true;
|
||
|
||
// 代码生成器
|
||
AutoGenerator mpg = new AutoGenerator();
|
||
String projectPath = System.getProperty("user.dir");
|
||
// 全局配置
|
||
GlobalConfig globalConfig = new GlobalConfig();
|
||
// 输出目录
|
||
globalConfig.setOutputDir(projectPath + "/code-util/src/main/java")
|
||
// 作者
|
||
.setAuthor("hzs")
|
||
// 时间使用Date
|
||
.setDateType(DateType.ONLY_DATE)
|
||
// // 生成实体后缀追加 Entity
|
||
// .setEntityName("%sEntity")
|
||
// 生成文件覆盖旧文件
|
||
.setFileOverride(true)
|
||
// 生成BaseResultMap
|
||
.setBaseResultMap(true)
|
||
// 生成通用查询结果列
|
||
.setBaseColumnList(true);
|
||
mpg.setGlobalConfig(globalConfig);
|
||
|
||
// 数据源配置
|
||
DataSourceConfig dataSourceConfig = new DataSourceConfig();
|
||
// dsc.setSchemaName("public");
|
||
dataSourceConfig.setUrl("jdbc:oracle:thin:@39.107.153.159:1521/orcl")
|
||
.setDriverName("oracle.jdbc.driver.OracleDriver")
|
||
.setUsername("hzs_retail")
|
||
.setPassword("123456")
|
||
.setDbType(DbType.ORACLE);
|
||
mpg.setDataSource(dataSourceConfig);
|
||
|
||
// 包配置
|
||
PackageConfig packageConfig = new PackageConfig();
|
||
// 模块名称
|
||
packageConfig.setModuleName(moduleName)
|
||
.setParent("com.hzs");
|
||
mpg.setPackageInfo(packageConfig);
|
||
|
||
// 自定义配置
|
||
InjectionConfig cfg = new InjectionConfig() {
|
||
@Override
|
||
public void initMap() {
|
||
// to do nothing
|
||
}
|
||
};
|
||
|
||
// 如果模板引擎是 freemarker
|
||
String templatePath = "/templates/mapper.xml.ftl";
|
||
// 如果模板引擎是 velocity
|
||
// String templatePath = "/templates/mapper.xml.vm";
|
||
|
||
// 自定义输出配置
|
||
List<FileOutConfig> focList = new ArrayList<>();
|
||
// 自定义配置会被优先输出
|
||
focList.add(new FileOutConfig(templatePath) {
|
||
@Override
|
||
public String outputFile(TableInfo tableInfo) {
|
||
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
|
||
return projectPath + "/code-util/src/main/resources/mapper/" + packageConfig.getModuleName()
|
||
+ "/" + tableInfo.getMapperName() + StringPool.DOT_XML;
|
||
}
|
||
|
||
});
|
||
/*
|
||
cfg.setFileCreate(new IFileCreate() {
|
||
@Override
|
||
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
|
||
// 判断自定义文件夹是否需要创建
|
||
checkDir("调用默认方法创建的目录,自定义目录用");
|
||
if (fileType == FileType.MAPPER) {
|
||
// 已经生成 mapper 文件判断存在,不想重新生成返回 false
|
||
return !new File(filePath).exists();
|
||
}
|
||
// 允许生成模板文件
|
||
return true;
|
||
}
|
||
});
|
||
*/
|
||
cfg.setFileOutConfigList(focList);
|
||
mpg.setCfg(cfg);
|
||
|
||
// 配置模板
|
||
TemplateConfig templateConfig = new TemplateConfig();
|
||
|
||
// 配置自定义输出模板
|
||
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
|
||
// templateConfig.setEntity("templates/entity2.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);
|
||
if (superBool) {
|
||
// entity 继承父类
|
||
strategy.setSuperEntityClass("com.hzs.common.core.web.domain.BaseEntity");
|
||
// 写于父类中的公共字段(多个用逗号间隔)
|
||
strategy.setSuperEntityColumns("DEL_FLAG", "PK_CREATOR", "CREATION_TIME", "PK_MODIFIED", "MODIFIED_TIME", "PK_COUNTRY");
|
||
}
|
||
// 开启lombok
|
||
strategy.setEntityLombokModel(true);
|
||
// rest风格
|
||
strategy.setRestControllerStyle(true);
|
||
// // controller 继承父粝
|
||
// strategy.setSuperControllerClass("com.hzs.framework.web.controller.BaseController");
|
||
// 表名(多个用逗号间隔)
|
||
strategy.setInclude(tables);
|
||
strategy.setControllerMappingHyphenStyle(true);
|
||
// 设置表前缀
|
||
strategy.setTablePrefix(packageConfig.getModuleName() + "_");
|
||
mpg.setStrategy(strategy);
|
||
// 使用 freemarker 需要指定
|
||
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
|
||
mpg.execute();
|
||
}
|
||
|
||
}
|