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

91 lines
3.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 {
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[]{
"ebike_default_billing_configuration"
};
@Test
void gen_mybatis_code() {
//配置数据源
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl("jdbc:postgresql://47.109.71.130/ebike_operations??currentSchema=public&stringtype=unspecified");
dataSource.setUsername("root");
dataSource.setPassword("970529");
//生成全库的
// GlobalConfig globalConfig = createGlobalConfigUseStyle2();
//单表的
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;
}
}