2025-08-09 01:37:25 +08:00
|
|
|
|
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";
|
2025-08-09 01:37:25 +08:00
|
|
|
|
private static final String[] tables= new String[]{
|
2025-10-22 10:18:32 +08:00
|
|
|
|
"ebike_region"
|
2025-08-09 01:37:25 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
@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");
|
2025-08-09 01:37:25 +08:00
|
|
|
|
dataSource.setPassword("970529");
|
|
|
|
|
|
//生成全库的
|
2025-09-15 15:48:54 +08:00
|
|
|
|
// GlobalConfig globalConfig = createGlobalConfigUseStyle2();
|
2025-08-09 01:37:25 +08:00
|
|
|
|
//单表的
|
2025-09-15 15:48:54 +08:00
|
|
|
|
GlobalConfig globalConfig = createGlobalConfigUseStyle2();
|
2025-08-09 01:37:25 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|