diff --git a/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java b/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java index ccab77f0..856d9247 100644 --- a/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java +++ b/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java @@ -38,40 +38,36 @@ public class GeneratorTest { // JdbcTypeMapping.registerMapping(Integer.class, Long.class); GlobalConfig globalConfig = new GlobalConfig(); - globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java"); - // globalConfig.setTablePrefix("tb_"); - // globalConfig.setEntityWithLombok(true); - globalConfig.setEntitySupperClass(BaseEntity.class); + //设置生成文件目录和根包 + globalConfig.getPackageConfig() + .setSourceDir(System.getProperty("user.dir") + "/src/test/java") + .setBasePackage("com.test"); //设置只生成哪些表 - globalConfig.addGenerateTable("account", "account_session"); + globalConfig.getStrategyConfig() + .addGenerateTable("account", "account_session"); - //设置 entity 的包名 - globalConfig.setEntityPackage("com.test.entity"); - globalConfig.setEntityClassPrefix("My"); - globalConfig.setEntityClassSuffix("Entity"); + //设置生成 entity + globalConfig.enableEntity() + .setWithLombok(true) + .setClassPrefix("My") + .setClassSuffix("Entity") + .setSupperClass(BaseEntity.class); - //设置 entity 的包名 - globalConfig.setTableDefGenerateEnable(true); - globalConfig.setTableDefPackage("com.test.entity.tables"); - globalConfig.setTableDefClassPrefix("My"); - globalConfig.setTableDefClassSuffix("TableDef"); - - //是否生成 mapper 类,默认为 false - globalConfig.setMapperGenerateEnable(true); - globalConfig.setMapperClassPrefix("Flex"); - globalConfig.setMapperClassSuffix("Dao"); - - //设置 mapper 类的包名 - globalConfig.setMapperPackage("com.test.mapper"); - globalConfig.setMapperSupperClass(MyBaseMapper.class); + //设置生成 tableDef + globalConfig.enableTableDef(); + //设置生成 mapper + globalConfig.enableMapper() + .setClassPrefix("Flex") + .setClassSuffix("Dao") + .setSupperClass(MyBaseMapper.class); TableConfig tableConfig = new TableConfig(); tableConfig.setTableName("account"); tableConfig.setUpdateListenerClass(MyUpdateListener.class); - globalConfig.addTableConfig(tableConfig); + globalConfig.getStrategyConfig().addTableConfig(tableConfig); //可以单独配置某个列 @@ -79,7 +75,7 @@ public class GeneratorTest { columnConfig.setColumnName("tenant_id"); columnConfig.setLarge(true); columnConfig.setVersion(true); - globalConfig.addColumnConfig("account", columnConfig); + globalConfig.getStrategyConfig().addColumnConfig("account", columnConfig); //通过 datasource 和 globalConfig 创建代码生成器 @@ -91,39 +87,44 @@ public class GeneratorTest { @Test public void testCodeGen() { - // 配置数据源 + //配置数据源 HikariDataSource dataSource = new HikariDataSource(); dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8"); dataSource.setUsername("root"); dataSource.setPassword("12345678"); GlobalConfig globalConfig = new GlobalConfig(); - globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java"); - globalConfig.setTablePrefix("sys_"); - globalConfig.setBasePackage("com.test"); - globalConfig.setEntityWithLombok(true); - globalConfig.setEntitySupperClass(BaseEntity.class); + //设置生成文件目录和根包 + globalConfig.getPackageConfig() + .setSourceDir(System.getProperty("user.dir") + "/src/test/java") + .setBasePackage("com.test"); - // 设置只生成哪些表 - globalConfig.addGenerateTable("sys_user"); + //设置表前缀和只生成哪些表 + globalConfig.getStrategyConfig() + .setTablePrefix("sys_") + .addGenerateTable("sys_user"); - // 设置 entity 的包名 - globalConfig.setTableDefGenerateEnable(true); + //配置生成 entity + globalConfig.enableEntity() + .setWithLombok(true) + .setSupperClass(BaseEntity.class); - // 是否生成 mapper 类,默认为 false - globalConfig.setMapperGenerateEnable(true); - // 是否生成 service 类,默认为 false - globalConfig.setServiceGenerateEnable(true); - // 是否生成 serviceImpl 类,默认为 false - globalConfig.setServiceImplGenerateEnable(true); - // 是否生成 controller 类,默认为 false - globalConfig.setControllerGenerateEnable(true); + //配置生成 mapper + globalConfig.enableMapper(); + //配置生成 service + globalConfig.enableService(); + //配置生成 serviceImpl + globalConfig.enableServiceImpl(); + //配置生成 controller + globalConfig.enableController(); + //配置生成 tableDef + globalConfig.enableTableDef(); - // 通过 datasource 和 globalConfig 创建代码生成器 + //通过 datasource 和 globalConfig 创建代码生成器 Generator generator = new Generator(dataSource, globalConfig); - // 开始生成代码 + //开始生成代码 generator.generate(); } diff --git a/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/SqliteGeneratorTest.java b/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/SqliteGeneratorTest.java index 00f086f6..f8cdde78 100644 --- a/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/SqliteGeneratorTest.java +++ b/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/SqliteGeneratorTest.java @@ -4,14 +4,13 @@ import com.mybatisflex.codegen.Generator; import com.mybatisflex.codegen.config.GlobalConfig; import com.mybatisflex.codegen.dialect.IDialect; import com.zaxxer.hikari.HikariDataSource; -import org.junit.Test; import java.sql.Connection; import java.sql.Statement; public class SqliteGeneratorTest { -// @Test + // @Test public void testGenerator3() { //配置数据源 @@ -24,20 +23,21 @@ public class SqliteGeneratorTest { GlobalConfig globalConfig = new GlobalConfig(); - globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java"); + + //配置生成文件目录与根包 + globalConfig.getPackageConfig() + .setSourceDir(System.getProperty("user.dir") + "/src/test/java") + .setBasePackage("com.test"); //设置只生成哪些表 - globalConfig.addGenerateTable("person"); + globalConfig.getStrategyConfig() + .addGenerateTable("person"); - //设置 entity 的包名 - globalConfig.setEntityPackage("com.test.entity"); + globalConfig.enableEntity() + .setWithLombok(true); - //是否生成 mapper 类,默认为 false - globalConfig.setMapperGenerateEnable(true); - globalConfig.setEntityWithLombok(true); - - //设置 mapper 类的包名 - globalConfig.setMapperPackage("com.test.mapper"); + //设置生成 mapper 类 + globalConfig.enableMapper(); Generator generator = new Generator(dataSource, globalConfig, IDialect.SQLITE);