ebike-plus/ebike-staff/src/test/java/com/cdzy/staff/EbikeStaffApplicationTests.java

91 lines
3.8 KiB
Java
Raw Normal View History

package com.cdzy.staff;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = com.cdzy.staff.EbikeStaffApplication.class)
class EbikeStaffApplicationTests {
2025-09-15 15:48:54 +08:00
private static final String model_path ="D:/ebike_plus/ebike-operations";
private static final String mapperPath="D:/ebike_plus/ebike-operations/resources/mapper";
private static final String packageName ="com.cdzy.operations";
private static final String[] tables= new String[]{
2025-10-23 10:14:41 +08:00
"ebike_default_billing_configuration"
};
@Test
void gen_mybatis_code() {
//配置数据源
HikariDataSource dataSource = new HikariDataSource();
2025-09-15 15:48:54 +08:00
dataSource.setJdbcUrl("jdbc:postgresql://47.109.71.130/ebike_operations??currentSchema=public&stringtype=unspecified");
2025-09-04 23:01:59 +08:00
dataSource.setUsername("root");
dataSource.setPassword("970529");
//生成全库的
2025-09-15 15:48:54 +08:00
// GlobalConfig globalConfig = createGlobalConfigUseStyle2();
//单表的
2025-09-15 15:48:54 +08:00
GlobalConfig globalConfig = createGlobalConfigUseStyle2();
Generator generator = new Generator(dataSource, globalConfig);
//生成代码
generator.generate();
}
private GlobalConfig createGlobalConfigUseStyle1() {
// 创建配置内容
GlobalConfig globalConfig = new GlobalConfig();
// 设置项目源目录和基础包
globalConfig.getPackageConfig()
.setSourceDir(model_path)
.setBasePackage(packageName);
// 启用生成 entity并启用 Lombok
globalConfig.setEntityGenerateEnable(true);
globalConfig.setEntityWithLombok(true);
// 设置项目的JDK版本
globalConfig.setEntityJdkVersion(17);
// 启用生成 mapper、service、controller
globalConfig.enableEntity();
globalConfig.enableMapper();
globalConfig.enableService();
globalConfig.enableServiceImpl();
globalConfig.enableController();
globalConfig.enableMapperXml();
globalConfig.setMapperXmlPath(mapperPath);
// 配置 Mapper XML 生成路径和文件名
globalConfig.getMapperXmlConfig()
.setFilePrefix("") // 设置合适的前缀
.setFileSuffix("Mapper"); // 确保设置正确的后缀名
return globalConfig;
}
private GlobalConfig createGlobalConfigUseStyle2() {
// 创建配置内容
GlobalConfig globalConfig = new GlobalConfig();
// 设置项目源目录和基础包
globalConfig.getPackageConfig()
.setSourceDir(model_path)
.setBasePackage(packageName);
// 启用生成 entity并启用 Lombok
globalConfig.setEntityGenerateEnable(true);
globalConfig.setEntityWithLombok(true);
// 设置项目的JDK版本
globalConfig.setEntityJdkVersion(17);
// 启用生成 mapper、service、controller
globalConfig.enableEntity();
globalConfig.enableMapper();
globalConfig.enableService();
globalConfig.enableServiceImpl();
globalConfig.enableController();
globalConfig.enableMapperXml();
globalConfig.setMapperXmlPath(mapperPath);
// 配置 Mapper XML 生成路径和文件名
globalConfig.getMapperXmlConfig()
.setFilePrefix("") // 设置合适的前缀
.setFileSuffix("Mapper"); // 确保设置正确的后缀名
//设置表前缀和只生成哪些表
// globalConfig.setTablePrefix("tb_");
globalConfig.setGenerateTable(tables);
// 返回配置
return globalConfig;
}
}