232 lines
8.2 KiB
Java
232 lines
8.2 KiB
Java
package com.aircraft.generate;
|
||
|
||
|
||
import com.baomidou.mybatisplus.annotation.DbType;
|
||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||
import com.baomidou.mybatisplus.generator.AutoGenerator;
|
||
import com.baomidou.mybatisplus.generator.InjectionConfig;
|
||
import com.baomidou.mybatisplus.generator.config.*;
|
||
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
|
||
import com.baomidou.mybatisplus.generator.config.po.TableFill;
|
||
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
||
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
|
||
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
|
||
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
|
||
|
||
|
||
|
||
import java.io.File;
|
||
import java.util.*;
|
||
|
||
/**
|
||
* <p>
|
||
* 代码生成器父类
|
||
* </p>
|
||
*
|
||
*/
|
||
public class SuperGenerator {
|
||
|
||
/**
|
||
* 获取TemplateConfig
|
||
*
|
||
* @return
|
||
*/
|
||
protected TemplateConfig getTemplateConfig() {
|
||
return new TemplateConfig().setXml(null);
|
||
}
|
||
|
||
/**
|
||
* 获取InjectionConfig
|
||
*
|
||
* @return
|
||
*/
|
||
protected InjectionConfig getInjectionConfig() {
|
||
return new InjectionConfig() {
|
||
@Override
|
||
public void initMap() {
|
||
Map<String, Object> map = new HashMap<>();
|
||
this.setMap(map);
|
||
}
|
||
}.setFileOutConfigList(Collections.<FileOutConfig>singletonList(new FileOutConfig(
|
||
"/templates/mapper.xml.vm") {
|
||
// 自定义输出文件目录
|
||
@Override
|
||
public String outputFile(TableInfo tableInfo) {
|
||
return getResourcePath() + "/mapper/" + tableInfo.getEntityName() + "Mapper.xml";
|
||
}
|
||
}));
|
||
}
|
||
|
||
/**
|
||
* 获取PackageConfig
|
||
*
|
||
* @return
|
||
*/
|
||
protected PackageConfig getPackageConfig() {
|
||
return new PackageConfig()
|
||
.setParent("com.aircraft.modules.system")
|
||
.setController("controller")
|
||
.setEntity("domain")
|
||
.setMapper("mapper")
|
||
.setService("service")
|
||
.setServiceImpl("service.impl");
|
||
}
|
||
|
||
/**
|
||
* 获取StrategyConfig
|
||
*
|
||
* @param tableName
|
||
* @return
|
||
*/
|
||
protected StrategyConfig getStrategyConfig(String tableName) {
|
||
return new StrategyConfig()
|
||
.setCapitalMode(false)// 全局大写命名
|
||
// .setTablePrefix("sys_")// 去除前缀
|
||
.setNaming(NamingStrategy.underline_to_camel)// 表名生成策略
|
||
//.setInclude(new String[] { "user" }) // 需要生成的表
|
||
//自定义实体父类
|
||
.setSuperEntityClass("com.aircraft.base.BaseEntity")
|
||
// 自定义实体,公共字段
|
||
.setSuperEntityColumns("create_by")
|
||
.setSuperEntityColumns("create_time")
|
||
.setSuperEntityColumns("update_by")
|
||
.setSuperEntityColumns("update_time")
|
||
.setSuperEntityColumns("del_flag")
|
||
|
||
// 自定义 mapper 父类
|
||
.setSuperMapperClass("com.baomidou.mybatisplus.core.mapper.BaseMapper")
|
||
// 自定义 service 实现类父类
|
||
.setSuperServiceImplClass("com.baomidou.mybatisplus.extension.service.impl.ServiceImpl")
|
||
// 自定义 service 接口父类
|
||
.setSuperServiceClass("com.baomidou.mybatisplus.extension.service.IService")
|
||
// 【实体】是否生成字段常量(默认 false)
|
||
// .setEntityColumnConstant(true)
|
||
// 【实体】是否为构建者模型(默认 false)
|
||
.setEntityBuilderModel(false)
|
||
// 【实体】是否为lombok模型(默认 false)<a href="https://projectlombok.org/">document</a>
|
||
.setEntityLombokModel(true)
|
||
// Boolean类型字段是否移除is前缀处理
|
||
.setEntityBooleanColumnRemoveIsPrefix(true)
|
||
.setRestControllerStyle(false)
|
||
.setRestControllerStyle(true)
|
||
.setInclude(tableName);
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 获取DataSourceConfig
|
||
*
|
||
* @return
|
||
*/
|
||
protected DataSourceConfig getDataSourceConfig() {
|
||
return new DataSourceConfig()
|
||
.setDbType(DbType.MYSQL)// 数据库类型
|
||
.setTypeConvert(new MySqlTypeConvert() {
|
||
@Override
|
||
public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
|
||
if (fieldType.toLowerCase().equals("bit")) {
|
||
return DbColumnType.BOOLEAN;
|
||
}
|
||
if (fieldType.toLowerCase().equals("tinyint")) {
|
||
return DbColumnType.BOOLEAN;
|
||
}
|
||
if (fieldType.toLowerCase().equals("date")) {
|
||
return DbColumnType.LOCAL_DATE;
|
||
}
|
||
if (fieldType.toLowerCase().equals("time")) {
|
||
return DbColumnType.LOCAL_TIME;
|
||
}
|
||
if (fieldType.toLowerCase().equals("datetime")) {
|
||
return DbColumnType.DATE;
|
||
}
|
||
return super.processTypeConvert(globalConfig, fieldType);
|
||
}
|
||
})
|
||
.setDriverName("com.mysql.cj.jdbc.Driver")
|
||
.setUsername("root")
|
||
.setPassword("scutensave")
|
||
.setUrl("jdbc:mysql://8.138.171.103:3306/aircraft-mp?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=GMT%2B8&useSSL=false");
|
||
}
|
||
|
||
/**
|
||
* 获取GlobalConfig
|
||
*
|
||
* @return
|
||
*/
|
||
protected GlobalConfig getGlobalConfig() {
|
||
return new GlobalConfig()
|
||
.setOutputDir(getJavaPath())//输出目录
|
||
.setFileOverride(false)// 是否覆盖文件
|
||
.setActiveRecord(false)// 开启 activeRecord 模式
|
||
.setEnableCache(false)// XML 二级缓存
|
||
.setBaseResultMap(false)// XML ResultMap
|
||
.setBaseColumnList(false)// XML columList
|
||
.setKotlin(false) //是否生成 kotlin 代码
|
||
.setOpen(false)
|
||
.setAuthor("cli") //作者
|
||
//自定义文件命名,注意 %s 会自动填充表实体属性!
|
||
.setEntityName("%s")
|
||
.setMapperName("%sMapper")
|
||
.setXmlName("%sMapper")
|
||
.setServiceName("%sService")
|
||
.setServiceImplName("%sServiceImpl")
|
||
.setControllerName("%sController");
|
||
}
|
||
|
||
|
||
/**
|
||
* 获取根目录
|
||
*
|
||
* @return
|
||
*/
|
||
private String getRootPath() {
|
||
String file = Objects.requireNonNull(SuperGenerator.class.getClassLoader().getResource("")).getFile();
|
||
return new File(file).getParentFile().getParent();
|
||
}
|
||
|
||
/**
|
||
* 获取JAVA目录
|
||
*
|
||
* @return
|
||
*/
|
||
protected String getJavaPath() {
|
||
String javaPath = getRootPath() + "/src/main/java";
|
||
System.err.println(" Generator Java Path:【 " + javaPath + " 】");
|
||
return javaPath;
|
||
}
|
||
|
||
/**
|
||
* 获取Resource目录
|
||
*
|
||
* @return
|
||
*/
|
||
protected String getResourcePath() {
|
||
String resourcePath = getRootPath() + "/src/main/resources";
|
||
System.err.println(" Generator Resource Path:【 " + resourcePath + " 】");
|
||
return resourcePath;
|
||
}
|
||
|
||
/**
|
||
* 获取AutoGenerator
|
||
*
|
||
* @param tableName
|
||
* @return
|
||
*/
|
||
protected AutoGenerator getAutoGenerator(String tableName) {
|
||
return new AutoGenerator()
|
||
// 全局配置
|
||
.setGlobalConfig(getGlobalConfig())
|
||
// 数据源配置
|
||
.setDataSource(getDataSourceConfig())
|
||
// 策略配置
|
||
.setStrategy(getStrategyConfig(tableName))
|
||
// 包配置
|
||
.setPackageInfo(getPackageConfig())
|
||
// 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
|
||
.setCfg(getInjectionConfig())
|
||
.setTemplate(getTemplateConfig());
|
||
}
|
||
|
||
}
|