import com.baomidou.mybatisplus.annotation.FieldFill;
|
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.LikeTable;
|
import com.baomidou.mybatisplus.generator.config.po.TableFill;
|
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
|
import org.junit.Test;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* 通过指定 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) 可达到加速的效果。
|
* 这时测试类启动时就只会初始化 Spring 上下文,不再启动 Tomcat 容器
|
*/
|
public class CodeGeneratorTests {
|
|
private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
|
|
private static final String JDBC_URL = "jdbc:mysql://127.0.0.1:3306/financialdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
|
private static final String USER_NAME = "root";
|
private static final String PASSWORD = "123456";
|
|
|
// 防止误生成
|
private static final String BASE_PACKAGE = "com.financial";
|
|
@Test
|
public void contextLoads() {
|
// 代码生成器
|
AutoGenerator mpg = new AutoGenerator();
|
|
// 全局配置
|
GlobalConfig gc = new GlobalConfig();
|
String projectPath = "F:\\喜望软件\\code\\FinancialDataDashboard\\code-generator";
|
gc.setOutputDir(projectPath + "/src/main/java")
|
.setAuthor("mitao")
|
.setMapperName("%sMapper")
|
.setXmlName("%sMapper")
|
.setServiceName("%sService")
|
.setServiceImplName("%sServiceImpl")
|
.setControllerName("%sController")
|
.setOpen(false)//当代码生成完成之后是否打开代码所在的文件夹
|
.setSwagger2(true) //实体属性 Swagger2 注解
|
.setFileOverride(true)//是否覆盖
|
//.setActiveRecord(true)
|
.setEnableCache(false)// XML 二级缓存
|
.setBaseResultMap(true)// XML ResultMap
|
.setBaseColumnList(true);// XML columList
|
mpg.setGlobalConfig(gc);
|
|
// 数据源配置
|
DataSourceConfig dsc = new DataSourceConfig();
|
dsc.setUrl(JDBC_URL);
|
// dsc.setSchemaName("public");
|
dsc.setDriverName(DRIVER_NAME);
|
dsc.setUsername(USER_NAME);
|
dsc.setPassword(PASSWORD);
|
mpg.setDataSource(dsc);
|
|
// 包配置
|
PackageConfig pc = new PackageConfig();
|
// pc.setModuleName(scanner("模块名"));
|
// pc.setModuleName("sys");
|
pc.setParent(BASE_PACKAGE);//controller entity service service.impl
|
pc.setController("controller");
|
pc.setEntity("model");
|
pc.setMapper("mapper");
|
pc.setService("service");
|
|
|
// 自定义mapping文件生成路径配置
|
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 projectPath + "/src/main/resources/mapping/"
|
+ tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
|
}
|
});
|
cfg.setFileOutConfigList(focList);
|
mpg.setCfg(cfg);
|
// 配置模板
|
TemplateConfig templateConfig = new TemplateConfig();
|
templateConfig.setXml(null);
|
mpg.setTemplate(templateConfig);
|
mpg.setPackageInfo(pc);
|
|
// 配置模板
|
// 不生成controller
|
//TemplateConfig templateConfig = new TemplateConfig();
|
//templateConfig.setController(null);
|
//mpg.setTemplate(templateConfig);
|
|
|
// 策略配置
|
StrategyConfig strategy = new StrategyConfig();
|
//设置字段和表名的是否把下划线完成驼峰命名规则
|
strategy.setNaming(NamingStrategy.underline_to_camel);
|
strategy.setEntitySerialVersionUID(true);
|
strategy.setEntityColumnConstant(false);
|
|
|
//设置生成的实体类继承的父类
|
// strategy.setSuperEntityClass(BaseModel.class);
|
// 生成字段常量
|
// strategy.setEntityColumnConstant(true);
|
// strategy.setSuperEntityColumns("create_by", "update_by", "disabled", "create_time", "last_time");
|
strategy.setEntityLombokModel(true);//是否启动lombok
|
|
strategy.setRestControllerStyle(true);//是否生成resetController
|
strategy.setLogicDeleteFieldName("del_flag");
|
|
// 字段自动操作策略
|
List<TableFill> tableFillList = new ArrayList<>();
|
tableFillList.add(new TableFill("create_time", FieldFill.INSERT));
|
tableFillList.add(new TableFill("update_time", FieldFill.INSERT_UPDATE));
|
// 表字段与属性映射关系
|
strategy.setTableFillList(tableFillList);
|
|
// 公共父类
|
// strategy.setSuperControllerClass("com.sxt.BaseController");
|
// 写于父类中的公共字段
|
// strategy.setSuperEntityColumns("person_id","person_name");
|
//要设置生成哪些表 如果不设置就是生成所有的表
|
// strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
|
strategy.setControllerMappingHyphenStyle(true);
|
strategy.setEntityTableFieldAnnotationEnable(true);
|
// strategy.setTablePrefix(pc.getModuleName() + "");
|
// strategy.setLikeTable(new LikeTable("room"));
|
//strategy.setLikeTable(new LikeTable("member"));
|
strategy.setLikeTable(new LikeTable("tb_"));// 生成表名
|
// strategy.setLikeTable(new LikeTable("t_hotel"));// 生成表名
|
// strategy.setLikeTable(new LikeTable("t_scan_message"));// 生成表名
|
// strategy.setNotLikeTable(new LikeTable("tb_dept"));// 不生成表名
|
// strategy.setLikeTable();
|
// strategy.setTablePrefix("sys_");
|
mpg.setStrategy(strategy);
|
mpg.execute();
|
}
|
|
|
}
|