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_region" }; @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; } }