package com.xxx.core;
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.NamingStrategy;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class MybatisCodeGeneratorCore {
/**
* 路径设置
*/
private static final String ENTITY_PACKAGE = "model.entity.core";
private static final String MAPPER_PACKAGE = "mapper.core";
private static final String DAO_PACKAGE = "dao.core";
private static final String DAO_IMPL_PACKAGE = "dao.core.impl";
/**
* 文件名设置
*/
private static final String REMOVE_TABLE_PREFIX = "";
private static final String ADD_ENTITY_PREFIX = "";
private static final String ADD_ENTITY_SUFFIX = "Entity";
private static final String ADD_DAO_SUFFIX = "Dao";
private static final String ADD_DAO_IMPL_SUFFIX = "DaoImpl";
private static final String ADD_MAPPER_SUFFIX = "Mapper";
/**
* 文件内容设置
*/
private static final String AUTHOR = "Rainey";
/**
* 数据库
*/
private static final String MYSQL_URL = "jdbc:mysql://xxx:3306/lanxi_core?useUnicode=true&characterEncoding=UTF-8";
private static final String MYSQL_USERNAME = "xxx";
private static final String MYSQL_PASSWORD = "xxx";
/**
* 目标表
*/
private static final Set<String> TABLES = new HashSet<>();
static {
// TABLES.add("disease_code");
TABLES.add("health_record");
}
public static void main(String[] args) {
doGenerate(MybatisCodeGeneratorParam.core());
}
private static void doGenerate(MybatisCodeGeneratorParam param) {
// 全局配置
String projectPath = System.getProperty("user.dir");
String modulePath = projectPath + "/" + param.getModuleName();
GlobalConfig gc = new GlobalConfig()
.setOutputDir(modulePath + "/src/main/java")
.setAuthor(AUTHOR)
.setFileOverride(false)
// 二级缓存
.setEnableCache(false)
.setEntityName(ADD_ENTITY_PREFIX + "%s" + ADD_ENTITY_SUFFIX)
.setServiceName(param.getAddDaoPrefix() + "%s" + ADD_DAO_SUFFIX)
.setServiceImplName(param.getAddDaoImplPrefix() + "%s" + ADD_DAO_IMPL_SUFFIX)
.setMapperName(param.getAddMapperPrefix() + "%s" + ADD_MAPPER_SUFFIX)
// .setSwagger2(true)
.setBaseColumnList(true)
.setBaseResultMap(true)
.setOpen(false);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig()
.setUrl(MYSQL_URL)
.setDriverName("com.mysql.cj.jdbc.Driver")
.setUsername(MYSQL_USERNAME)
.setPassword(MYSQL_PASSWORD)
;
// 包配置
PackageConfig pc = new PackageConfig()
.setParent(param.getParentPackage())
.setMapper(MAPPER_PACKAGE)
.setService(DAO_PACKAGE)
.setServiceImpl(DAO_IMPL_PACKAGE)
.setEntity(ENTITY_PACKAGE)
;
// 策略配置
StrategyConfig strategy = new StrategyConfig()
.setNaming(NamingStrategy.underline_to_camel)
.setColumnNaming(NamingStrategy.underline_to_camel)
.setEntityLombokModel(true)
.setRestControllerStyle(false)
.setEntityTableFieldAnnotationEnable(true)
// entity公共父类
// .setSuperEntityClass("com.alvin.learning.mybatis.plus.model.entity.BaseEntity")
// 写于父类中的公共字段
// .setSuperEntityColumns("id")
.setTablePrefix(REMOVE_TABLE_PREFIX)
.setInclude(TABLES.toArray(new String[TABLES.size()]))
.setControllerMappingHyphenStyle(true);
// controller公共父类
// strategy.setSuperControllerClass("com.alvin.learning.mybatis.plus.controller.BaseController");
// 配置自定义模板
TemplateConfig templateConfig = new TemplateConfig()
// 设置成null即不生成
.setController(null)
// .setMapper(null)
// .setService(null)
// .setServiceImpl(null)
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// .setEntity("templates/entity2.java")
// 下面自定义了xml模板地址的配置, 此处关闭默认xml模板生成
.setXml(null)
;
// 自定义配置
// 自定义了xml文件的模板和路径
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 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 modulePath + "/src/main/resources/" + param.getXmlPath() + "core/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
// 代码生成器
AutoGenerator mpg = new AutoGenerator()
.setGlobalConfig(gc)
.setDataSource(dsc)
.setPackageInfo(pc)
.setCfg(cfg)
.setTemplate(templateConfig)
.setStrategy(strategy);
mpg.execute();
}
}
编辑
设置
设置模板
项目运行时间,往前计数,比如运行时间为2022-08-02(周二):
由于数据量太多,进行分表处理