package com.cdzy.ebikeoperate; 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; /** * 生成 mybatis-flex 代码的工具类。 * * @author dingchao * @date 2025/3/25 * @modified by: */ @SpringBootTest public class MybatisFlexGenCode { private static final String model_path ="D:/Project/克能单车"; private static final String mapperPath="D:/Project/克能单车/resources/mapper"; private static final String packageName ="com.cdzy.ebikeoperate"; private static final String[] tables= new String[]{ "ebike_region_level" }; @Test public void gen_mybatis_code() { //配置数据源 HikariDataSource dataSource = new HikariDataSource(); dataSource.setJdbcUrl("jdbc:mysql://192.168.2.226:3306/ebike_operate?characterEncoding=utf-8"); 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; } }