diff --git a/docs/zh/others/codegen.md b/docs/zh/others/codegen.md index c6502665..92199f35 100644 --- a/docs/zh/others/codegen.md +++ b/docs/zh/others/codegen.md @@ -6,6 +6,7 @@ 在使用前先添加 `mybatis-flex-codegen` 的 Maven 依赖: ```xml + com.mybatis-flex mybatis-flex-codegen @@ -16,6 +17,7 @@ 同时需要添加数据源的 Maven 依赖和 jdbc 驱动依赖: ```xml + com.zaxxer HikariCP @@ -23,9 +25,9 @@ - mysql - mysql-connector-java - 8.0.32 +mysql +mysql-connector-java +8.0.32 ``` @@ -33,42 +35,17 @@ ```java public class Codegen { - + public static void main(String[] args) { - //配置数据源 HikariDataSource dataSource = new HikariDataSource(); dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/your-database?characterEncoding=utf-8"); dataSource.setUsername("root"); dataSource.setPassword("******"); - //创建配置内容 - GlobalConfig globalConfig = new GlobalConfig(); - - //设置只生成哪些表 - globalConfig.addGenerateTable("account", "account_session"); - - //设置 entity 的包名 - globalConfig.setEntityPackage("com.test.entity"); - - //设置表前缀 - //globalConfig.setTablePrefix("tb_"); - - //设置 entity 是否使用 Lombok - //globalConfig.setEntityWithLombok(true); - - //是否生成 mapper 类,默认为 false - //globalConfig.setMapperGenerateEnable(true); - - //设置 mapper 类的包名 - globalConfig.setMapperPackage("com.test.mapper"); - - //可以单独配置某个列 - ColumnConfig columnConfig = new ColumnConfig(); - columnConfig.setColumnName("tenant_id"); - columnConfig.setLarge(true); - columnConfig.setVersion(true); - globalConfig.addColumnConfig("account", columnConfig); + //创建配置内容,两种风格都可以。 + GlobalConfig globalConfig = createGlobalConfigUseStyle1(); + //GlobalConfig globalConfig = createGlobalConfigUseStyle2(); //通过 datasource 和 globalConfig 创建代码生成器 Generator generator = new Generator(dataSource, globalConfig); @@ -76,6 +53,65 @@ public class Codegen { //生成代码 generator.generate(); } + + public static GlobalConfig createGlobalConfigUseStyle1() { + //创建配置内容 + GlobalConfig globalConfig = new GlobalConfig(); + + //设置根包 + globalConfig.setBasePackage("com.test"); + + //设置表前缀和只生成哪些表 + globalConfig.setTablePrefix("tb_"); + globalConfig.setGenerateTable("account", "account_session"); + + //设置生成 entity 并启用 Lombok + globalConfig.setEntityGenerateEnable(true); + globalConfig.setWithLombok(true); + + //设置生成 mapper + globalConfig.setMapperGenerateEnable(true); + + //可以单独配置某个列 + ColumnConfig columnConfig = new ColumnConfig(); + columnConfig.setColumnName("tenant_id"); + columnConfig.setLarge(true); + columnConfig.setVersion(true); + globalConfig.setColumnConfig("account", columnConfig); + + return globalConfig; + } + + public static GlobalConfig createGlobalConfigUseStyle2() { + //创建配置内容 + GlobalConfig globalConfig = new GlobalConfig(); + + //设置根包 + globalConfig.getPackageConfig() + .setBasePackage("com.test"); + + //设置表前缀和只生成哪些表 + globalConfig.getStrategyConfig() + .setTablePrefix("tb_") + .setGenerateTable("account", "account_session"); + + //设置生成 entity 并启用 Lombok + globalConfig.enableEntity() + .setWithLombok(true); + + //设置生成 mapper + globalConfig.enableMapper(); + + //可以单独配置某个列 + ColumnConfig columnConfig = new ColumnConfig(); + columnConfig.setColumnName("tenant_id"); + columnConfig.setLarge(true); + columnConfig.setVersion(true); + globalConfig.getStrategyConfig() + .setColumnConfig("account", columnConfig); + + return globalConfig; + } } ``` @@ -84,158 +120,271 @@ public class Codegen { 关闭 APT 的 Mapper 类文件生成,请参考:[APT 设置章节](../others/apt.md) -## 全局配置 GlobalConfig +## 使用介绍 -GlobalConfig 支持更多的配置如下: +在 Mybatis-Flex 的代码生成器中,支持如下 8 种类型的的产物生成: + +- Entity 实体类 +- Mapper 映射类 +- TableDef 表定义辅助类 +- Service 服务类 +- ServiceImpl 服务实现类 +- Controller 控制类 +- MapperXml 文件 +- package-info.java 文件 + +启用或关闭某种类型产物的生成,代码如下: ```java - -public class GlobalConfig { - - //代码生成目录 - private String sourceDir; - - //根包名 - private String basePackage = "com.mybatisflex"; - - //entity 的包名 - private String entityPackage; - - //entity 类的前缀 - private String entityClassPrefix; - - //entity 类的后缀 - private String entityClassSuffix; - - //entity 类的父类,可以自定义一些 BaseEntity 类 - private Class entitySupperClass; - - //entity 默认实现的接口 - private Class[] entityInterfaces = {Serializable.class}; - - //entity 是否使用 Lombok - private boolean entityWithLombok = false; - - private boolean tableDefGenerateEnable = false; - - //tableDef 的包名 - private String tableDefPackage; - - //tableDef 类的前缀 - private String tableDefClassPrefix; - - //tableDef 类的后缀 - private String tableDefClassSuffix = "Def"; - - //是否生成 mapper 类 - private boolean mapperGenerateEnable = false; - - //是否覆盖已经存在的 mapper - private boolean mapperOverwriteEnable = false; - - //mapper 类的前缀 - private String mapperClassPrefix; - - //mapper 类的后缀 - private String mapperClassSuffix = "Mapper"; - - //mapper 的包名 - private String mapperPackage; - - //自定义 mapper 的父类 - private Class mapperSupperClass = BaseMapper.class; - - //是否生成 service 类 - private boolean serviceGenerateEnable = false; - - //是否覆盖已经存在的 service - private boolean serviceOverwriteEnable = false; - - //service 类的前缀 - private String serviceClassPrefix; - - //service 类的后缀 - private String serviceClassSuffix = "Service"; - - //service 的包名 - private String servicePackage; - - //自定义 service 的父类 - private Class serviceSupperClass = IService.class; - - //是否生成 serviceImpl 类 - private boolean serviceImplGenerateEnable = false; - - //是否覆盖已经存在的 serviceImpl - private boolean serviceImplOverwriteEnable = false; - - //serviceImpl 类的前缀 - private String serviceImplClassPrefix; - - //serviceImpl 类的后缀 - private String serviceImplClassSuffix = "ServiceImpl"; - - //serviceImpl 的包名 - private String serviceImplPackage; - - //自定义 serviceImpl 的父类 - private Class serviceImplSupperClass = ServiceImpl.class; - - //是否生成 controller 类 - private boolean controllerGenerateEnable = false; - - //是否覆盖已经存在的 controller - private boolean controllerOverwriteEnable = false; - - //controller 类的前缀 - private String controllerClassPrefix; - - //controller 类的后缀 - private String controllerClassSuffix = "Controller"; - - //controller 的包名 - private String controllerPackage; - - //自定义 controller 的父类 - private Class controllerSupperClass; - - //rest 风格的 Controller - private boolean restStyleController = true; - - //数据库表前缀,多个前缀用英文逗号(,) 隔开 - private String tablePrefix; - - //逻辑删除的默认字段名称 - private String logicDeleteColumn; - - //乐观锁的字段名称 - private String versionColumn; - - //是否生成视图映射 - private boolean generateForView = false; - - //单独为某张表添加独立的配置 - private Map tableConfigMap; - - //设置某个列的全局配置 - private Map defaultColumnConfigMap; - - //生成哪些表,白名单 - private Set generateTables; - - //不生成哪些表,黑名单 - private Set unGenerateTables; - - //使用哪个模板引擎来生成代码 - protected ITemplate templateEngine; - - //其他自定义配置 - private Map others; - -} +// 开启 Entity 的生成 +globalConfig.enableEntity(); +// 关闭 Entity 的生成 +globalConfig.disableEntity(); ``` -## 表配置 TableConfig +所有方法均支持链式调用配置,代码如下: + +```java +// 设置生成 Entity 并启用 Lombok、设置父类 +globalConfig.enableEntity() + .setWithLombok(true) + .setSupperClass(BaseEntity.class); +``` + +## 全局配置 `GlobalConfig` + +> 可以像先前一样直接使用 `setXxx()` 进行配置,也可以使用 `getXxxConfig().setXxx()` 进行分类配置。 + +| 获取配置 | 描述 | +|------------------------|------------------| +| getJavadocConfig() | 注释配置 | +| getPackageConfig() | 包配置 | +| getStrategyConfig() | 策略配置 | +| getTemplateConfig() | 模板配置 | +| getEntityConfig() | Entity 生成配置 | +| getMapperConfig() | Mapper 生成配置 | +| getServiceConfig() | Service 生成配置 | +| getServiceImplConfig() | ServiceImpl 生成配置 | +| getControllerConfig() | Controller 生成配置 | +| getTableDefConfig() | TableDef 生成配置 | +| getMapperXmlConfig() | MapperXml 生成配置 | + +```java +globalConfig.getPackageConfig() + .setSourceDir("D://files/java") + .setBasePackage("com.your.domain"); +``` + +| 启用生成 | 描述 | +|---------------------|-------------------| +| enableEntity() | 启用 Entity 生成 | +| enableMapper() | 启用 Mapper 生成 | +| enableService() | 启用 Service 生成 | +| enableServiceImpl() | 启用 ServiceImpl 生成 | +| enableController() | 启用 Controller 生成 | +| enableTableDef() | 启用 TableDef 生成 | +| enableMapperXml() | 启用 MapperXml 生成 | + +启用生成之后可以继续链式进行配置,例如: + +```java +// 设置生成 Entity 并启用 Lombok、设置父类 +globalConfig.enableEntity() + .setWithLombok(true) + .setSupperClass(BaseEntity.class); +``` + +## 注释配置 `JavadocConfig` + +| 配置 | 描述 | 默认值 | +|---------------------------------|-----------------|---------------------------------| +| setAuthor(String) | 作者 | System.getProperty("user.name") | +| setSince(String) | 自 | 日期(yyyy-MM-dd) | +| setTableCommentFormat(Function) | 表名格式化 | 原表名 | +| setEntityPackage(String) | Entity 包注释 | "实体类层(Entity)软件包。" | +| setMapperPackage(String) | Mapper 包注释 | "映射层(Mapper)软件包。" | +| setServicePackage(String) | Service 包注释 | "服务层(Service)软件包。" | +| setServiceImplPackage(String) | ServiceImpl 包注释 | "服务层实现(ServiceImpl)软件包。" | +| setControllerPackage(String) | Controller 包注释 | "控制层(Controller)软件包。" | +| setTableDefPackage(String) | TableDef 包注释 | "表定义层(TableDef)软件包。" | + +```java +globalConfig.getJavadocConfig() + .setAuthor("Your Name") + .setSince("1.0.1"); +``` + +## 包配置 `PackageConfig` + +| 配置 | 描述 | 默认值 | +|-------------------------------|----------------|---------------------------------------------------| +| setSourceDir(String) | 文件输出目录 | System.getProperty("user.dir") + "/src/main/java" | +| setBasePackage(String) | 根包名 | "com.mybatisflex" | +| setEntityPackage(String) | Entity 包名 | getBasePackage() + ".entity" | | +| setMapperPackage(String) | Mapper 包名 | getBasePackage() + ".mapper" | | +| setServicePackage(String) | Service 包名 | getBasePackage() + ".service" | | +| setServiceImplPackage(String) | ServiceImpl 包名 | getBasePackage() + ".service.impl" | | +| setControllerPackage(String) | Controller 包名 | getBasePackage() + ".controller" | | +| setTableDefPackage(String) | TableDef 包名 | getEntityPackage() + ".tables" | | +| setMapperXmlPath(String) | MapperXml 路径 | getSourceDir() + "/resources/mapper" | | + +```java +globalConfig.getPackageConfig() + .setSourceDir("D://files/java") + .setBasePackage("com.your.domain"); +``` + +## 策略配置 `StrategyConfig` + +| 配置 | 描述 | 默认值 | +|--------------------------------|------------------------|-------| +| setTablePrefix(String) | 数据库表前缀,多个前缀用英文逗号(,) 隔开 | null | +| setLogicDeleteColumn(String) | 逻辑删除的默认字段名称 | null | +| setVersionColumn(String) | 乐观锁的字段名称 | null | +| setGenerateForView(boolean) | 是否生成视图映射 | false | +| setTableConfig(TableConfig) | 单独为某张表添加独立的配置 | null | +| setColumnConfig(ColumnConfig) | 设置某个列的全局配置 | null | +| setGenerateTables(String...) | 生成哪些表,白名单 | null | +| setUnGenerateTables(String...) | 不生成哪些表,黑名单 | null | + +```java +globalConfig.getStrategyConfig() + .setTablePrefix("sys_") + .setGenerateTables("sys_user","sys_dept"); +``` + +## 模板配置 `TemplateConfig` + +| 配置 | 描述 | 默认值 | +|------------------------|------------------|------------------------------------| +| setTemplate(ITemplate) | | | +| setEntity(String) | Entity 模板路径 | "/templates/enjoy/entity.tpl" | +| setMapper(String) | Mapper 模板路径 | "/templates/enjoy/mapper.tpl" | +| setService(String) | Service 模板路径 | "/templates/enjoy/service.tpl" | +| setServiceImpl(String) | ServiceImpl 模板路径 | "/templates/enjoy/serviceImpl.tpl" | +| setController(String) | Controller 模板路径 | "/templates/enjoy/controller.tpl" | +| setTableDef(String) | TableDef 模板路径 | "/templates/enjoy/tableDef.tpl" | +| setMapperXml(String) | MapperXml 模板路径 | "/templates/enjoy/mapperXml.tpl" | + +```java +globalConfig.getTemplateConfig() + .setTemplate(new FreeMarkerTemplate()) + .setEntity("D:\your-template-file\my-entity.tpl"); +``` + +## Entity 生成配置 `EntityConfig` + +| 配置 | 描述 | 默认值 | +|-----------------------------|----------------------------------|--------------------| +| setClassPrefix(String) | Entity 类的前缀 | "" | +| setClassSuffix(String) | Entity 类的后缀 | "" | +| setSupperClass(Class) | Entity 类的父类,可以自定义一些 BaseEntity 类 | null | +| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false | +| setImplInterfaces(Class[]) | Entity 默认实现的接口 | Serializable.class | +| setWithLombok(boolean) | Entity 是否使用 Lombok 注解 | false | + +```java +globalConfig.getEntityConfig() + .setWithLombok(true) + .setClassPrefix("My") + .setClassSuffix("Entity") + .setSupperClass(BaseEntity.class); +``` + +## Mapper 生成配置 `MapperConfig` + +| 配置 | 描述 | 默认值 | +|-----------------------------|-------------|------------------| +| setClassPrefix(String) | Mapper 类的前缀 | "" | +| setClassSuffix(String) | Mapper 类的后缀 | "Mapper" | +| setSupperClass(Class) | Mapper 类的父类 | BaseMapper.class | +| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false | + +```java +globalConfig.getMapperConfig() + .setClassPrefix("My") + .setClassSuffix("Mapper") + .setSuperClass(BaseMapper.class); +``` + +## Service 生成配置 `ServiceConfig` + +| 配置 | 描述 | 默认值 | +|-----------------------------|--------------|----------------| +| setClassPrefix(String) | Service 类的前缀 | "" | +| setClassSuffix(String) | Service 类的后缀 | "Service" | +| setSupperClass(Class) | Service 类的父类 | IService.class | +| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false | + +```java +globalConfig.getServiceConfig() + .setClassPrefix("My") + .setClassSuffix("Service") + .setSuperClass(IService.class); +``` + +## ServiceImpl 生成配置 `ServiceImplConfig` + +| 配置 | 描述 | 默认值 | +|-----------------------------|------------------|-------------------| +| setClassPrefix(String) | ServiceImpl 类的前缀 | "" | +| setClassSuffix(String) | ServiceImpl 类的后缀 | "ServiceImpl" | +| setSupperClass(Class) | ServiceImpl 类的父类 | ServiceImpl.class | +| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false | + +```java +globalConfig.getServiceImplConfig() + .setClassPrefix("My") + .setClassSuffix("ServiceImpl") + .setSuperClass(ServiceImpl.class); +``` + +## Controller 生成配置 `ControllerConfig` + +| 配置 | 描述 | 默认值 | +|-----------------------------|---------------------|--------------| +| setClassPrefix(String) | Controller 类的前缀 | "" | +| setClassSuffix(String) | Controller 类的后缀 | "Controller" | +| setSupperClass(Class) | Controller 类的父类 | null | +| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false | +| setRestStyle(boolean) | REST 风格的 Controller | true | + +```java +globalConfig.getControllerConfig() + .setClassPrefix("My") + .setClassSuffix("Controller") + .setSuperClass(BaseController.class); +``` + +## TableDef 生成配置 `TableDefConfig` + +| 配置 | 描述 | 默认值 | +|-----------------------------|---------------|-------| +| setClassPrefix(String) | TableDef 类的前缀 | "" | +| setClassSuffix(String) | TableDef 类的后缀 | "Def" | +| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false | + +```java +globalConfig.getTableDefConfig() + .setClassPrefix("My") + .setClassSuffix("Def"); +``` + +## MapperXml 生成配置 `MapperXmlConfig` + +| 配置 | 描述 | 默认值 | +|-----------------------------|-----------------|----------| +| setFilePrefix(String) | MapperXml 文件的前缀 | "" | +| setFileSuffix(String) | MapperXml 文件的后缀 | "Mapper" | +| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false | + +```java +globalConfig.getMapperXmlConfig() + .setFilePrefix("My") + .setFileSuffix("Mapper"); +``` + +## 表配置 `TableConfig` TableConfig 支持的配置如下: @@ -265,7 +414,7 @@ public class TableConfig { } ``` -## 列配置 ColumnConfig +## 列配置 `ColumnConfig` ColumnConfig 支持的配置如下: @@ -288,7 +437,7 @@ public class ColumnConfig implements Serializable { private KeyType keyType; private String keyValue; private Boolean keyBefore; - + // 是否是租户列 private Boolean tenantId; } @@ -300,13 +449,15 @@ MyBatis-Flex 内置了一个名为:`JdbcTypeMapping` 的 java 类,我们可 数据类型,在开始生成代码之前,可以先调用其进行配置,例如: ```java -JdbcTypeMapping.registerMapping(LocalDateTime.class, Date.class); +JdbcTypeMapping.registerMapping(LocalDateTime.class,Date.class); ``` + 那么,当我们生成代码的时候,发现 JDBC 驱动的数据类型为 `LocalDateTime`,则 Entity 对应的属性类型为 `Date`。 ## 自定义代码模板 -通过 `GlobalConfig`(全局配置)的 `setTemplateEngine()` 方法,可以配置自己的模板引擎以及模板,以下是内置的 `EnjoyTemplate` 的代码示例: +通过 `GlobalConfig`(全局配置)的 `setTemplateEngine()` 方法,可以配置自己的模板引擎以及模板,以下是内置的 `EnjoyTemplate` +的代码示例: ```java public class EnjoyTemplate implements ITemplate { @@ -354,16 +505,7 @@ public class EnjoyTemplate implements ITemplate { ## 添加其他产物的生成 -在 MyBatis-Flex 的代码生成器中,支持如下 6 种类型的的产物生成 - -- 1、Entity 实体类 -- 2、Mapper 类(默认关闭) -- 3、TableDef 表定义辅助类(默认关闭) -- 4、Service 类(默认关闭) -- 5、ServiceImpl 类(默认关闭) -- 6、Controller 类(默认关闭) - -这 6 种产物,都是通过实现 `IGenerator` 来实现的,比如 Entity 实体类的代码如下: +通过实现 `IGenerator` 来实现,比如 Entity 实体类的代码如下: ```java public class EntityGenerator implements IGenerator { @@ -373,15 +515,30 @@ public class EntityGenerator implements IGenerator { @Override public void generate(Table table, GlobalConfig globalConfig) { - String entityPackagePath = globalConfig.getEntityPackage().replace(".", "/"); - File entityJavaFile = new File(globalConfig.getSourceDir(), entityPackagePath + "/" + + if (!globalConfig.isEntityGenerateEnable()) { + return; + } + + PackageConfig packageConfig = globalConfig.getPackageConfig(); + EntityConfig entityConfig = globalConfig.getEntityConfig(); + + String entityPackagePath = packageConfig.getEntityPackage().replace(".", "/"); + File entityJavaFile = new File(packageConfig.getSourceDir(), entityPackagePath + "/" + table.buildEntityClassName() + ".java"); - Map params = new HashMap<>(); - params.put("table", table); - params.put("globalConfig", globalConfig); - globalConfig.getTemplateEngine().generate(params, templatePath, entityJavaFile); + if (entityJavaFile.exists() && !entityConfig.isOverwriteEnable()) { + return; + } + + + Map params = new HashMap<>(4); + params.put("table", table); + params.put("entityConfig", entityConfig); + params.put("packageConfig", packageConfig); + params.put("javadocConfig", globalConfig.getJavadocConfig()); + + globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, entityJavaFile); } } ``` @@ -397,6 +554,7 @@ public class HtmlGenerator implements IGenerator { } } ``` + 最后,通过 `GeneratorFactory` 来注册 `HtmlGenerator` 即可: ```java diff --git a/mybatis-flex-codegen/pom.xml b/mybatis-flex-codegen/pom.xml index e181690f..1f4f674f 100644 --- a/mybatis-flex-codegen/pom.xml +++ b/mybatis-flex-codegen/pom.xml @@ -82,6 +82,13 @@ test + + org.springframework + spring-web + ${spring.version} + test + + diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java index ce9c8f4e..547cd610 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/Generator.java @@ -16,6 +16,7 @@ package com.mybatisflex.codegen; import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.config.StrategyConfig; import com.mybatisflex.codegen.dialect.IDialect; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.GeneratorFactory; @@ -83,22 +84,23 @@ public class Generator { private List buildTables() throws SQLException { + StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); List
tables = new ArrayList<>(); try (ResultSet rs = getTablesResultSet()) { while (rs.next()) { String tableName = rs.getString("TABLE_NAME"); - if (!globalConfig.isSupportGenerate(tableName)) { + if (!strategyConfig.isSupportGenerate(tableName)) { continue; } Table table = new Table(); table.setGlobalConfig(globalConfig); - table.setTableConfig(globalConfig.getTableConfig(tableName)); + table.setTableConfig(strategyConfig.getTableConfig(tableName)); table.setName(tableName); String remarks = rs.getString("REMARKS"); - table.setRemarks(remarks); + table.setComment(remarks); buildPrimaryKey(table); @@ -113,7 +115,7 @@ public class Generator { protected ResultSet getTablesResultSet() throws SQLException { - if (globalConfig.isGenerateForView()) { + if (globalConfig.getStrategyConfig().isGenerateForView()) { return dialect.getTablesResultSet(dbMeta, conn, new String[]{"TABLE", "VIEW"}); } else { return dialect.getTablesResultSet(dbMeta, conn, new String[]{"TABLE"}); diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ControllerConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ControllerConfig.java new file mode 100644 index 00000000..7a29d2f3 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ControllerConfig.java @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.codegen.config; + +/** + * 生成 Controller 的配置。 + * + * @author 王帅 + * @since 2023-05-15 + */ +@SuppressWarnings("unused") +public class ControllerConfig { + + /** + * Controller 类的前缀。 + */ + private String classPrefix = ""; + + /** + * Controller 类的后缀。 + */ + private String classSuffix = "Controller"; + + /** + * 自定义 Controller 的父类。 + */ + private Class supperClass; + + /** + * 是否覆盖之前生成的文件。 + */ + private boolean overwriteEnable; + + /** + * 生成 REST 风格的 Controller。 + */ + private boolean restStyle = true; + + public String buildSuperClassImport() { + return supperClass.getName(); + } + + public String buildSuperClassName() { + return supperClass.getSimpleName(); + } + + /** + * 获取类前缀。 + */ + public String getClassPrefix() { + return classPrefix; + } + + /** + * 设置类前缀。 + */ + public ControllerConfig setClassPrefix(String classPrefix) { + this.classPrefix = classPrefix; + return this; + } + + /** + * 获取类后缀。 + */ + public String getClassSuffix() { + return classSuffix; + } + + /** + * 设置类后缀。 + */ + public ControllerConfig setClassSuffix(String classSuffix) { + this.classSuffix = classSuffix; + return this; + } + + /** + * 获取父类。 + */ + public Class getSupperClass() { + return supperClass; + } + + /** + * 设置父类。 + */ + public ControllerConfig setSupperClass(Class supperClass) { + this.supperClass = supperClass; + return this; + } + + /** + * 是否覆盖原有文件。 + */ + public boolean isOverwriteEnable() { + return overwriteEnable; + } + + /** + * 设置是否覆盖原有文件。 + */ + public ControllerConfig setOverwriteEnable(boolean overwriteEnable) { + this.overwriteEnable = overwriteEnable; + return this; + } + + /** + * 是否 REST 风格。 + */ + public boolean isRestStyle() { + return restStyle; + } + + /** + * 设置 REST 风格。 + */ + public ControllerConfig setRestStyle(boolean restStyle) { + this.restStyle = restStyle; + return this; + } + +} \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java new file mode 100644 index 00000000..0a3b4edf --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.codegen.config; + +import java.io.Serializable; + +/** + * 生成 Entity 的配置。 + * + * @author 王帅 + * @since 2023-05-15 + */ +@SuppressWarnings("unused") +public class EntityConfig { + + /** + * Entity 类的前缀。 + */ + private String classPrefix = ""; + + /** + * Entity 类的后缀。 + */ + private String classSuffix = ""; + + /** + * Entity 类的父类,可以自定义一些 BaseEntity 类。 + */ + private Class supperClass; + + /** + * 是否覆盖之前生成的文件。 + */ + private boolean overwriteEnable; + + /** + * Entity 默认实现的接口。 + */ + private Class[] implInterfaces = {Serializable.class}; + + /** + * Entity 是否使用 Lombok 注解。 + */ + private boolean withLombok; + + /** + * 获取类前缀。 + */ + public String getClassPrefix() { + return classPrefix; + } + + /** + * 设置类前缀。 + */ + public EntityConfig setClassPrefix(String classPrefix) { + this.classPrefix = classPrefix; + return this; + } + + /** + * 获取类后缀。 + */ + public String getClassSuffix() { + return classSuffix; + } + + /** + * 设置类后缀。 + */ + public EntityConfig setClassSuffix(String classSuffix) { + this.classSuffix = classSuffix; + return this; + } + + /** + * 获取父类。 + */ + public Class getSupperClass() { + return supperClass; + } + + /** + * 设置父类。 + */ + public EntityConfig setSupperClass(Class supperClass) { + this.supperClass = supperClass; + return this; + } + + /** + * 是否覆盖原有文件。 + */ + public boolean isOverwriteEnable() { + return overwriteEnable; + } + + /** + * 设置是否覆盖原有文件。 + */ + public EntityConfig setOverwriteEnable(boolean overwriteEnable) { + this.overwriteEnable = overwriteEnable; + return this; + } + + /** + * 获取实现接口。 + */ + public Class[] getImplInterfaces() { + return implInterfaces; + } + + /** + * 设置实现接口。 + */ + public EntityConfig setImplInterfaces(Class... implInterfaces) { + this.implInterfaces = implInterfaces; + return this; + } + + /** + * 是否使用 Lombok。 + */ + public boolean isWithLombok() { + return withLombok; + } + + /** + * 设置是否使用 Lombok。 + */ + public EntityConfig setWithLombok(boolean withLombok) { + this.withLombok = withLombok; + return this; + } + +} \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java index 7c636677..6bc7aeac 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/GlobalConfig.java @@ -1,706 +1,1270 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.config; -import com.mybatisflex.codegen.template.EnjoyTemplate; import com.mybatisflex.codegen.template.ITemplate; -import com.mybatisflex.core.BaseMapper; -import com.mybatisflex.core.util.StringUtil; -import com.mybatisflex.core.service.IService; -import com.mybatisflex.spring.service.impl.ServiceImpl; -import java.io.Serializable; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.function.UnaryOperator; +/** + * 代码生成全局配置类。 + * + * @author Michael Yang + * @author 王帅 + * @since 2023-05-15 + */ +@SuppressWarnings("unused") public class GlobalConfig { - //代码生成目录 - private String sourceDir; + // === 必须配置 === - //根包名 - private String basePackage = "com.mybatisflex"; + private final JavadocConfig javadocConfig; + private final PackageConfig packageConfig; + private final StrategyConfig strategyConfig; + private final TemplateConfig templateConfig; - //entity 的包名 - private String entityPackage; + // === 可选配置 === - //entity 类的前缀 - private String entityClassPrefix; + private EntityConfig entityConfig; + private MapperConfig mapperConfig; + private ServiceConfig serviceConfig; + private ServiceImplConfig serviceImplConfig; + private ControllerConfig controllerConfig; + private TableDefConfig tableDefConfig; + private MapperXmlConfig mapperXmlConfig; - //entity 类的后缀 - private String entityClassSuffix; + // === 其他配置 === - //entity 类的父类,可以自定义一些 BaseEntity 类 - private Class entitySupperClass; + private Map customConfig; - //entity 默认实现的接口 - private Class[] entityInterfaces = {Serializable.class}; + // === 是否启用生成 === - //entity 是否使用 Lombok - private boolean entityWithLombok = false; + private boolean entityGenerateEnable; + private boolean mapperGenerateEnable; + private boolean serviceGenerateEnable; + private boolean serviceImplGenerateEnable; + private boolean controllerGenerateEnable; + private boolean tableDefGenerateEnable; + private boolean mapperXmlGenerateEnable; + private boolean packageInfoGenerateEnable; - private boolean tableDefGenerateEnable = false; + public GlobalConfig() { + this.javadocConfig = new JavadocConfig(); + this.packageConfig = new PackageConfig(); + this.strategyConfig = new StrategyConfig(); + this.templateConfig = new TemplateConfig(); + } - //tableDef 的包名 - private String tableDefPackage; + // === 分类配置 === - //tableDef 类的前缀 - private String tableDefClassPrefix; + public JavadocConfig getJavadocConfig() { + return javadocConfig; + } - //tableDef 类的后缀 - private String tableDefClassSuffix = "Def"; + public PackageConfig getPackageConfig() { + return packageConfig; + } - //是否生成 mapper 类 - private boolean mapperGenerateEnable = false; + public StrategyConfig getStrategyConfig() { + return strategyConfig; + } - //是否覆盖已经存在的 mapper - private boolean mapperOverwriteEnable = false; + public TemplateConfig getTemplateConfig() { + return templateConfig; + } - //mapper 类的前缀 - private String mapperClassPrefix; + public EntityConfig getEntityConfig() { + if (entityConfig == null) { + entityConfig = new EntityConfig(); + } + return entityConfig; + } - //mapper 类的后缀 - private String mapperClassSuffix = "Mapper"; + public MapperConfig getMapperConfig() { + if (mapperConfig == null) { + mapperConfig = new MapperConfig(); + } + return mapperConfig; + } - //mapper 的包名 - private String mapperPackage; + public ServiceConfig getServiceConfig() { + if (serviceConfig == null) { + serviceConfig = new ServiceConfig(); + } + return serviceConfig; + } - //自定义 mapper 的父类 - private Class mapperSupperClass = BaseMapper.class; + public ServiceImplConfig getServiceImplConfig() { + if (serviceImplConfig == null) { + serviceImplConfig = new ServiceImplConfig(); + } + return serviceImplConfig; + } - //是否生成 service 类 - private boolean serviceGenerateEnable = false; + public ControllerConfig getControllerConfig() { + if (controllerConfig == null) { + controllerConfig = new ControllerConfig(); + } + return controllerConfig; + } - //是否覆盖已经存在的 service - private boolean serviceOverwriteEnable = false; + public TableDefConfig getTableDefConfig() { + if (tableDefConfig == null) { + tableDefConfig = new TableDefConfig(); + } + return tableDefConfig; + } - //service 类的前缀 - private String serviceClassPrefix; + public MapperXmlConfig getMapperXmlConfig() { + if (mapperXmlConfig == null) { + mapperXmlConfig = new MapperXmlConfig(); + } + return mapperXmlConfig; + } - //service 类的后缀 - private String serviceClassSuffix = "Service"; + // === 启用配置 === - //service 的包名 - private String servicePackage; + public EntityConfig enableEntity() { + entityGenerateEnable = true; + return getEntityConfig(); + } - //自定义 service 的父类 - private Class serviceSupperClass = IService.class; + public MapperConfig enableMapper() { + mapperGenerateEnable = true; + return getMapperConfig(); + } - //是否生成 serviceImpl 类 - private boolean serviceImplGenerateEnable = false; + public ServiceConfig enableService() { + serviceGenerateEnable = true; + return getServiceConfig(); + } - //是否覆盖已经存在的 serviceImpl - private boolean serviceImplOverwriteEnable = false; + public ServiceImplConfig enableServiceImpl() { + serviceImplGenerateEnable = true; + return getServiceImplConfig(); + } - //serviceImpl 类的前缀 - private String serviceImplClassPrefix; + public ControllerConfig enableController() { + controllerGenerateEnable = true; + return getControllerConfig(); + } - //serviceImpl 类的后缀 - private String serviceImplClassSuffix = "ServiceImpl"; + public TableDefConfig enableTableDef() { + tableDefGenerateEnable = true; + return getTableDefConfig(); + } - //serviceImpl 的包名 - private String serviceImplPackage; + public MapperXmlConfig enableMapperXml() { + mapperXmlGenerateEnable = true; + return getMapperXmlConfig(); + } - //自定义 serviceImpl 的父类 - private Class serviceImplSupperClass = ServiceImpl.class; + public void enablePackageInfo() { + packageInfoGenerateEnable = true; + } - //是否生成 controller 类 - private boolean controllerGenerateEnable = false; + // === 禁用配置 === - //是否覆盖已经存在的 controller - private boolean controllerOverwriteEnable = false; + public void disableEntity() { + entityGenerateEnable = false; + } - //controller 类的前缀 - private String controllerClassPrefix; + public void disableMapper() { + mapperGenerateEnable = false; + } - //controller 类的后缀 - private String controllerClassSuffix = "Controller"; + public void disableService() { + serviceGenerateEnable = false; + } - //controller 的包名 - private String controllerPackage; + public void disableServiceImpl() { + serviceImplGenerateEnable = false; + } - //自定义 controller 的父类 - private Class controllerSupperClass; + public void disableController() { + controllerGenerateEnable = false; + } - //rest 风格的 Controller - private boolean restStyleController = true; + public void disableTableDef() { + tableDefGenerateEnable = false; + } - //数据库表前缀,多个前缀用英文逗号(,) 隔开 - private String tablePrefix; + public void disableMapperXml() { + mapperXmlGenerateEnable = false; + } - //逻辑删除的默认字段名称 - private String logicDeleteColumn; + public void disablePackageInfo() { + packageInfoGenerateEnable = false; + } - //乐观锁的字段名称 - private String versionColumn; + // === 自定义配置 === - //是否生成视图映射 - private boolean generateForView = false; + public Object getCustomConfig(String key) { + if (customConfig != null) { + return customConfig.get(key); + } + return null; + } - //单独为某张表添加独立的配置 - private Map tableConfigMap; + public void setCustomConfig(String key, Object value) { + if (customConfig == null) { + customConfig = new HashMap<>(); + } + customConfig.put(key, value); + } - //设置某个列的全局配置 - private Map defaultColumnConfigMap; + // === 分项配置 === - //生成哪些表,白名单 - private Set generateTables; + /** + * @see JavadocConfig#getAuthor() + */ + public String getAuthor() { + return getJavadocConfig().getAuthor(); + } - //不生成哪些表,黑名单 - private Set unGenerateTables; + /** + * @see JavadocConfig#setAuthor(String) + */ + public void setAuthor(String author) { + getJavadocConfig().setAuthor(author); + } - //使用哪个模板引擎来生成代码 - protected ITemplate templateEngine; + /** + * @see JavadocConfig#getSince() + */ + public String getSince() { + return getJavadocConfig().getSince(); + } - //其他自定义配置 - private Map others; + /** + * @see JavadocConfig#setSince(String) + */ + public void setSince(String since) { + getJavadocConfig().setSince(since); + } + /** + * @see JavadocConfig#setSince(Supplier) + */ + public void setSince(Supplier since) { + getJavadocConfig().setSince(since); + } + /** + * @see JavadocConfig#getTableCommentFormat() + */ + public Function getTableCommentFormat() { + return getJavadocConfig().getTableCommentFormat(); + } + + /** + * @see JavadocConfig#setTableCommentFormat(UnaryOperator) + */ + public void setTableCommentFormat(UnaryOperator tableCommentFormat) { + getJavadocConfig().setTableCommentFormat(tableCommentFormat); + } + + /** + * @see JavadocConfig#getEntityPackage() + */ + public String getEntityPackageComment() { + return getJavadocConfig().getEntityPackage(); + } + + /** + * @see JavadocConfig#setEntityPackage(String) + */ + public void setEntityPackageComment(String entityPackageComment) { + getJavadocConfig().setEntityPackage(entityPackageComment); + } + + /** + * @see JavadocConfig#getMapperPackage() + */ + public String getMapperPackageComment() { + return getJavadocConfig().getMapperPackage(); + } + + /** + * @see JavadocConfig#setMapperPackage(String) + */ + public void setMapperPackageComment(String mapperPackageComment) { + getJavadocConfig().setMapperPackage(mapperPackageComment); + } + + /** + * @see JavadocConfig#getServicePackage() + */ + public String getServicePackageComment() { + return getJavadocConfig().getServicePackage(); + } + + /** + * @see JavadocConfig#setServicePackage(String) + */ + public void setServicePackageComment(String servicePackageComment) { + getJavadocConfig().setServicePackage(servicePackageComment); + } + + /** + * @see JavadocConfig#getServiceImplPackage() + */ + public String getServiceImplPackageComment() { + return getJavadocConfig().getServiceImplPackage(); + } + + /** + * @see JavadocConfig#setServiceImplPackage(String) + */ + public void setServiceImplPackageComment(String serviceImplPackageComment) { + getJavadocConfig().setServiceImplPackage(serviceImplPackageComment); + } + + /** + * @see JavadocConfig#getControllerPackage() + */ + public String getControllerPackageComment() { + return getJavadocConfig().getControllerPackage(); + } + + /** + * @see JavadocConfig#setControllerPackage(String) + */ + public void setControllerPackageComment(String controllerPackageComment) { + getJavadocConfig().setControllerPackage(controllerPackageComment); + } + + /** + * @see JavadocConfig#getTableDefPackage() + */ + public String getTableDefPackageComment() { + return getJavadocConfig().getTableDefPackage(); + } + + /** + * @see JavadocConfig#setTableDefPackage(String) + */ + public void setTableDefPackageComment(String tableDefPackageComment) { + getJavadocConfig().setTableDefPackage(tableDefPackageComment); + } + + /** + * @see PackageConfig#getSourceDir() + */ public String getSourceDir() { - if (sourceDir == null || sourceDir.trim().length() == 0) { - return System.getProperty("user.dir") + "/src/main/java"; - } - return sourceDir; + return getPackageConfig().getSourceDir(); } + /** + * @see PackageConfig#setSourceDir(String) + */ public void setSourceDir(String sourceDir) { - this.sourceDir = StringUtil.trimOrNull(sourceDir); + getPackageConfig().setSourceDir(sourceDir); } + /** + * @see PackageConfig#getBasePackage() + */ public String getBasePackage() { - return basePackage; + return getPackageConfig().getBasePackage(); } + /** + * @see PackageConfig#setBasePackage(String) + */ public void setBasePackage(String basePackage) { - this.basePackage = StringUtil.trimOrNull(basePackage); + getPackageConfig().setBasePackage(basePackage); } + /** + * @see PackageConfig#getEntityPackage() + */ public String getEntityPackage() { - if (StringUtil.isBlank(entityPackage)) { - entityPackage = basePackage + ".entity"; - } - return entityPackage; + return getPackageConfig().getEntityPackage(); } + /** + * @see PackageConfig#setEntityPackage(String) + */ public void setEntityPackage(String entityPackage) { - this.entityPackage = StringUtil.trimOrNull(entityPackage); + getPackageConfig().setEntityPackage(entityPackage); } - public String getEntityClassPrefix() { - if (StringUtil.isBlank(entityClassPrefix)) { - return ""; - } - return entityClassPrefix; + /** + * @see PackageConfig#getMapperPackage() + */ + public String getMapperPackage() { + return getPackageConfig().getMapperPackage(); } - public void setEntityClassPrefix(String entityClassPrefix) { - this.entityClassPrefix = StringUtil.trimOrNull(entityClassPrefix); + /** + * @see PackageConfig#setMapperPackage(String) + */ + public void setMapperPackage(String mapperPackage) { + getPackageConfig().setMapperPackage(mapperPackage); } - public String getEntityClassSuffix() { - if (StringUtil.isBlank(entityClassSuffix)) { - return ""; - } - return entityClassSuffix; + /** + * @see PackageConfig#getServicePackage() + */ + public String getServicePackage() { + return getPackageConfig().getServicePackage(); } - public void setEntityClassSuffix(String entityClassSuffix) { - this.entityClassSuffix = StringUtil.trimOrNull(entityClassSuffix); + /** + * @see PackageConfig#setServicePackage(String) + */ + public void setServicePackage(String servicePackage) { + getPackageConfig().setServicePackage(servicePackage); } - public Class getEntitySupperClass() { - return entitySupperClass; + /** + * @see PackageConfig#getServiceImplPackage() + */ + public String getServiceImplPackage() { + return getPackageConfig().getServiceImplPackage(); } - public void setEntitySupperClass(Class entitySupperClass) { - this.entitySupperClass = entitySupperClass; + /** + * @see PackageConfig#setServiceImplPackage(String) + */ + public void setServiceImplPackage(String serviceImplPackage) { + getPackageConfig().setServiceImplPackage(serviceImplPackage); } - public Class[] getEntityInterfaces() { - return entityInterfaces; + /** + * @see PackageConfig#getControllerPackage() + */ + public String getControllerPackage() { + return getPackageConfig().getControllerPackage(); } - public void setEntityInterfaces(Class[] entityInterfaces) { - this.entityInterfaces = entityInterfaces; - } - - public boolean isEntityWithLombok() { - return entityWithLombok; - } - - public void setEntityWithLombok(boolean entityWithLombok) { - this.entityWithLombok = entityWithLombok; - } - - public boolean isTableDefGenerateEnable() { - return tableDefGenerateEnable; - } - - public void setTableDefGenerateEnable(boolean tableDefGenerateEnable) { - this.tableDefGenerateEnable = tableDefGenerateEnable; + /** + * @see PackageConfig#setControllerPackage(String) + */ + public void setControllerPackage(String controllerPackage) { + getPackageConfig().setControllerPackage(controllerPackage); } + /** + * @see PackageConfig#getTableDefPackage() + */ public String getTableDefPackage() { - if (StringUtil.isBlank(tableDefPackage)) { - return getEntityPackage() + ".tables"; - } - return tableDefPackage; + return getPackageConfig().getTableDefPackage(); } + /** + * @see PackageConfig#setTableDefPackage(String) + */ public void setTableDefPackage(String tableDefPackage) { - this.tableDefPackage = StringUtil.trimOrNull(tableDefPackage); + getPackageConfig().setTableDefPackage(tableDefPackage); } - public String getTableDefClassPrefix() { - if (StringUtil.isBlank(tableDefClassPrefix)) { - return ""; - } - return tableDefClassPrefix; + /** + * @see PackageConfig#getMapperXmlPath() + */ + public String getMapperXmlPath() { + return getPackageConfig().getMapperXmlPath(); } - public void setTableDefClassPrefix(String tableDefClassPrefix) { - this.tableDefClassPrefix = StringUtil.trimOrNull(tableDefClassPrefix); + /** + * @see PackageConfig#setMapperXmlPath(String) + */ + public void setMapperXmlPath(String mapperXmlPath) { + getPackageConfig().setMapperXmlPath(mapperXmlPath); } - public String getTableDefClassSuffix() { - return tableDefClassSuffix; + /** + * @see StrategyConfig#getTablePrefix() + */ + public String getTablePrefix() { + return getStrategyConfig().getTablePrefix(); } - public void setTableDefClassSuffix(String tableDefClassSuffix) { - this.tableDefClassSuffix = StringUtil.trimOrNull(tableDefClassSuffix); + /** + * @see StrategyConfig#setTablePrefix(String) + */ + public void setTablePrefix(String tablePrefix) { + getStrategyConfig().setTablePrefix(tablePrefix); + } + + /** + * @see StrategyConfig#getLogicDeleteColumn() + */ + public String getLogicDeleteColumn() { + return getStrategyConfig().getLogicDeleteColumn(); + } + + /** + * @see StrategyConfig#setLogicDeleteColumn(String) + */ + public void setLogicDeleteColumn(String logicDeleteColumn) { + getStrategyConfig().setLogicDeleteColumn(logicDeleteColumn); + } + + /** + * @see StrategyConfig#getVersionColumn() + */ + public String getVersionColumn() { + return getStrategyConfig().getVersionColumn(); + } + + /** + * @see StrategyConfig#setVersionColumn(String) + */ + public void setVersionColumn(String versionColumn) { + getStrategyConfig().setVersionColumn(versionColumn); + } + + /** + * @see StrategyConfig#getTableConfigMap() + */ + public Map getTableConfigMap() { + return getStrategyConfig().getTableConfigMap(); + } + + /** + * @see StrategyConfig#setTableConfigMap(Map) + */ + public void setTableConfigMap(Map tableConfigMap) { + getStrategyConfig().setTableConfigMap(tableConfigMap); + } + + /** + * @see StrategyConfig#setTableConfig(TableConfig) + */ + public void setTableConfig(TableConfig tableConfig) { + getStrategyConfig().setTableConfig(tableConfig); + } + + /** + * @see StrategyConfig#getTableConfig(String) + */ + public TableConfig getTableConfig(String tableName) { + return getStrategyConfig().getTableConfig(tableName); + } + + /** + * @see StrategyConfig#getColumnConfigMap() + */ + public Map getColumnConfigMap() { + return getStrategyConfig().getColumnConfigMap(); + } + + /** + * @see StrategyConfig#setColumnConfigMap(Map) + */ + public void setColumnConfigMap(Map columnConfigMap) { + getStrategyConfig().setColumnConfigMap(columnConfigMap); + } + + /** + * @see StrategyConfig#setColumnConfig(ColumnConfig) + */ + public void setColumnConfig(ColumnConfig columnConfig) { + getStrategyConfig().setColumnConfig(columnConfig); + } + + /** + * @see StrategyConfig#setColumnConfig(String, ColumnConfig) + */ + public void setColumnConfig(String tableName, ColumnConfig columnConfig) { + getStrategyConfig().setColumnConfig(tableName, columnConfig); + } + + /** + * @see StrategyConfig#getColumnConfig(String, String) + */ + public ColumnConfig getColumnConfig(String tableName, String columnName) { + return getStrategyConfig().getColumnConfig(tableName, columnName); + } + + /** + * @see StrategyConfig#isGenerateForView() + */ + public boolean isGenerateForView() { + return getStrategyConfig().isGenerateForView(); + } + + /** + * @see StrategyConfig#setGenerateForView(boolean) + */ + public void setGenerateForView(boolean generateForView) { + getStrategyConfig().setGenerateForView(generateForView); + } + + /** + * @see StrategyConfig#getGenerateTables() + */ + public Set getGenerateTables() { + return getStrategyConfig().getGenerateTables(); + } + + /** + * @see StrategyConfig#setGenerateTables(Set) + */ + public void setGenerateTables(Set generateTables) { + getStrategyConfig().setGenerateTables(generateTables); + } + + /** + * @see StrategyConfig#setGenerateTable(String...) + */ + public void setGenerateTable(String... tables) { + getStrategyConfig().setGenerateTable(tables); + } + + /** + * @see StrategyConfig#getUnGenerateTables() + */ + public Set getUnGenerateTables() { + return getStrategyConfig().getUnGenerateTables(); + } + + /** + * @see StrategyConfig#setUnGenerateTables(Set) + */ + public void setUnGenerateTables(Set unGenerateTables) { + getStrategyConfig().setUnGenerateTables(unGenerateTables); + } + + /** + * @see StrategyConfig#setUnGenerateTable(String...) + */ + public void setUnGenerateTable(String... tables) { + getStrategyConfig().setUnGenerateTable(tables); + } + + /** + * @see TemplateConfig#getTemplate() + */ + public ITemplate getTemplateEngine() { + return getTemplateConfig().getTemplate(); + } + + /** + * @see TemplateConfig#setTemplate(ITemplate) + */ + public void setTemplateEngine(ITemplate template) { + getTemplateConfig().setTemplate(template); + } + + /** + * @see TemplateConfig#getEntity() + */ + public String getEntityTemplatePath() { + return getTemplateConfig().getEntity(); + } + + /** + * @see TemplateConfig#setEntity(String) + */ + public void setEntityTemplatePath(String entityTemplatePath) { + getTemplateConfig().setEntity(entityTemplatePath); + } + + /** + * @see TemplateConfig#getMapper() + */ + public String getMapperTemplatePath() { + return getTemplateConfig().getMapper(); + } + + /** + * @see TemplateConfig#setMapper(String) + */ + public void setMapperTemplatePath(String mapperTemplatePath) { + getTemplateConfig().setMapper(mapperTemplatePath); + } + + /** + * @see TemplateConfig#getService() + */ + public String getServiceTemplatePath() { + return getTemplateConfig().getService(); + } + + /** + * @see TemplateConfig#setService(String) + */ + public void setServiceTemplatePath(String serviceTemplatePath) { + getTemplateConfig().setService(serviceTemplatePath); + } + + /** + * @see TemplateConfig#getServiceImpl() + */ + public String getServiceImplTemplatePath() { + return getTemplateConfig().getServiceImpl(); + } + + /** + * @see TemplateConfig#setServiceImpl(String) + */ + public void setServiceImplTemplatePath(String serviceImplTemplatePath) { + getTemplateConfig().setServiceImpl(serviceImplTemplatePath); + } + + /** + * @see TemplateConfig#getController() + */ + public String getControllerTemplatePath() { + return getTemplateConfig().getController(); + } + + /** + * @see TemplateConfig#setController(String) + */ + public void setControllerTemplatePath(String controllerTemplatePath) { + getTemplateConfig().setController(controllerTemplatePath); + } + + /** + * @see TemplateConfig#getTableDef() + */ + public String getTableDefTemplatePath() { + return getTemplateConfig().getTableDef(); + } + + /** + * @see TemplateConfig#setTableDef(String) + */ + public void setTableDefTemplatePath(String tableDefTemplatePath) { + getTemplateConfig().setTableDef(tableDefTemplatePath); + } + + /** + * @see TemplateConfig#getMapperXml() + */ + public String getMapperXmlTemplatePath() { + return getTemplateConfig().getMapperXml(); + } + + /** + * @see TemplateConfig#setMapperXml(String) + */ + public void setMapperXmlTemplatePath(String mapperXmlTemplatePath) { + getTemplateConfig().setMapperXml(mapperXmlTemplatePath); + } + + public boolean isEntityGenerateEnable() { + return entityGenerateEnable; + } + + /** + * @see #enableEntity() + * @see #disableEntity() + */ + public void setEntityGenerateEnable(boolean entityGenerateEnable) { + this.entityGenerateEnable = entityGenerateEnable; + } + + /** + * @see EntityConfig#isOverwriteEnable() + */ + public boolean isEntityOverwriteEnable() { + return getEntityConfig().isOverwriteEnable(); + } + + /** + * @see EntityConfig#setOverwriteEnable(boolean) + */ + public void setEntityOverwriteEnable(boolean entityOverwriteEnable) { + getEntityConfig().setOverwriteEnable(entityOverwriteEnable); + } + + /** + * @see EntityConfig#getClassPrefix() + */ + public String getEntityClassPrefix() { + return getEntityConfig().getClassPrefix(); + } + + /** + * @see EntityConfig#setClassPrefix(String) + */ + public void setEntityClassPrefix(String entityClassPrefix) { + getEntityConfig().setClassPrefix(entityClassPrefix); + } + + /** + * @see EntityConfig#getClassSuffix() + */ + public String getEntityClassSuffix() { + return getEntityConfig().getClassSuffix(); + } + + /** + * @see EntityConfig#setClassSuffix(String) + */ + public void setEntityClassSuffix(String entityClassSuffix) { + getEntityConfig().setClassSuffix(entityClassSuffix); + } + + /** + * @see EntityConfig#getSupperClass() + */ + public Class getEntitySupperClass() { + return getEntityConfig().getSupperClass(); + } + + /** + * @see EntityConfig#setSupperClass(Class) + */ + public void setEntitySupperClass(Class entitySupperClass) { + getEntityConfig().setSupperClass(entitySupperClass); + } + + /** + * @see EntityConfig#getImplInterfaces() + */ + public Class[] getEntityInterfaces() { + return getEntityConfig().getImplInterfaces(); + } + + /** + * @see EntityConfig#setImplInterfaces(Class[]) + */ + public void setEntityInterfaces(Class[] entityInterfaces) { + getEntityConfig().setImplInterfaces(entityInterfaces); + } + + /** + * @see EntityConfig#isWithLombok() + */ + public boolean isEntityWithLombok() { + return getEntityConfig().isWithLombok(); + } + + /** + * @see EntityConfig#setWithLombok(boolean) + */ + public void setEntityWithLombok(boolean entityWithLombok) { + getEntityConfig().setWithLombok(entityWithLombok); } public boolean isMapperGenerateEnable() { return mapperGenerateEnable; } + /** + * @see #enableMapper() + * @see #disableMapper() + */ public void setMapperGenerateEnable(boolean mapperGenerateEnable) { this.mapperGenerateEnable = mapperGenerateEnable; } + /** + * @see MapperConfig#isOverwriteEnable() + */ public boolean isMapperOverwriteEnable() { - return mapperOverwriteEnable; + return getMapperConfig().isOverwriteEnable(); } + /** + * @see MapperConfig#setOverwriteEnable(boolean) + */ public void setMapperOverwriteEnable(boolean mapperOverwriteEnable) { - this.mapperOverwriteEnable = mapperOverwriteEnable; + getMapperConfig().setOverwriteEnable(mapperOverwriteEnable); } + /** + * @see MapperConfig#getClassPrefix() + */ public String getMapperClassPrefix() { - if (StringUtil.isBlank(mapperClassPrefix)) { - return ""; - } - return mapperClassPrefix; + return getMapperConfig().getClassPrefix(); } + /** + * @see MapperConfig#setClassPrefix(String) + */ public void setMapperClassPrefix(String mapperClassPrefix) { - this.mapperClassPrefix = StringUtil.trimOrNull(mapperClassPrefix); + getMapperConfig().setClassPrefix(mapperClassPrefix); } + /** + * @see MapperConfig#getClassSuffix() + */ public String getMapperClassSuffix() { - return mapperClassSuffix; + return getMapperConfig().getClassSuffix(); } + /** + * @see MapperConfig#setClassSuffix(String) + */ public void setMapperClassSuffix(String mapperClassSuffix) { - this.mapperClassSuffix = StringUtil.trimOrNull(mapperClassSuffix); - } - - public String getMapperPackage() { - if (StringUtil.isBlank(mapperPackage)) { - mapperPackage = basePackage + ".mapper"; - } - return mapperPackage; - } - - public void setMapperPackage(String mapperPackage) { - this.mapperPackage = StringUtil.trimOrNull(mapperPackage); + getMapperConfig().setClassSuffix(mapperClassSuffix); } + /** + * @see MapperConfig#getSupperClass() + */ public Class getMapperSupperClass() { - return mapperSupperClass; + return getMapperConfig().getSupperClass(); } + /** + * @see MapperConfig#setSupperClass(Class) + */ public void setMapperSupperClass(Class mapperSupperClass) { - this.mapperSupperClass = mapperSupperClass; + getMapperConfig().setSupperClass(mapperSupperClass); } public boolean isServiceGenerateEnable() { return serviceGenerateEnable; } + /** + * @see #enableService() + * @see #disableService() + */ public void setServiceGenerateEnable(boolean serviceGenerateEnable) { this.serviceGenerateEnable = serviceGenerateEnable; } + /** + * @see ServiceConfig#isOverwriteEnable() + */ public boolean isServiceOverwriteEnable() { - return serviceOverwriteEnable; + return getServiceConfig().isOverwriteEnable(); } + /** + * @see ServiceConfig#setOverwriteEnable(boolean) + */ public void setServiceOverwriteEnable(boolean serviceOverwriteEnable) { - this.serviceOverwriteEnable = serviceOverwriteEnable; + getServiceConfig().setOverwriteEnable(serviceOverwriteEnable); } + /** + * @see ServiceConfig#getClassPrefix() + */ public String getServiceClassPrefix() { - if (StringUtil.isBlank(serviceClassPrefix)) { - return ""; - } - return serviceClassPrefix; + return getServiceConfig().getClassPrefix(); } + /** + * @see ServiceConfig#setClassPrefix(String) + */ public void setServiceClassPrefix(String serviceClassPrefix) { - this.serviceClassPrefix = StringUtil.trimOrNull(serviceClassPrefix); + getServiceConfig().setClassPrefix(serviceClassPrefix); } + /** + * @see ServiceConfig#getClassSuffix() + */ public String getServiceClassSuffix() { - return serviceClassSuffix; + return getServiceConfig().getClassSuffix(); } + /** + * @see ServiceConfig#setClassSuffix(String) + */ public void setServiceClassSuffix(String serviceClassSuffix) { - this.serviceClassSuffix = StringUtil.trimOrNull(serviceClassSuffix); - } - - public String getServicePackage() { - if (StringUtil.isBlank(servicePackage)) { - servicePackage = basePackage + ".service"; - } - return servicePackage; - } - - public void setServicePackage(String servicePackage) { - this.servicePackage = StringUtil.trimOrNull(servicePackage); + getServiceConfig().setClassSuffix(serviceClassSuffix); } + /** + * @see ServiceConfig#getSupperClass() + */ public Class getServiceSupperClass() { - return serviceSupperClass; + return getServiceConfig().getSupperClass(); } + /** + * @see ServiceConfig#setSupperClass(Class) + */ public void setServiceSupperClass(Class serviceSupperClass) { - this.serviceSupperClass = serviceSupperClass; + getServiceConfig().setSupperClass(serviceSupperClass); } public boolean isServiceImplGenerateEnable() { return serviceImplGenerateEnable; } + /** + * @see #enableServiceImpl() + * @see #disableServiceImpl() + */ public void setServiceImplGenerateEnable(boolean serviceImplGenerateEnable) { this.serviceImplGenerateEnable = serviceImplGenerateEnable; } + /** + * @see ServiceImplConfig#isOverwriteEnable() + */ public boolean isServiceImplOverwriteEnable() { - return serviceImplOverwriteEnable; + return getServiceImplConfig().isOverwriteEnable(); } + /** + * @see ServiceImplConfig#setOverwriteEnable(boolean) + */ public void setServiceImplOverwriteEnable(boolean serviceImplOverwriteEnable) { - this.serviceImplOverwriteEnable = serviceImplOverwriteEnable; + getServiceImplConfig().setOverwriteEnable(serviceImplOverwriteEnable); } + /** + * @see ServiceImplConfig#getClassPrefix() + */ public String getServiceImplClassPrefix() { - if (StringUtil.isBlank(serviceImplClassPrefix)) { - return ""; - } - return serviceImplClassPrefix; + return getServiceImplConfig().getClassPrefix(); } + /** + * @see ServiceImplConfig#setClassPrefix(String) + */ public void setServiceImplClassPrefix(String serviceImplClassPrefix) { - this.serviceImplClassPrefix = StringUtil.trimOrNull(serviceImplClassPrefix); + getServiceImplConfig().setClassPrefix(serviceImplClassPrefix); } + /** + * @see ServiceImplConfig#getClassSuffix() + */ public String getServiceImplClassSuffix() { - return serviceImplClassSuffix; + return getServiceImplConfig().getClassSuffix(); } + /** + * @see ServiceImplConfig#setClassSuffix(String) + */ public void setServiceImplClassSuffix(String serviceImplClassSuffix) { - this.serviceImplClassSuffix = StringUtil.trimOrNull(serviceImplClassSuffix); - } - - public String getServiceImplPackage() { - if (StringUtil.isBlank(serviceImplPackage)) { - serviceImplPackage = basePackage + ".service.impl"; - } - return serviceImplPackage; - } - - public void setServiceImplPackage(String serviceImplPackage) { - this.serviceImplPackage = StringUtil.trimOrNull(serviceImplPackage); + getServiceImplConfig().setClassSuffix(serviceImplClassSuffix); } + /** + * @see ServiceImplConfig#getSupperClass() + */ public Class getServiceImplSupperClass() { - return serviceImplSupperClass; + return getServiceImplConfig().getSupperClass(); } + /** + * @see ServiceImplConfig#setSupperClass(Class) + */ public void setServiceImplSupperClass(Class serviceImplSupperClass) { - this.serviceImplSupperClass = serviceImplSupperClass; + getServiceImplConfig().setSupperClass(serviceImplSupperClass); + } + + /** + * @see ControllerConfig#isOverwriteEnable() + */ + public boolean isControllerOverwriteEnable() { + return getControllerConfig().isOverwriteEnable(); } public boolean isControllerGenerateEnable() { return controllerGenerateEnable; } + /** + * @see ControllerConfig#setOverwriteEnable(boolean) + */ + public void setControllerOverwriteEnable(boolean controllerOverwriteEnable) { + getControllerConfig().setOverwriteEnable(controllerOverwriteEnable); + } + + /** + * @see #enableController() + * @see #disableController() + */ public void setControllerGenerateEnable(boolean controllerGenerateEnable) { this.controllerGenerateEnable = controllerGenerateEnable; } - public boolean isControllerOverwriteEnable() { - return controllerOverwriteEnable; - } - - public void setControllerOverwriteEnable(boolean controllerOverwriteEnable) { - this.controllerOverwriteEnable = controllerOverwriteEnable; - } - + /** + * @see ControllerConfig#getClassPrefix() + */ public String getControllerClassPrefix() { - if (StringUtil.isBlank(controllerClassPrefix)) { - return ""; - } - return controllerClassPrefix; + return getControllerConfig().getClassPrefix(); } + /** + * @see ControllerConfig#setClassPrefix(String) + */ public void setControllerClassPrefix(String controllerClassPrefix) { - this.controllerClassPrefix = StringUtil.trimOrNull(controllerClassPrefix); + getControllerConfig().setClassPrefix(controllerClassPrefix); } + /** + * @see ControllerConfig#getClassSuffix() + */ public String getControllerClassSuffix() { - return controllerClassSuffix; + return getControllerConfig().getClassSuffix(); } + /** + * @see ControllerConfig#setClassSuffix(String) + */ public void setControllerClassSuffix(String controllerClassSuffix) { - this.controllerClassSuffix = StringUtil.trimOrNull(controllerClassSuffix); - } - - public String getControllerPackage() { - if (StringUtil.isBlank(controllerPackage)) { - controllerPackage = basePackage + ".controller"; - } - return controllerPackage; - } - - public void setControllerPackage(String controllerPackage) { - this.controllerPackage = StringUtil.trimOrNull(controllerPackage); + getControllerConfig().setClassSuffix(controllerClassSuffix); } + /** + * @see ControllerConfig#getSupperClass() + */ public Class getControllerSupperClass() { - return controllerSupperClass; + return getControllerConfig().getSupperClass(); } + /** + * @see ControllerConfig#setSupperClass(Class) + */ public void setControllerSupperClass(Class controllerSupperClass) { - this.controllerSupperClass = controllerSupperClass; + getControllerConfig().setSupperClass(controllerSupperClass); } - public boolean isRestStyleController() { - return restStyleController; + /** + * @see ControllerConfig#isRestStyle() + */ + public boolean isControllerRestStyle() { + return getControllerConfig().isRestStyle(); } - public void setRestStyleController(boolean restStyleController) { - this.restStyleController = restStyleController; + /** + * @see ControllerConfig#setRestStyle(boolean) + */ + public void setControllerRestStyle(boolean restStyle) { + getControllerConfig().setRestStyle(restStyle); } - public String getTablePrefix() { - return tablePrefix; + public boolean isTableDefGenerateEnable() { + return tableDefGenerateEnable; } - public void setTablePrefix(String tablePrefix) { - this.tablePrefix = StringUtil.trimOrNull(tablePrefix); + /** + * @see #enableTableDef() + * @see #disableTableDef() + */ + public void setTableDefGenerateEnable(boolean tableDefGenerateEnable) { + this.tableDefGenerateEnable = tableDefGenerateEnable; } - public String getLogicDeleteColumn() { - return logicDeleteColumn; + /** + * @see TableDefConfig#isOverwriteEnable() + */ + public boolean isTableDefOverwriteEnable() { + return getTableDefConfig().isOverwriteEnable(); } - public void setLogicDeleteColumn(String logicDeleteColumn) { - this.logicDeleteColumn = StringUtil.trimOrNull(logicDeleteColumn); + /** + * @see TableDefConfig#setOverwriteEnable(boolean) + */ + public void setTableDefOverwriteEnable(boolean tableDefOverwriteEnable) { + getTableDefConfig().setOverwriteEnable(tableDefOverwriteEnable); } - public String getVersionColumn() { - return versionColumn; + /** + * @see TableDefConfig#getClassPrefix() + */ + public String getTableDefClassPrefix() { + return getTableDefConfig().getClassPrefix(); } - public void setVersionColumn(String versionColumn) { - this.versionColumn = StringUtil.trimOrNull(versionColumn); + /** + * @see TableDefConfig#setClassPrefix(String) + */ + public void setTableDefClassPrefix(String tableDefClassPrefix) { + getTableDefConfig().setClassPrefix(tableDefClassPrefix); } - public Map getTableConfigMap() { - return tableConfigMap; + /** + * @see TableDefConfig#getClassSuffix() + */ + public String getTableDefClassSuffix() { + return getTableDefConfig().getClassSuffix(); } - public void setTableConfigMap(Map tableConfigMap) { - this.tableConfigMap = tableConfigMap; + /** + * @see TableDefConfig#setClassSuffix(String) + */ + public void setTableDefClassSuffix(String tableDefClassSuffix) { + getTableDefConfig().setClassSuffix(tableDefClassSuffix); } - public void addTableConfig(TableConfig tableConfig) { - if (tableConfigMap == null) { - tableConfigMap = new HashMap<>(); - } - tableConfigMap.put(tableConfig.getTableName(), tableConfig); + public boolean isMapperXmlGenerateEnable() { + return mapperXmlGenerateEnable; } - public TableConfig getTableConfig(String tableName) { - return tableConfigMap == null ? null : tableConfigMap.get(tableName); + /** + * @see #enableMapperXml() + * @see #disableMapperXml() + */ + public void setMapperXmlGenerateEnable(boolean mapperXmlGenerateEnable) { + this.mapperXmlGenerateEnable = mapperXmlGenerateEnable; } - public Map getDefaultColumnConfigMap() { - return defaultColumnConfigMap; + /** + * @see MapperXmlConfig#isOverwriteEnable() + */ + public boolean isMapperXmlOverwriteEnable() { + return getMapperXmlConfig().isOverwriteEnable(); } - public void setDefaultColumnConfigMap(Map defaultColumnConfigMap) { - this.defaultColumnConfigMap = defaultColumnConfigMap; + /** + * @see MapperXmlConfig#setOverwriteEnable(boolean) + */ + public void setMapperXmlOverwriteEnable(boolean mapperXmlOverwriteEnable) { + getMapperXmlConfig().setOverwriteEnable(mapperXmlOverwriteEnable); } - - public void addColumnConfig(ColumnConfig columnConfig) { - if (defaultColumnConfigMap == null) { - defaultColumnConfigMap = new HashMap<>(); - } - defaultColumnConfigMap.put(columnConfig.getColumnName(), columnConfig); + /** + * @see MapperXmlConfig#getFilePrefix() + */ + public String getMapperXmlFilePrefix() { + return getMapperXmlConfig().getFilePrefix(); } - public void addColumnConfig(String tableName, ColumnConfig columnConfig) { - TableConfig tableConfig = getTableConfig(tableName); - if (tableConfig == null) { - tableConfig = new TableConfig(); - tableConfig.setTableName(tableName); - addTableConfig(tableConfig); - } - - tableConfig.addColumnConfig(columnConfig); + /** + * @see MapperXmlConfig#setFilePrefix(String) + */ + public void setMapperXmlFilePrefix(String mapperXmlFilePrefix) { + getMapperXmlConfig().setFilePrefix(mapperXmlFilePrefix); } - - public ColumnConfig getColumnConfig(String tableName, String columnName) { - ColumnConfig columnConfig = null; - - TableConfig tableConfig = getTableConfig(tableName); - if (tableConfig != null) { - columnConfig = tableConfig.getColumnConfig(columnName); - } - - if (columnConfig == null && defaultColumnConfigMap != null) { - columnConfig = defaultColumnConfigMap.get(columnName); - } - - if (columnConfig == null) { - columnConfig = new ColumnConfig(); - } - - //全局配置的逻辑删除 - if (columnName.equals(logicDeleteColumn) && columnConfig.getLogicDelete() == null) { - columnConfig.setLogicDelete(true); - } - - //全部配置的乐观锁版本 - if (columnName.equals(versionColumn) && columnConfig.getVersion() == null) { - columnConfig.setVersion(true); - } - - - return columnConfig; + /** + * @see MapperXmlConfig#getFileSuffix() + */ + public String getMapperXmlFileSuffix() { + return getMapperXmlConfig().getFileSuffix(); } - public boolean isGenerateForView() { - return generateForView; + /** + * @see MapperXmlConfig#setFileSuffix(String) + */ + public void setMapperXmlFileSuffix(String mapperXmlFileSuffix) { + getMapperXmlConfig().setFileSuffix(mapperXmlFileSuffix); } - public void setGenerateForView(boolean generateForView) { - this.generateForView = generateForView; + public boolean isPackageInfoGenerateEnable() { + return packageInfoGenerateEnable; } - public Set getGenerateTables() { - return generateTables; + /** + * @see #enablePackageInfo() + * @see #disablePackageInfo() + */ + public void setPackageInfoGenerateEnable(boolean packageInfoGenerateEnable) { + this.packageInfoGenerateEnable = packageInfoGenerateEnable; } - public void setGenerateTables(Set generateTables) { - this.generateTables = generateTables; - } - - public void addGenerateTable(String... tables) { - if (generateTables == null) { - generateTables = new HashSet<>(); - } - - for (String table : tables) { - if (table != null && table.trim().length() > 0) { - generateTables.add(table.trim()); - } - } - } - - public Set getUnGenerateTables() { - return unGenerateTables; - } - - public void setUnGenerateTables(Set unGenerateTables) { - this.unGenerateTables = unGenerateTables; - } - - - public void addUnGenerateTable(String... tables) { - if (unGenerateTables == null) { - unGenerateTables = new HashSet<>(); - } - - for (String table : tables) { - if (table != null && table.trim().length() > 0) { - unGenerateTables.add(table.trim()); - } - } - } - - public boolean isSupportGenerate(String table) { - if (unGenerateTables != null && unGenerateTables.contains(table)) { - return false; - } - - //不配置指定比表名的情况下,支持所有表 - if (generateTables == null || generateTables.isEmpty()) { - return true; - } - - for (String generateTable : generateTables) { - if (generateTable.equals(table)) { - return true; - } - } - - return false; - } - - public ITemplate getTemplateEngine() { - if (templateEngine == null) { - templateEngine = new EnjoyTemplate(); - } - return templateEngine; - } - - public void setTemplateEngine(ITemplate templateEngine) { - this.templateEngine = templateEngine; - } - - public Map getOthers() { - return others; - } - - public void setOthers(Map others) { - this.others = others; - } - - public void addConfig(String key, Object value) { - if (others == null) { - others = new HashMap<>(); - } - others.put(key, value); - } } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/JavadocConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/JavadocConfig.java new file mode 100644 index 00000000..2ba8beb4 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/JavadocConfig.java @@ -0,0 +1,228 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.codegen.config; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + +/** + * 注释配置类。 + * + * @author 王帅 + * @since 2023-05-17 + */ +@SuppressWarnings("unused") +public class JavadocConfig { + + /** + * 作者。 + */ + private String author = System.getProperty("user.name"); + + /** + * 自。 + */ + private Supplier since = () -> DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDate.now()); + + /** + * 表名格式化。 + */ + private UnaryOperator tableCommentFormat = UnaryOperator.identity(); + + /** + * Entity 包注释。 + */ + private String entityPackage = "实体类层(Entity)软件包。"; + + /** + * Mapper 包注释。 + */ + private String mapperPackage = "映射层(Mapper)软件包。"; + + /** + * Service 包注释。 + */ + private String servicePackage = "服务层(Service)软件包。"; + + /** + * ServiceImpl 包注释。 + */ + private String serviceImplPackage = "服务层实现(ServiceImpl)软件包。"; + + /** + * Controller 包注释。 + */ + private String controllerPackage = "控制层(Controller)软件包。"; + + /** + * TableDef 包注释。 + */ + private String tableDefPackage = "表定义层(TableDef)软件包。"; + + /** + * 获取作者。 + */ + public String getAuthor() { + return author; + } + + /** + * 设置作者。 + */ + public JavadocConfig setAuthor(String author) { + this.author = author; + return this; + } + + /** + * 获取自。 + */ + public String getSince() { + return since.get(); + } + + /** + * 设置自。 + */ + public JavadocConfig setSince(String since) { + this.since = () -> since; + return this; + } + + /** + * 设置自。 + */ + public JavadocConfig setSince(Supplier since) { + this.since = since; + return this; + } + + public String formatTableComment(String comment) { + return tableCommentFormat.apply(comment); + } + + /** + * 获取表注释格式化。 + */ + public Function getTableCommentFormat() { + return tableCommentFormat; + } + + /** + * 设置表注释格式化方案,用来生成实体类注释。 + */ + public JavadocConfig setTableCommentFormat(UnaryOperator tableCommentFormat) { + this.tableCommentFormat = tableCommentFormat; + return this; + } + + /** + * 获取实体类层包注释。 + */ + public String getEntityPackage() { + return entityPackage; + } + + /** + * 设置实体类层包注释。 + */ + public JavadocConfig setEntityPackage(String entityPackage) { + this.entityPackage = entityPackage; + return this; + } + + /** + * 获取映射层包注释。 + */ + public String getMapperPackage() { + return mapperPackage; + } + + /** + * 设置映射层包注释。 + */ + public JavadocConfig setMapperPackage(String mapperPackage) { + this.mapperPackage = mapperPackage; + return this; + } + + /** + * 获取服务层包注释。 + */ + public String getServicePackage() { + return servicePackage; + } + + /** + * 设置服务层包注释。 + */ + public JavadocConfig setServicePackage(String servicePackage) { + this.servicePackage = servicePackage; + return this; + } + + /** + * 获取服务层实现包注释。 + * + * @return {@link String} + */ + public String getServiceImplPackage() { + return serviceImplPackage; + } + + /** + * 设置服务层实现包注释。 + */ + public JavadocConfig setServiceImplPackage(String serviceImplPackage) { + this.serviceImplPackage = serviceImplPackage; + return this; + } + + /** + * 获取控制层包注释。 + */ + public String getControllerPackage() { + return controllerPackage; + } + + /** + * 设置控制层包注释。 + */ + public JavadocConfig setControllerPackage(String controllerPackage) { + this.controllerPackage = controllerPackage; + return this; + } + + /** + * 获取表定义层包注释。 + */ + public String getTableDefPackage() { + return tableDefPackage; + } + + /** + * 设置表定义层包注释。 + */ + public JavadocConfig setTableDefPackage(String tableDefPackage) { + this.tableDefPackage = tableDefPackage; + return this; + } + +} \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperConfig.java new file mode 100644 index 00000000..62fc4905 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperConfig.java @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.codegen.config; + +import com.mybatisflex.core.BaseMapper; + +/** + * 生成 Mapper 的配置。 + * + * @author 王帅 + * @since 2023-05-15 + */ +@SuppressWarnings("unused") +public class MapperConfig { + + /** + * Mapper 类的前缀。 + */ + private String classPrefix = ""; + + /** + * Mapper 类的后缀。 + */ + private String classSuffix = "Mapper"; + + /** + * 自定义 Mapper 的父类。 + */ + private Class supperClass = BaseMapper.class; + + /** + * 是否覆盖之前生成的文件。 + */ + private boolean overwriteEnable; + + public String buildSuperClassImport() { + return supperClass.getName(); + } + + public String buildSuperClassName() { + return supperClass.getSimpleName(); + } + + /** + * 获取类前缀。 + */ + public String getClassPrefix() { + return classPrefix; + } + + /** + * 设置类前缀。 + */ + public MapperConfig setClassPrefix(String classPrefix) { + this.classPrefix = classPrefix; + return this; + } + + /** + * 获取类后缀。 + */ + public String getClassSuffix() { + return classSuffix; + } + + /** + * 设置类后缀。 + */ + public MapperConfig setClassSuffix(String classSuffix) { + this.classSuffix = classSuffix; + return this; + } + + /** + * 获取父类。 + */ + public Class getSupperClass() { + return supperClass; + } + + /** + * 设置父类。 + */ + public MapperConfig setSupperClass(Class supperClass) { + this.supperClass = supperClass; + return this; + } + + /** + * 是否覆盖原有文件。 + */ + public boolean isOverwriteEnable() { + return overwriteEnable; + } + + /** + * 设置是否覆盖原有文件。 + */ + public MapperConfig setOverwriteEnable(boolean overwriteEnable) { + this.overwriteEnable = overwriteEnable; + return this; + } + +} \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperXmlConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperXmlConfig.java new file mode 100644 index 00000000..dfa5fa47 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperXmlConfig.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.codegen.config; + +/** + * 生成 MapperXml 的配置。 + * + * @author 王帅 + * @since 2023-05-17 + */ +@SuppressWarnings("unused") +public class MapperXmlConfig { + + /** + * MapperXml 文件的前缀。 + */ + private String filePrefix = ""; + + /** + * MapperXml 文件的后缀。 + */ + private String fileSuffix = "Mapper"; + + /** + * 是否覆盖之前生成的文件。 + */ + private boolean overwriteEnable; + + /** + * 获取文件前缀。 + */ + public String getFilePrefix() { + return filePrefix; + } + + /** + * 设置文件前缀。 + */ + public MapperXmlConfig setFilePrefix(String filePrefix) { + this.filePrefix = filePrefix; + return this; + } + + /** + * 获取文件后缀。 + */ + public String getFileSuffix() { + return fileSuffix; + } + + /** + * 设置文件后缀。 + */ + public MapperXmlConfig setFileSuffix(String fileSuffix) { + this.fileSuffix = fileSuffix; + return this; + } + + /** + * 是否覆盖原有文件。 + */ + public boolean isOverwriteEnable() { + return overwriteEnable; + } + + /** + * 设置是否覆盖原有文件。 + */ + public MapperXmlConfig setOverwriteEnable(boolean overwriteEnable) { + this.overwriteEnable = overwriteEnable; + return this; + } + +} \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/PackageConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/PackageConfig.java new file mode 100644 index 00000000..24cbc012 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/PackageConfig.java @@ -0,0 +1,234 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.codegen.config; + +import com.mybatisflex.core.util.StringUtil; + +/** + * 生成软件包的配置。 + * + * @author 王帅 + * @since 2023-05-15 + */ +@SuppressWarnings("unused") +public class PackageConfig { + + /** + * 代码生成目录。 + */ + private String sourceDir; + + /** + * 根包。 + */ + private String basePackage = "com.mybatisflex"; + + /** + * Entity 所在包。 + */ + private String entityPackage; + + /** + * Mapper 所在包。 + */ + private String mapperPackage; + + /** + * Service 所在包。 + */ + private String servicePackage; + + /** + * ServiceImpl 所在包。 + */ + private String serviceImplPackage; + + /** + * Controller 所在包。 + */ + private String controllerPackage; + + /** + * TableDef 所在包。 + */ + private String tableDefPackage; + + /** + * MapperXml 文件所在位置。 + */ + private String mapperXmlPath; + + /** + * 获取生成目录。 + */ + public String getSourceDir() { + if (StringUtil.isBlank(sourceDir)) { + return System.getProperty("user.dir") + "/src/main/java"; + } + return sourceDir; + } + + /** + * 设置生成目录。 + */ + public PackageConfig setSourceDir(String sourceDir) { + this.sourceDir = sourceDir; + return this; + } + + /** + * 获得根包路径。 + */ + public String getBasePackage() { + return basePackage; + } + + /** + * 设置根包路径。 + */ + public PackageConfig setBasePackage(String basePackage) { + this.basePackage = basePackage; + return this; + } + + /** + * 获取实体类层包路径。 + */ + public String getEntityPackage() { + if (StringUtil.isBlank(entityPackage)) { + return basePackage.concat(".entity"); + } + return entityPackage; + } + + /** + * 设置实体类层包路径。 + */ + public PackageConfig setEntityPackage(String entityPackage) { + this.entityPackage = entityPackage; + return this; + } + + /** + * 获取映射层包路径。 + */ + public String getMapperPackage() { + if (StringUtil.isBlank(mapperPackage)) { + return basePackage.concat(".mapper"); + } + return mapperPackage; + } + + /** + * 设置映射层包路径。 + */ + public PackageConfig setMapperPackage(String mapperPackage) { + this.mapperPackage = mapperPackage; + return this; + } + + /** + * 获取服务层包路径。 + */ + public String getServicePackage() { + if (StringUtil.isBlank(servicePackage)) { + return basePackage.concat(".service"); + } + return servicePackage; + } + + /** + * 设置服务层包路径。 + */ + public PackageConfig setServicePackage(String servicePackage) { + this.servicePackage = servicePackage; + return this; + } + + /** + * 获取服务层实现包路径。 + */ + public String getServiceImplPackage() { + if (StringUtil.isBlank(serviceImplPackage)) { + return basePackage.concat(".service.impl"); + } + return serviceImplPackage; + } + + /** + * 设置服务层实现包路径。 + */ + public PackageConfig setServiceImplPackage(String serviceImplPackage) { + this.serviceImplPackage = serviceImplPackage; + return this; + } + + /** + * 获取控制层包路径。 + */ + public String getControllerPackage() { + if (StringUtil.isBlank(controllerPackage)) { + return basePackage.concat(".controller"); + } + return controllerPackage; + } + + /** + * 设置控制层包路径。 + */ + public PackageConfig setControllerPackage(String controllerPackage) { + this.controllerPackage = controllerPackage; + return this; + } + + /** + * 获取表定义层包路径。 + */ + public String getTableDefPackage() { + if (StringUtil.isBlank(tableDefPackage)) { + return getEntityPackage().concat(".tables"); + } + return tableDefPackage; + } + + /** + * 设置表定义层包路径。 + */ + public PackageConfig setTableDefPackage(String tableDefPackage) { + this.tableDefPackage = tableDefPackage; + return this; + } + + /** + * 获取 Mapper XML 文件路径。 + */ + public String getMapperXmlPath() { + if (StringUtil.isBlank(mapperXmlPath)) { + return getSourceDir().concat("/resources/mapper"); + } + return mapperXmlPath; + } + + /** + * 设置 Mapper XML 文件路径。 + */ + public PackageConfig setMapperXmlPath(String mapperXmlPath) { + this.mapperXmlPath = mapperXmlPath; + return this; + } + +} \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceConfig.java new file mode 100644 index 00000000..17be7dd3 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceConfig.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.codegen.config; + + +import com.mybatisflex.core.service.IService; + +/** + * 生成 Service 的配置。 + * + * @author 王帅 + * @since 2023-05-15 + */ +@SuppressWarnings("unused") +public class ServiceConfig { + + /** + * Service 类的前缀。 + */ + private String classPrefix = ""; + + /** + * Service 类的后缀。 + */ + private String classSuffix = "Service"; + + /** + * 自定义 Service 的父类。 + */ + private Class supperClass = IService.class; + + /** + * 是否覆盖之前生成的文件。 + */ + private boolean overwriteEnable; + + public String buildSuperClassImport() { + return supperClass.getName(); + } + + public String buildSuperClassName() { + return supperClass.getSimpleName(); + } + + /** + * 获取类前缀。 + */ + public String getClassPrefix() { + return classPrefix; + } + + /** + * 设置类前缀。 + */ + public ServiceConfig setClassPrefix(String classPrefix) { + this.classPrefix = classPrefix; + return this; + } + + /** + * 获取类后缀。 + */ + public String getClassSuffix() { + return classSuffix; + } + + /** + * 设置类后缀。 + */ + public ServiceConfig setClassSuffix(String classSuffix) { + this.classSuffix = classSuffix; + return this; + } + + /** + * 获取父类。 + */ + public Class getSupperClass() { + return supperClass; + } + + /** + * 设置父类。 + */ + public ServiceConfig setSupperClass(Class supperClass) { + this.supperClass = supperClass; + return this; + } + + /** + * 是否覆盖原有文件。 + */ + public boolean isOverwriteEnable() { + return overwriteEnable; + } + + /** + * 设置是否覆盖原有文件。 + */ + public ServiceConfig setOverwriteEnable(boolean overwriteEnable) { + this.overwriteEnable = overwriteEnable; + return this; + } + +} \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceImplConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceImplConfig.java new file mode 100644 index 00000000..0c0d5523 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceImplConfig.java @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.codegen.config; + +import com.mybatisflex.spring.service.impl.ServiceImpl; + +/** + * 生成 ServiceImpl 的配置。 + * + * @author 王帅 + * @since 2023-05-15 + */ +@SuppressWarnings("unused") +public class ServiceImplConfig { + + /** + * ServiceImpl 类的前缀。 + */ + private String classPrefix = ""; + + /** + * ServiceImpl 类的后缀。 + */ + private String classSuffix = "ServiceImpl"; + + /** + * 自定义 ServiceImpl 的父类。 + */ + private Class supperClass = ServiceImpl.class; + + /** + * 是否覆盖之前生成的文件。 + */ + private boolean overwriteEnable; + + public String buildSuperClassImport() { + return supperClass.getName(); + } + + public String buildSuperClassName() { + return supperClass.getSimpleName(); + } + + /** + * 获取类前缀。 + */ + public String getClassPrefix() { + return classPrefix; + } + + /** + * 设置类前缀。 + */ + public ServiceImplConfig setClassPrefix(String classPrefix) { + this.classPrefix = classPrefix; + return this; + } + + /** + * 获取类后缀。 + */ + public String getClassSuffix() { + return classSuffix; + } + + /** + * 设置类后缀。 + */ + public ServiceImplConfig setClassSuffix(String classSuffix) { + this.classSuffix = classSuffix; + return this; + } + + /** + * 获取父类。 + */ + public Class getSupperClass() { + return supperClass; + } + + /** + * 设置父类。 + */ + public ServiceImplConfig setSupperClass(Class supperClass) { + this.supperClass = supperClass; + return this; + } + + /** + * 是否覆盖原有文件。 + */ + public boolean isOverwriteEnable() { + return overwriteEnable; + } + + /** + * 设置是否覆盖原有文件。 + */ + public ServiceImplConfig setOverwriteEnable(boolean overwriteEnable) { + this.overwriteEnable = overwriteEnable; + return this; + } + +} \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java new file mode 100644 index 00000000..917053ac --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java @@ -0,0 +1,316 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.codegen.config; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * 表策略配置。 + * + * @author 王帅 + * @since 2023-05-14 + */ +@SuppressWarnings("unused") +public class StrategyConfig { + + /** + * 数据库表前缀,多个前缀用英文逗号(,) 隔开。 + */ + private String tablePrefix; + + /** + * 逻辑删除的默认字段名称。 + */ + private String logicDeleteColumn; + + /** + * 乐观锁的字段名称。 + */ + private String versionColumn; + + /** + * 是否生成视图映射。 + */ + private boolean generateForView; + + /** + * 单独为某张表添加独立的配置。 + */ + private Map tableConfigMap; + + /** + * 设置某个列的全局配置。 + */ + private Map columnConfigMap; + + /** + * 生成哪些表,白名单。 + */ + private Set generateTables; + + /** + * 不生成哪些表,黑名单。 + */ + private Set unGenerateTables; + + /** + * 获取表配置。 + */ + public TableConfig getTableConfig(String tableName) { + return tableConfigMap == null ? null : tableConfigMap.get(tableName); + } + + /** + * 设置表配置。 + */ + public void setTableConfig(TableConfig tableConfig) { + if (tableConfigMap == null) { + tableConfigMap = new HashMap<>(); + } + tableConfigMap.put(tableConfig.getTableName(), tableConfig); + } + + /** + * 获取列配置。 + */ + public ColumnConfig getColumnConfig(String tableName, String columnName) { + ColumnConfig columnConfig = null; + + TableConfig tableConfig = getTableConfig(tableName); + if (tableConfig != null) { + columnConfig = tableConfig.getColumnConfig(columnName); + } + + if (columnConfig == null && columnConfigMap != null) { + columnConfig = columnConfigMap.get(columnName); + } + + if (columnConfig == null) { + columnConfig = new ColumnConfig(); + } + + //全局配置的逻辑删除 + if (columnName.equals(logicDeleteColumn) && columnConfig.getLogicDelete() == null) { + columnConfig.setLogicDelete(true); + } + + //全部配置的乐观锁版本 + if (columnName.equals(versionColumn) && columnConfig.getVersion() == null) { + columnConfig.setVersion(true); + } + + + return columnConfig; + } + + /** + * 设置列配置。 + */ + public void setColumnConfig(ColumnConfig columnConfig) { + if (columnConfigMap == null) { + columnConfigMap = new HashMap<>(); + } + columnConfigMap.put(columnConfig.getColumnName(), columnConfig); + } + + /** + * 设置列配置。 + */ + public void setColumnConfig(String tableName, ColumnConfig columnConfig) { + TableConfig tableConfig = getTableConfig(tableName); + if (tableConfig == null) { + tableConfig = new TableConfig(); + tableConfig.setTableName(tableName); + setTableConfig(tableConfig); + } + + tableConfig.addColumnConfig(columnConfig); + } + + /** + * 设置生成哪些表。 + */ + public void setGenerateTable(String... tables) { + if (generateTables == null) { + generateTables = new HashSet<>(); + } + + for (String table : tables) { + if (table != null && table.trim().length() > 0) { + generateTables.add(table.trim()); + } + } + } + + /** + * 设置不生成哪些表。 + */ + public void setUnGenerateTable(String... tables) { + if (unGenerateTables == null) { + unGenerateTables = new HashSet<>(); + } + + for (String table : tables) { + if (table != null && table.trim().length() > 0) { + unGenerateTables.add(table.trim()); + } + } + } + + public boolean isSupportGenerate(String table) { + if (unGenerateTables != null && unGenerateTables.contains(table)) { + return false; + } + + //不配置指定比表名的情况下,支持所有表 + if (generateTables == null || generateTables.isEmpty()) { + return true; + } + + for (String generateTable : generateTables) { + if (generateTable.equals(table)) { + return true; + } + } + + return false; + } + + /** + * 获取表前缀。 + */ + public String getTablePrefix() { + return tablePrefix; + } + + /** + * 设置表前缀。 + */ + public StrategyConfig setTablePrefix(String tablePrefix) { + this.tablePrefix = tablePrefix; + return this; + } + + /** + * 获取逻辑删除列。 + */ + public String getLogicDeleteColumn() { + return logicDeleteColumn; + } + + /** + * 设置逻辑删除列。 + */ + public StrategyConfig setLogicDeleteColumn(String logicDeleteColumn) { + this.logicDeleteColumn = logicDeleteColumn; + return this; + } + + /** + * 获取版本列。 + */ + public String getVersionColumn() { + return versionColumn; + } + + /** + * 设置版本列。 + */ + public StrategyConfig setVersionColumn(String versionColumn) { + this.versionColumn = versionColumn; + return this; + } + + /** + * 是否生成视图。 + */ + public boolean isGenerateForView() { + return generateForView; + } + + /** + * 设置是否生成视图。 + */ + public StrategyConfig setGenerateForView(boolean generateForView) { + this.generateForView = generateForView; + return this; + } + + /** + * 获取表配置。 + */ + public Map getTableConfigMap() { + return tableConfigMap; + } + + /** + * 设置表配置。 + */ + public StrategyConfig setTableConfigMap(Map tableConfigMap) { + this.tableConfigMap = tableConfigMap; + return this; + } + + /** + * 获取列配置。 + */ + public Map getColumnConfigMap() { + return columnConfigMap; + } + + /** + * 设置列配置。 + */ + public StrategyConfig setColumnConfigMap(Map columnConfigMap) { + this.columnConfigMap = columnConfigMap; + return this; + } + + /** + * 获取生成哪些表。 + */ + public Set getGenerateTables() { + return generateTables; + } + + /** + * 设置生成哪些表。 + */ + public StrategyConfig setGenerateTables(Set generateTables) { + this.generateTables = generateTables; + return this; + } + + /** + * 获取不生成哪些表。 + */ + public Set getUnGenerateTables() { + return unGenerateTables; + } + + /** + * 设置不生成哪些表。 + */ + public StrategyConfig setUnGenerateTables(Set unGenerateTables) { + this.unGenerateTables = unGenerateTables; + return this; + } + +} \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableDefConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableDefConfig.java new file mode 100644 index 00000000..f909ac11 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableDefConfig.java @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.codegen.config; + +/** + * 生成 TableDef 的配置。 + * + * @author 王帅 + * @since 2023-05-15 + */ +@SuppressWarnings("unused") +public class TableDefConfig { + + /** + * TableDef 类的前缀。 + */ + private String classPrefix = ""; + + /** + * TableDef 类的后缀。 + */ + private String classSuffix = "Def"; + + /** + * 是否覆盖之前生成的文件。 + */ + private boolean overwriteEnable; + + /** + * 获取类前缀。 + */ + public String getClassPrefix() { + return classPrefix; + } + + /** + * 设置类前缀。 + */ + public TableDefConfig setClassPrefix(String classPrefix) { + this.classPrefix = classPrefix; + return this; + } + + /** + * 获取类后缀。 + */ + public String getClassSuffix() { + return classSuffix; + } + + /** + * 设置类后缀。 + */ + public TableDefConfig setClassSuffix(String classSuffix) { + this.classSuffix = classSuffix; + return this; + } + + /** + * 是否覆盖原有文件。 + */ + public boolean isOverwriteEnable() { + return overwriteEnable; + } + + /** + * 设置是否覆盖原有文件。 + */ + public TableDefConfig setOverwriteEnable(boolean overwriteEnable) { + this.overwriteEnable = overwriteEnable; + return this; + } + +} \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TemplateConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TemplateConfig.java new file mode 100644 index 00000000..b0e0af8f --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TemplateConfig.java @@ -0,0 +1,157 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.codegen.config; + +import com.mybatisflex.codegen.constant.GenTypeConst; +import com.mybatisflex.codegen.generator.GeneratorFactory; +import com.mybatisflex.codegen.template.ITemplate; +import com.mybatisflex.codegen.template.impl.EnjoyTemplate; + +/** + * 模板配置。 + * + * @author 王帅 + * @since 2023-05-17 + */ +@SuppressWarnings("unused") +public class TemplateConfig { + + /** + * 生成代码的模板引擎。 + */ + private ITemplate template = new EnjoyTemplate(); + + /** + * 获取模板引擎。 + */ + public ITemplate getTemplate() { + return template; + } + + /** + * 设置模板引擎。 + */ + public TemplateConfig setTemplate(ITemplate template) { + this.template = template; + return this; + } + + /** + * 获取生成 Entity 模板文件的位置。 + */ + public String getEntity() { + return GeneratorFactory.getGenerator(GenTypeConst.ENTITY).getTemplatePath(); + } + + /** + * 设置生成 Entity 模板文件的位置。 + */ + public TemplateConfig setEntity(String entity) { + GeneratorFactory.getGenerator(GenTypeConst.ENTITY).setTemplatePath(entity); + return this; + } + + /** + * 获取生成 Mapper 模板文件的位置。 + */ + public String getMapper() { + return GeneratorFactory.getGenerator(GenTypeConst.MAPPER).getTemplatePath(); + } + + /** + * 设置生成 Mapper 模板文件的位置。 + */ + public TemplateConfig setMapper(String mapper) { + GeneratorFactory.getGenerator(GenTypeConst.MAPPER).setTemplatePath(mapper); + return this; + } + + /** + * 获取生成 Service 模板文件的位置。 + */ + public String getService() { + return GeneratorFactory.getGenerator(GenTypeConst.SERVICE).getTemplatePath(); + } + + /** + * 设置生成 Service 模板文件的位置。 + */ + public TemplateConfig setService(String service) { + GeneratorFactory.getGenerator(GenTypeConst.SERVICE).setTemplatePath(service); + return this; + } + + /** + * 获取生成 ServiceImpl 模板文件的位置。 + */ + public String getServiceImpl() { + return GeneratorFactory.getGenerator(GenTypeConst.SERVICE_IMPL).getTemplatePath(); + } + + /** + * 设置生成 ServiceImpl 模板文件的位置。 + */ + public TemplateConfig setServiceImpl(String serviceImpl) { + GeneratorFactory.getGenerator(GenTypeConst.SERVICE_IMPL).setTemplatePath(serviceImpl); + return this; + } + + /** + * 获取生成 Controller 模板文件的位置。 + */ + public String getController() { + return GeneratorFactory.getGenerator(GenTypeConst.CONTROLLER).getTemplatePath(); + } + + /** + * 设置生成 Controller 模板文件的位置。 + */ + public TemplateConfig setController(String controller) { + GeneratorFactory.getGenerator(GenTypeConst.CONTROLLER).setTemplatePath(controller); + return this; + } + + /** + * 获取生成 TableDef 模板文件的位置。 + */ + public String getTableDef() { + return GeneratorFactory.getGenerator(GenTypeConst.TABLE_DEF).getTemplatePath(); + } + + /** + * 设置生成 TableDef 模板文件的位置。 + */ + public TemplateConfig setTableDef(String tableDef) { + GeneratorFactory.getGenerator(GenTypeConst.TABLE_DEF).setTemplatePath(tableDef); + return this; + } + + /** + * 获取生成 MapperXml 模板文件的位置。 + */ + public String getMapperXml() { + return GeneratorFactory.getGenerator(GenTypeConst.MAPPER_XML).getTemplatePath(); + } + + /** + * 设置生成 MapperXml 模板文件的位置。 + */ + public TemplateConfig setMapperXml(String mapperXml) { + GeneratorFactory.getGenerator(GenTypeConst.MAPPER_XML).setTemplatePath(mapperXml); + return this; + } + +} \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/GenTypeConst.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/GenTypeConst.java new file mode 100644 index 00000000..e91969cf --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/GenTypeConst.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.codegen.constant; + +/** + * 生成类型常量。 + * + * @author 王帅 + * @since 2023-05-17 + */ +public class GenTypeConst { + + public static final String ENTITY = "entity"; + public static final String MAPPER = "mapper"; + public static final String SERVICE = "service"; + public static final String SERVICE_IMPL = "serviceImpl"; + public static final String CONTROLLER = "controller"; + public static final String TABLE_DEF = "tableDef"; + public static final String MAPPER_XML = "mapperXml"; + public static final String PACKAGE_INFO = "package-info"; + + + private GenTypeConst() { + } + +} \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/TemplateConst.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/TemplateConst.java new file mode 100644 index 00000000..21a14621 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/TemplateConst.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.codegen.constant; + +/** + * 代码生成模板常量池。 + * + * @author 王帅 + * @since 2023-05-16 + */ +public final class TemplateConst { + + public static final String ENTITY = "/templates/enjoy/entity.tpl"; + public static final String MAPPER = "/templates/enjoy/mapper.tpl"; + public static final String SERVICE = "/templates/enjoy/service.tpl"; + public static final String SERVICE_IMPL = "/templates/enjoy/serviceImpl.tpl"; + public static final String CONTROLLER = "/templates/enjoy/controller.tpl"; + public static final String TABLE_DEF = "/templates/enjoy/tableDef.tpl"; + public static final String MAPPER_XML = "/templates/enjoy/mapperXml.tpl"; + public static final String PACKAGE_INFO = "/templates/enjoy/package-info.tpl"; + + private TemplateConst() { + } + +} \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java index 686fc2bb..33bacbe3 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/dialect/JdbcDialect.java @@ -45,7 +45,7 @@ public abstract class JdbcDialect implements IDialect { column.setAutoIncrement(columnMetaData.isAutoIncrement(i)); //注释 - column.setRemarks(columnRemarks.get(column.getName())); + column.setComment(columnRemarks.get(column.getName())); table.addColumn(column); } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java index 614e5746..6daef109 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java @@ -30,7 +30,7 @@ public class Column { private String property; private String propertyType; - private String remarks; + private String comment; private boolean isPrimaryKey = false; private Boolean isAutoIncrement; @@ -63,12 +63,12 @@ public class Column { this.propertyType = propertyType; } - public String getRemarks() { - return remarks; + public String getComment() { + return comment; } - public void setRemarks(String remarks) { - this.remarks = remarks; + public void setComment(String comment) { + this.comment = comment; } public boolean isPrimaryKey() { @@ -104,12 +104,12 @@ public class Column { } - public String buildRemarks(){ - if (StringUtil.isBlank(remarks)){ + public String buildComment(){ + if (StringUtil.isBlank(comment)){ return ""; }else { StringBuilder sb = new StringBuilder("/**\n") - .append(" * ").append(remarks).append("\n") + .append(" * ").append(comment).append("\n") .append(" */"); return sb.toString(); } @@ -280,7 +280,7 @@ public class Column { return "Column{" + "name='" + name + '\'' + ", className='" + propertyType + '\'' + - ", remarks='" + remarks + '\'' + + ", remarks='" + comment + '\'' + ", isAutoIncrement=" + isAutoIncrement + '}'; } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java index 5b109735..0a229ca0 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Table.java @@ -15,8 +15,7 @@ */ package com.mybatisflex.codegen.entity; -import com.mybatisflex.codegen.config.GlobalConfig; -import com.mybatisflex.codegen.config.TableConfig; +import com.mybatisflex.codegen.config.*; import com.mybatisflex.core.util.StringUtil; import java.math.BigInteger; @@ -26,7 +25,7 @@ import java.util.stream.Collectors; public class Table { private String name; - private String remarks; + private String comment; private Set primaryKeys; private List columns = new ArrayList<>(); @@ -41,12 +40,12 @@ public class Table { this.name = name; } - public String getRemarks() { - return remarks; + public String getComment() { + return globalConfig.getJavadocConfig().formatTableComment(comment); } - public void setRemarks(String remarks) { - this.remarks = remarks; + public void setComment(String comment) { + this.comment = comment; } @@ -89,7 +88,7 @@ public class Table { column.setAutoIncrement(false); } - column.setColumnConfig(globalConfig.getColumnConfig(name, column.getName())); + column.setColumnConfig(globalConfig.getStrategyConfig().getColumnConfig(name, column.getName())); columns.add(column); } @@ -118,8 +117,10 @@ public class Table { imports.addAll(column.getImportClasses()); } + EntityConfig entityConfig = globalConfig.getEntityConfig(); + //开启 lombok - if (globalConfig.isEntityWithLombok()) { + if (entityConfig.isWithLombok()) { //import lombok.AllArgsConstructor; //import lombok.Builder; //import lombok.Data; @@ -130,12 +131,12 @@ public class Table { imports.add("lombok.NoArgsConstructor"); } - if (globalConfig.getEntitySupperClass() != null) { - imports.add(globalConfig.getEntitySupperClass().getName()); + if (entityConfig.getSupperClass() != null) { + imports.add(entityConfig.getSupperClass().getName()); } - if (globalConfig.getEntityInterfaces() != null) { - for (Class entityInterface : globalConfig.getEntityInterfaces()) { + if (entityConfig.getImplInterfaces() != null) { + for (Class entityInterface : entityConfig.getImplInterfaces()) { imports.add(entityInterface.getName()); } } @@ -155,20 +156,9 @@ public class Table { return imports.stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList()); } - public String buildRemarks(){ - if (StringUtil.isBlank(remarks)){ - return ""; - }else { - StringBuilder sb = new StringBuilder("/**\n") - .append(" * ").append(remarks).append("\n") - .append(" */"); - return sb.toString(); - } - } - public String getEntityJavaFileName() { String entityJavaFileName = name; - String tablePrefix = globalConfig.getTablePrefix(); + String tablePrefix = globalConfig.getStrategyConfig().getTablePrefix(); if (tablePrefix != null) { String[] tablePrefixes = tablePrefix.split(","); for (String prefix : tablePrefixes) { @@ -189,9 +179,10 @@ public class Table { */ public String buildEntityClassName() { String entityJavaFileName = getEntityJavaFileName(); - return globalConfig.getEntityClassPrefix() + EntityConfig entityConfig = globalConfig.getEntityConfig(); + return entityConfig.getClassPrefix() + entityJavaFileName - + globalConfig.getEntityClassSuffix(); + + entityConfig.getClassSuffix(); } /** @@ -201,21 +192,36 @@ public class Table { */ public String buildTableDefClassName() { String tableDefJavaFileName = getEntityJavaFileName(); - return globalConfig.getTableDefClassPrefix() + TableDefConfig tableDefConfig = globalConfig.getTableDefConfig(); + return tableDefConfig.getClassPrefix() + tableDefJavaFileName - + globalConfig.getTableDefClassSuffix(); + + tableDefConfig.getClassSuffix(); + } + + /** + * 构建 MapperXml 的文件名称 + * + * @return fileName + */ + public String buildMapperXmlFileName() { + String tableDefJavaFileName = getEntityJavaFileName(); + MapperXmlConfig mapperXmlConfig = globalConfig.getMapperXmlConfig(); + return mapperXmlConfig.getFilePrefix() + + tableDefJavaFileName + + mapperXmlConfig.getFileSuffix(); } public String buildExtends() { - if (globalConfig.getEntitySupperClass() != null) { - return " extends " + globalConfig.getEntitySupperClass().getSimpleName(); + EntityConfig entityConfig = globalConfig.getEntityConfig(); + if (entityConfig.getSupperClass() != null) { + return " extends " + entityConfig.getSupperClass().getSimpleName(); } else { return ""; } } public String buildImplements() { - Class[] entityInterfaces = globalConfig.getEntityInterfaces(); + Class[] entityInterfaces = globalConfig.getEntityConfig().getImplInterfaces(); if (entityInterfaces != null && entityInterfaces.length > 0) { return " implements " + StringUtil.join(", ", Arrays.stream(entityInterfaces) .map(Class::getSimpleName).collect(Collectors.toList())); @@ -227,30 +233,34 @@ public class Table { public String buildMapperClassName() { String entityJavaFileName = getEntityJavaFileName(); - return globalConfig.getMapperClassPrefix() + MapperConfig mapperConfig = globalConfig.getMapperConfig(); + return mapperConfig.getClassPrefix() + entityJavaFileName - + globalConfig.getMapperClassSuffix(); + + mapperConfig.getClassSuffix(); } public String buildServiceClassName() { String entityJavaFileName = getEntityJavaFileName(); - return globalConfig.getServiceClassPrefix() + ServiceConfig serviceConfig = globalConfig.getServiceConfig(); + return serviceConfig.getClassPrefix() + entityJavaFileName - + globalConfig.getServiceClassSuffix(); + + serviceConfig.getClassSuffix(); } public String buildServiceImplClassName() { String entityJavaFileName = getEntityJavaFileName(); - return globalConfig.getServiceImplClassPrefix() + ServiceImplConfig serviceImplConfig = globalConfig.getServiceImplConfig(); + return serviceImplConfig.getClassPrefix() + entityJavaFileName - + globalConfig.getServiceImplClassSuffix(); + + serviceImplConfig.getClassSuffix(); } public String buildControllerClassName() { String entityJavaFileName = getEntityJavaFileName(); - return globalConfig.getControllerClassPrefix() + ControllerConfig controllerConfig = globalConfig.getControllerConfig(); + return controllerConfig.getClassPrefix() + entityJavaFileName - + globalConfig.getControllerClassSuffix(); + + controllerConfig.getClassSuffix(); } /** @@ -258,7 +268,7 @@ public class Table { */ public String buildTableAnnotation() { StringBuilder tableAnnotation = new StringBuilder(); - if (globalConfig.isEntityWithLombok()) { + if (globalConfig.getEntityConfig().isWithLombok()) { //@Data //@Builder //@NoArgsConstructor @@ -273,19 +283,19 @@ public class Table { if (tableConfig != null) { if (tableConfig.getSchema() != null) { - tableAnnotation.append(", schema = \"" + tableConfig.getSchema() + "\""); + tableAnnotation.append(", schema = \"").append(tableConfig.getSchema()).append("\""); } if (tableConfig.getCamelToUnderline() != null) { - tableAnnotation.append(", camelToUnderline = \"" + tableConfig.getCamelToUnderline() + "\""); + tableAnnotation.append(", camelToUnderline = \"").append(tableConfig.getCamelToUnderline()).append("\""); } if (tableConfig.getInsertListenerClass() != null) { - tableAnnotation.append(", onInsert = " + tableConfig.getInsertListenerClass().getSimpleName() + ".class"); + tableAnnotation.append(", onInsert = ").append(tableConfig.getInsertListenerClass().getSimpleName()).append(".class"); } if (tableConfig.getUpdateListenerClass() != null) { - tableAnnotation.append(", onUpdate = " + tableConfig.getUpdateListenerClass().getSimpleName() + ".class"); + tableAnnotation.append(", onUpdate = ").append(tableConfig.getUpdateListenerClass().getSimpleName()).append(".class"); } if (tableConfig.getSetListenerClass() != null) { - tableAnnotation.append(", onSet = " + tableConfig.getUpdateListenerClass().getSimpleName() + ".class"); + tableAnnotation.append(", onSet = ").append(tableConfig.getUpdateListenerClass().getSimpleName()).append(".class"); } if (Boolean.FALSE.equals(tableConfig.getMapperGenerateEnable())) { tableAnnotation.append(", mapperGenerateEnable = false"); @@ -294,40 +304,11 @@ public class Table { return tableAnnotation.append(")").toString(); } - public String buildMapperImport() { - return globalConfig.getMapperSupperClass().getName(); - } - - public String buildServiceImport() { - return globalConfig.getServiceSupperClass().getName(); - } - - public String buildServiceImplImport() { - return globalConfig.getServiceImplSupperClass().getName(); - } - - public String buildMapperName() { - return globalConfig.getMapperSupperClass().getSimpleName(); - } - - public String buildServiceName() { - return globalConfig.getServiceSupperClass().getSimpleName(); - } - - public String buildServiceImplName() { - return globalConfig.getServiceImplSupperClass().getSimpleName(); - } - - public String buildControllerName() { - return globalConfig.getControllerSupperClass().getSimpleName(); - } - - @Override public String toString() { return "Table{" + "name='" + name + '\'' + - ", remarks='" + remarks + '\'' + + ", remarks='" + comment + '\'' + ", primaryKeys='" + primaryKeys + '\'' + ", columns=" + columns + '}'; diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/GeneratorFactory.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/GeneratorFactory.java index d82a2e3f..9bfb5b68 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/GeneratorFactory.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/GeneratorFactory.java @@ -1,20 +1,21 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.generator; +import com.mybatisflex.codegen.constant.GenTypeConst; import com.mybatisflex.codegen.generator.impl.*; import java.util.Collection; @@ -26,12 +27,18 @@ public class GeneratorFactory { private static final Map generators = new HashMap<>(); static { - registerGenerator("entity", new EntityGenerator()); - registerGenerator("mapper", new MapperGenerator()); - registerGenerator("service", new ServiceGenerator()); - registerGenerator("serviceImpl", new ServiceImplGenerator()); - registerGenerator("controller", new ControllerGenerator()); - registerGenerator("tableDef", new TableDefGenerator()); + registerGenerator(GenTypeConst.ENTITY, new EntityGenerator()); + registerGenerator(GenTypeConst.MAPPER, new MapperGenerator()); + registerGenerator(GenTypeConst.SERVICE, new ServiceGenerator()); + registerGenerator(GenTypeConst.SERVICE_IMPL, new ServiceImplGenerator()); + registerGenerator(GenTypeConst.CONTROLLER, new ControllerGenerator()); + registerGenerator(GenTypeConst.TABLE_DEF, new TableDefGenerator()); + registerGenerator(GenTypeConst.MAPPER_XML, new MapperXmlGenerator()); + registerGenerator(GenTypeConst.PACKAGE_INFO, new PackageInfoGenerator()); + } + + public static IGenerator getGenerator(String genType) { + return generators.get(genType); } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/IGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/IGenerator.java index 5f9e57fe..a291e524 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/IGenerator.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/IGenerator.java @@ -1,23 +1,51 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.generator; import com.mybatisflex.codegen.config.GlobalConfig; import com.mybatisflex.codegen.entity.Table; +/** + * 文件生成器接口。 + * + * @author Michael Yang + * @author 王帅 + */ public interface IGenerator { - void generate(Table table, GlobalConfig globalConfig); + + /** + * 获取模板文件位置。 + * + * @return 路径 + */ + String getTemplatePath(); + + /** + * 设置模板文件位置。 + * + * @param templatePath + */ + void setTemplatePath(String templatePath); + + /** + * 根据模板生成文件。 + * + * @param table 表内容 + * @param globalConfig 全局配置 + */ + void generate(Table table, GlobalConfig globalConfig); + } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ControllerGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ControllerGenerator.java index c1d77333..6ca56b6d 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ControllerGenerator.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ControllerGenerator.java @@ -1,21 +1,24 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.generator.impl; +import com.mybatisflex.codegen.config.ControllerConfig; import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.config.PackageConfig; +import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -29,12 +32,12 @@ import java.util.Map; * @author 王帅 * @since 2023-05-14 */ -@SuppressWarnings("unused") public class ControllerGenerator implements IGenerator { - private String templatePath = "/templates/enjoy/controller.tpl"; + private String templatePath; public ControllerGenerator() { + this(TemplateConst.CONTROLLER); } public ControllerGenerator(String templatePath) { @@ -47,21 +50,35 @@ public class ControllerGenerator implements IGenerator { if (!globalConfig.isControllerGenerateEnable()) { return; } - - String controllerPackagePath = globalConfig.getControllerPackage().replace(".", "/"); - File controllerJavaFile = new File(globalConfig.getSourceDir(), controllerPackagePath + "/" + + + PackageConfig packageConfig = globalConfig.getPackageConfig(); + ControllerConfig controllerConfig = globalConfig.getControllerConfig(); + + String controllerPackagePath = packageConfig.getControllerPackage().replace(".", "/"); + File controllerJavaFile = new File(packageConfig.getSourceDir(), controllerPackagePath + "/" + table.buildControllerClassName() + ".java"); - if (controllerJavaFile.exists() && !globalConfig.isControllerOverwriteEnable()) { + if (controllerJavaFile.exists() && controllerConfig.isOverwriteEnable()) { return; } - Map params = new HashMap<>(2); + Map params = new HashMap<>(4); params.put("table", table); - params.put("globalConfig", globalConfig); + params.put("packageConfig", packageConfig); + params.put("controllerConfig", controllerConfig); + params.put("javadocConfig", globalConfig.getJavadocConfig()); - globalConfig.getTemplateEngine().generate(params, templatePath, controllerJavaFile); + globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, controllerJavaFile); } + + public String getTemplatePath() { + return templatePath; + } + + public void setTemplatePath(String templatePath) { + this.templatePath = templatePath; + } + } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/EntityGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/EntityGenerator.java index 48779059..71ab102a 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/EntityGenerator.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/EntityGenerator.java @@ -1,21 +1,24 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.generator.impl; +import com.mybatisflex.codegen.config.EntityConfig; import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.config.PackageConfig; +import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -23,11 +26,18 @@ import java.io.File; import java.util.HashMap; import java.util.Map; +/** + * Entity 生成器。 + * + * @author Michael Yang + * @author 王帅 + */ public class EntityGenerator implements IGenerator { - private String templatePath = "/templates/enjoy/entity.tpl"; + private String templatePath; public EntityGenerator() { + this(TemplateConst.ENTITY); } public EntityGenerator(String templatePath) { @@ -37,14 +47,38 @@ public class EntityGenerator implements IGenerator { @Override public void generate(Table table, GlobalConfig globalConfig) { - String entityPackagePath = globalConfig.getEntityPackage().replace(".", "/"); - File entityJavaFile = new File(globalConfig.getSourceDir(), entityPackagePath + "/" + + if (!globalConfig.isEntityGenerateEnable()) { + return; + } + + PackageConfig packageConfig = globalConfig.getPackageConfig(); + EntityConfig entityConfig = globalConfig.getEntityConfig(); + + String entityPackagePath = packageConfig.getEntityPackage().replace(".", "/"); + File entityJavaFile = new File(packageConfig.getSourceDir(), entityPackagePath + "/" + table.buildEntityClassName() + ".java"); - Map params = new HashMap<>(); - params.put("table", table); - params.put("globalConfig", globalConfig); - globalConfig.getTemplateEngine().generate(params, templatePath, entityJavaFile); + if (entityJavaFile.exists() && !entityConfig.isOverwriteEnable()) { + return; + } + + + Map params = new HashMap<>(4); + params.put("table", table); + params.put("entityConfig", entityConfig); + params.put("packageConfig", packageConfig); + params.put("javadocConfig", globalConfig.getJavadocConfig()); + + globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, entityJavaFile); } + + public String getTemplatePath() { + return templatePath; + } + + public void setTemplatePath(String templatePath) { + this.templatePath = templatePath; + } + } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperGenerator.java index 1f9a37fa..9e89fe9d 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperGenerator.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperGenerator.java @@ -1,21 +1,24 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.config.MapperConfig; +import com.mybatisflex.codegen.config.PackageConfig; +import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -23,11 +26,18 @@ import java.io.File; import java.util.HashMap; import java.util.Map; +/** + * Mapper 生成器。 + * + * @author Michael Yang + * @author 王帅 + */ public class MapperGenerator implements IGenerator { - private String templatePath = "/templates/enjoy/mapper.tpl"; + private String templatePath; public MapperGenerator() { + this(TemplateConst.MAPPER); } public MapperGenerator(String templatePath) { @@ -40,21 +50,35 @@ public class MapperGenerator implements IGenerator { if (!globalConfig.isMapperGenerateEnable()) { return; } + + PackageConfig packageConfig = globalConfig.getPackageConfig(); + MapperConfig mapperConfig = globalConfig.getMapperConfig(); - String mapperPackagePath = globalConfig.getMapperPackage().replace(".", "/"); - File mapperJavaFile = new File(globalConfig.getSourceDir(), mapperPackagePath + "/" + + String mapperPackagePath = packageConfig.getMapperPackage().replace(".", "/"); + File mapperJavaFile = new File(packageConfig.getSourceDir(), mapperPackagePath + "/" + table.buildMapperClassName() + ".java"); - if (mapperJavaFile.exists() && !globalConfig.isMapperOverwriteEnable()) { - return;//ignore + if (mapperJavaFile.exists() && !mapperConfig.isOverwriteEnable()) { + return; } - Map params = new HashMap<>(); + Map params = new HashMap<>(4); params.put("table", table); - params.put("globalConfig", globalConfig); + params.put("mapperConfig", mapperConfig); + params.put("packageConfig", packageConfig); + params.put("javadocConfig", globalConfig.getJavadocConfig()); - globalConfig.getTemplateEngine().generate(params, templatePath, mapperJavaFile); + globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, mapperJavaFile); } + + public String getTemplatePath() { + return templatePath; + } + + public void setTemplatePath(String templatePath) { + this.templatePath = templatePath; + } + } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperXmlGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperXmlGenerator.java new file mode 100644 index 00000000..c5667d13 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperXmlGenerator.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.codegen.generator.impl; + +import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.config.MapperXmlConfig; +import com.mybatisflex.codegen.config.PackageConfig; +import com.mybatisflex.codegen.constant.TemplateConst; +import com.mybatisflex.codegen.entity.Table; +import com.mybatisflex.codegen.generator.IGenerator; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +/** + * MapperXml 生成器。 + * + * @author 王帅 + * @since 2023-05-17 + */ +public class MapperXmlGenerator implements IGenerator { + + private String templatePath; + + public MapperXmlGenerator() { + this(TemplateConst.MAPPER_XML); + } + + public MapperXmlGenerator(String templatePath) { + this.templatePath = templatePath; + } + + @Override + public void generate(Table table, GlobalConfig globalConfig) { + + if (!globalConfig.isMapperXmlGenerateEnable()) { + return; + } + + PackageConfig packageConfig = globalConfig.getPackageConfig(); + MapperXmlConfig mapperXmlConfig = globalConfig.getMapperXmlConfig(); + + File mapperXmlFile = new File(packageConfig.getMapperXmlPath() + "/" + + table.buildMapperXmlFileName() + ".xml"); + + + if (mapperXmlFile.exists() && !mapperXmlConfig.isOverwriteEnable()) { + return; + } + + + Map params = new HashMap<>(2); + params.put("table", table); + params.put("packageConfig", packageConfig); + + globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, mapperXmlFile); + } + + public String getTemplatePath() { + return templatePath; + } + + public void setTemplatePath(String templatePath) { + this.templatePath = templatePath; + } + +} diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/PackageInfoGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/PackageInfoGenerator.java new file mode 100644 index 00000000..002ffb7d --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/PackageInfoGenerator.java @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.codegen.generator.impl; + +import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.config.JavadocConfig; +import com.mybatisflex.codegen.config.PackageConfig; +import com.mybatisflex.codegen.constant.TemplateConst; +import com.mybatisflex.codegen.entity.Table; +import com.mybatisflex.codegen.generator.IGenerator; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * package-info.java 生成器。 + * + * @author 王帅 + * @since 2023-05-17 + */ +public class PackageInfoGenerator implements IGenerator { + + private String templatePath; + + public PackageInfoGenerator() { + this(TemplateConst.PACKAGE_INFO); + } + + public PackageInfoGenerator(String templatePath) { + this.templatePath = templatePath; + } + + @Override + public void generate(Table table, GlobalConfig globalConfig) { + + if (!globalConfig.isPackageInfoGenerateEnable()) { + return; + } + + JavadocConfig javadocConfig = globalConfig.getJavadocConfig(); + PackageConfig packageConfig = globalConfig.getPackageConfig(); + + String sourceDir = packageConfig.getSourceDir(); + + List dataList = new ArrayList<>(); + + if (globalConfig.isEntityGenerateEnable()) { + dataList.add(new Data(sourceDir, packageConfig.getEntityPackage(), javadocConfig.getEntityPackage())); + } + if (globalConfig.isMapperGenerateEnable()) { + dataList.add(new Data(sourceDir, packageConfig.getMapperPackage(), javadocConfig.getMapperPackage())); + } + if (globalConfig.isServiceGenerateEnable()) { + dataList.add(new Data(sourceDir, packageConfig.getServicePackage(), javadocConfig.getServicePackage())); + } + if (globalConfig.isServiceImplGenerateEnable()) { + dataList.add(new Data(sourceDir, packageConfig.getServiceImplPackage(), javadocConfig.getServiceImplPackage())); + } + if (globalConfig.isControllerGenerateEnable()) { + dataList.add(new Data(sourceDir, packageConfig.getControllerPackage(), javadocConfig.getControllerPackage())); + } + if (globalConfig.isTableDefGenerateEnable()) { + dataList.add(new Data(sourceDir, packageConfig.getTableDefPackage(), javadocConfig.getTableDefPackage())); + } + + dataList.forEach(data -> { + Map params = new HashMap<>(3); + params.put("packageName", data.packageName); + params.put("packageComment", data.packageComment); + params.put("javadocConfig", javadocConfig); + globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, data.filePath); + }); + } + + public String getTemplatePath() { + return templatePath; + } + + public void setTemplatePath(String templatePath) { + this.templatePath = templatePath; + } + + private static class Data { + + String packageName; + String packageComment; + File filePath; + + Data(String sourceDir, String packageName, String packageComment) { + this.packageName = packageName; + this.packageComment = packageComment; + this.filePath = getFilePath(sourceDir, packageName); + } + + File getFilePath(String sourceDir, String packageName) { + return new File(sourceDir, packageName.replace(".", "/") + "/package-info.java"); + } + + } + +} diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceGenerator.java index d9f75338..bced18f0 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceGenerator.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceGenerator.java @@ -1,21 +1,24 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.config.PackageConfig; +import com.mybatisflex.codegen.config.ServiceConfig; +import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -29,12 +32,12 @@ import java.util.Map; * @author 王帅 * @since 2023-05-14 */ -@SuppressWarnings("unused") public class ServiceGenerator implements IGenerator { - private String templatePath = "/templates/enjoy/service.tpl"; + private String templatePath; public ServiceGenerator() { + this(TemplateConst.SERVICE); } public ServiceGenerator(String templatePath) { @@ -47,21 +50,35 @@ public class ServiceGenerator implements IGenerator { if (!globalConfig.isServiceGenerateEnable()) { return; } + + PackageConfig packageConfig = globalConfig.getPackageConfig(); + ServiceConfig serviceConfig = globalConfig.getServiceConfig(); - String servicePackagePath = globalConfig.getServicePackage().replace(".", "/"); - File serviceJavaFile = new File(globalConfig.getSourceDir(), servicePackagePath + "/" + + String servicePackagePath = packageConfig.getServicePackage().replace(".", "/"); + File serviceJavaFile = new File(packageConfig.getSourceDir(), servicePackagePath + "/" + table.buildServiceClassName() + ".java"); - if (serviceJavaFile.exists() && !globalConfig.isServiceOverwriteEnable()) { + if (serviceJavaFile.exists() && !serviceConfig.isOverwriteEnable()) { return; } - Map params = new HashMap<>(2); + Map params = new HashMap<>(4); params.put("table", table); - params.put("globalConfig", globalConfig); + params.put("serviceConfig", serviceConfig); + params.put("packageConfig", packageConfig); + params.put("javadocConfig", globalConfig.getJavadocConfig()); - globalConfig.getTemplateEngine().generate(params, templatePath, serviceJavaFile); + globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, serviceJavaFile); } + + public String getTemplatePath() { + return templatePath; + } + + public void setTemplatePath(String templatePath) { + this.templatePath = templatePath; + } + } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceImplGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceImplGenerator.java index 099364fa..8c354b79 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceImplGenerator.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceImplGenerator.java @@ -1,21 +1,24 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.config.PackageConfig; +import com.mybatisflex.codegen.config.ServiceImplConfig; +import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -29,12 +32,12 @@ import java.util.Map; * @author 王帅 * @since 2023-05-14 */ -@SuppressWarnings("unused") public class ServiceImplGenerator implements IGenerator { - private String templatePath = "/templates/enjoy/serviceImpl.tpl"; + private String templatePath; public ServiceImplGenerator() { + this(TemplateConst.SERVICE_IMPL); } public ServiceImplGenerator(String templatePath) { @@ -47,21 +50,35 @@ public class ServiceImplGenerator implements IGenerator { if (!globalConfig.isServiceImplGenerateEnable()) { return; } + + PackageConfig packageConfig = globalConfig.getPackageConfig(); + ServiceImplConfig serviceImplConfig = globalConfig.getServiceImplConfig(); - String serviceImplPackagePath = globalConfig.getServiceImplPackage().replace(".", "/"); - File serviceImplJavaFile = new File(globalConfig.getSourceDir(), serviceImplPackagePath + "/" + + String serviceImplPackagePath = packageConfig.getServiceImplPackage().replace(".", "/"); + File serviceImplJavaFile = new File(packageConfig.getSourceDir(), serviceImplPackagePath + "/" + table.buildServiceImplClassName() + ".java"); - if (serviceImplJavaFile.exists() && !globalConfig.isServiceImplOverwriteEnable()) { + if (serviceImplJavaFile.exists() && !serviceImplConfig.isOverwriteEnable()) { return; } - Map params = new HashMap<>(2); + Map params = new HashMap<>(4); params.put("table", table); - params.put("globalConfig", globalConfig); + params.put("packageConfig", packageConfig); + params.put("serviceImplConfig", serviceImplConfig); + params.put("javadocConfig", globalConfig.getJavadocConfig()); - globalConfig.getTemplateEngine().generate(params, templatePath, serviceImplJavaFile); + globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, serviceImplJavaFile); } + + public String getTemplatePath() { + return templatePath; + } + + public void setTemplatePath(String templatePath) { + this.templatePath = templatePath; + } + } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/TableDefGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/TableDefGenerator.java index 4e08bf3f..9faafeaa 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/TableDefGenerator.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/TableDefGenerator.java @@ -1,21 +1,24 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.config.PackageConfig; +import com.mybatisflex.codegen.config.TableDefConfig; +import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -23,11 +26,18 @@ import java.io.File; import java.util.HashMap; import java.util.Map; +/** + * TableDef 生成器。 + * + * @author Michael Yang + * @author 王帅 + */ public class TableDefGenerator implements IGenerator { - private String templatePath = "/templates/enjoy/tableDef.tpl"; + private String templatePath; public TableDefGenerator() { + this(TemplateConst.TABLE_DEF); } public TableDefGenerator(String templatePath) { @@ -41,15 +51,34 @@ public class TableDefGenerator implements IGenerator { return; } - String tableDefPackagePath = globalConfig.getTableDefPackage().replace(".", "/"); - File tableDefJavaFile = new File(globalConfig.getSourceDir(), tableDefPackagePath + "/" + + PackageConfig packageConfig = globalConfig.getPackageConfig(); + TableDefConfig tableDefConfig = globalConfig.getTableDefConfig(); + + String tableDefPackagePath = packageConfig.getTableDefPackage().replace(".", "/"); + File tableDefJavaFile = new File(packageConfig.getSourceDir(), tableDefPackagePath + "/" + table.buildTableDefClassName() + ".java"); - Map params = new HashMap<>(); - params.put("table", table); - params.put("globalConfig", globalConfig); + if (tableDefJavaFile.exists() && !tableDefConfig.isOverwriteEnable()) { + return; + } - globalConfig.getTemplateEngine().generate(params, templatePath, tableDefJavaFile); + + Map params = new HashMap<>(4); + params.put("table", table); + params.put("packageConfig", packageConfig); + params.put("tableDefConfig", tableDefConfig); + params.put("javadocConfig", globalConfig.getJavadocConfig()); + + globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, tableDefJavaFile); } + + public String getTemplatePath() { + return templatePath; + } + + public void setTemplatePath(String templatePath) { + this.templatePath = templatePath; + } + } diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/EnjoyTemplate.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/impl/EnjoyTemplate.java similarity index 67% rename from mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/EnjoyTemplate.java rename to mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/impl/EnjoyTemplate.java index d1e92323..3ea8fbe4 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/EnjoyTemplate.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/impl/EnjoyTemplate.java @@ -13,10 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mybatisflex.codegen.template; +package com.mybatisflex.codegen.template.impl; import com.jfinal.template.Engine; import com.jfinal.template.expr.ast.FieldGetters; +import com.jfinal.template.source.ClassPathSource; +import com.jfinal.template.source.FileSource; +import com.jfinal.template.source.ISource; +import com.jfinal.template.source.ISourceFactory; +import com.mybatisflex.codegen.template.ITemplate; import com.mybatisflex.core.util.StringUtil; import java.io.File; @@ -29,8 +34,8 @@ public class EnjoyTemplate implements ITemplate { public EnjoyTemplate() { engine = Engine.create("mybatis-flex", engine -> { - engine.setToClassPathSourceFactory(); engine.addSharedMethod(StringUtil.class); + engine.setSourceFactory(new FileAndClassPathSourceFactory()); }); // 以下配置将支持 user.girl 表达式去调用 user 对象的 boolean isGirl() 方法 Engine.addFieldGetterToFirst(new FieldGetters.IsMethodFieldGetter()); @@ -48,4 +53,18 @@ public class EnjoyTemplate implements ITemplate { e.printStackTrace(); } } + + public static class FileAndClassPathSourceFactory implements ISourceFactory { + + @Override + public ISource getSource(String baseTemplatePath, String fileName, String encoding) { + // 先从文件寻找资源,找不到再从类路径寻找资源 + if (new File(fileName).exists()) { + return new FileSource(baseTemplatePath, fileName, encoding); + } + return new ClassPathSource(baseTemplatePath, fileName, encoding); + } + + } + } diff --git a/mybatis-flex-codegen/src/main/resources/templates/enjoy/controller.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/controller.tpl index b23c57e9..27d43b10 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/controller.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/controller.tpl @@ -1,27 +1,112 @@ -package #(globalConfig.controllerPackage); +#set(tableComment = table.getComment()) +#set(entityClassName = table.buildEntityClassName()) +#set(entityVarName = firstCharToLowerCase(entityClassName)) +#set(serviceVarName = firstCharToLowerCase(table.buildServiceClassName())) +package #(packageConfig.controllerPackage); +import com.mybatisflex.core.paginate.Page; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; -#if(globalConfig.restStyleController) +import org.springframework.beans.factory.annotation.Autowired; +import #(packageConfig.entityPackage).#(entityClassName); +import #(packageConfig.servicePackage).#(table.buildServiceClassName()); +#if(controllerConfig.restStyle) import org.springframework.web.bind.annotation.RestController; #else import org.springframework.stereotype.Controller; #end -#if(globalConfig.controllerSupperClass) -import #(table.buildControllerImport()) +#if(controllerConfig.supperClass) +import #(controllerConfig.buildSuperClassImport()); #end -#if(globalConfig.restStyleController) +import java.io.Serializable; +import java.util.List; + +/** + * #(tableComment) 控制层。 + * + * @author #(javadocConfig.getAuthor()) + * @since #(javadocConfig.getSince()) + */ +#if(controllerConfig.restStyle) @RestController #else @Controller #end -@RequestMapping("/#(table.getEntityJavaFileName())") -#if(globalConfig.controllerSupperClass) -public class #(table.table.buildControllerClassName()) extends #(table.buildControllerName()) { +@RequestMapping("/#(firstCharToLowerCase(entityClassName))") +public class #(table.buildControllerClassName()) #if(controllerConfig.supperClass)extends #(controllerConfig.buildSuperClassName()) #end { -} -#else -public class #(table.buildControllerClassName()) { + @Autowired + private #(table.buildServiceClassName()) #(serviceVarName); -} -#end \ No newline at end of file + /** + * 添加#(tableComment)。 + * + * @param #(entityVarName) #(tableComment) + * @return {@code true} 添加成功,{@code false} 添加失败 + */ + @PostMapping("save") + public boolean save(@RequestBody #(entityClassName) #(entityVarName)) { + return #(serviceVarName).save(#(entityVarName)); + } + + /** + * 根据主键删除#(tableComment)。 + * + * @param id 主键 + * @return {@code true} 删除成功,{@code false} 删除失败 + */ + @DeleteMapping("remove/{id}") + public boolean remove(@PathVariable Serializable id) { + return #(serviceVarName).removeById(id); + } + + /** + * 根据主键更新#(tableComment)。 + * + * @param #(entityVarName) #(tableComment) + * @return {@code true} 更新成功,{@code false} 更新失败 + */ + @PutMapping("update") + public boolean update(@RequestBody #(entityClassName) #(entityVarName)) { + return #(serviceVarName).updateById(#(entityVarName)); + } + + /** + * 查询所有#(tableComment)。 + * + * @return 所有数据 + */ + @GetMapping("list") + public List<#(entityClassName)> list() { + return #(serviceVarName).list(); + } + + /** + * 根据#(tableComment)主键获取详细信息。 + * + * @param id #(tableComment)主键 + * @return #(tableComment)详情 + */ + @GetMapping("getInfo/{id}") + public #(entityClassName) getInfo(@PathVariable Serializable id) { + return #(serviceVarName).getById(id); + } + + /** + * 分页查询#(tableComment)。 + * + * @param page 分页对象 + * @return 分页对象 + */ + @GetMapping("page") + public Page<#(entityClassName)> page(Page<#(entityClassName)> page) { + return #(serviceVarName).page(page); + } + +} \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl index ef2cd733..14fbd65b 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl @@ -1,19 +1,24 @@ -package #(globalConfig.entityPackage); +package #(packageConfig.entityPackage); #for(importClass:table.buildImports()) import #(importClass); #end -#(table.buildRemarks()) +/** + * #(table.getComment()) 实体类。 + * + * @author #(javadocConfig.getAuthor()) + * @since #(javadocConfig.getSince()) + */ #(table.buildTableAnnotation()) public class #(table.buildEntityClassName())#(table.buildExtends())#(table.buildImplements()) { #for(column: table.columns) - #(column.buildRemarks()) + #(column.buildComment()) #(column.buildAnnotations())private #(column.propertySimpleType) #(column.property); #end - #if(!globalConfig.isEntityWithLombok()) + #if(!entityConfig.isWithLombok()) #for(column: table.columns) public #(column.propertySimpleType) #(column.getterMethod())() { return #(column.property); diff --git a/mybatis-flex-codegen/src/main/resources/templates/enjoy/mapper.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/mapper.tpl index 25c8e151..24040c98 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/mapper.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/mapper.tpl @@ -1,8 +1,14 @@ -package #(globalConfig.mapperPackage); +package #(packageConfig.mapperPackage); -import #(table.buildMapperImport()); -import #(globalConfig.entityPackage).#(table.buildEntityClassName()); +import #(mapperConfig.buildSuperClassImport()); +import #(packageConfig.entityPackage).#(table.buildEntityClassName()); -public interface #(table.buildMapperClassName()) extends #(table.buildMapperName())<#(table.buildEntityClassName())> { +/** + * #(table.getComment()) 映射层。 + * + * @author #(javadocConfig.getAuthor()) + * @since #(javadocConfig.getSince()) + */ +public interface #(table.buildMapperClassName()) extends #(mapperConfig.buildSuperClassName())<#(table.buildEntityClassName())> { } diff --git a/mybatis-flex-codegen/src/main/resources/templates/enjoy/mapperXml.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/mapperXml.tpl new file mode 100644 index 00000000..cf593d5a --- /dev/null +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/mapperXml.tpl @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/resources/templates/enjoy/package-info.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/package-info.tpl new file mode 100644 index 00000000..a86e5048 --- /dev/null +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/package-info.tpl @@ -0,0 +1,7 @@ +/** + * #(packageComment) + * + * @author #(javadocConfig.getAuthor()) + * @since #(javadocConfig.getSince()) + */ + package #(packageName); \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/resources/templates/enjoy/service.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/service.tpl index 7bd3ac19..79d3909a 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/service.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/service.tpl @@ -1,8 +1,14 @@ -package #(globalConfig.servicePackage); +package #(packageConfig.servicePackage); -import #(table.buildServiceImport()); -import #(globalConfig.entityPackage).#(table.buildEntityClassName()); +import #(serviceConfig.buildSuperClassImport()); +import #(packageConfig.entityPackage).#(table.buildEntityClassName()); -public interface #(table.buildServiceClassName()) extends #(table.buildServiceName())<#(table.buildEntityClassName())> { +/** + * #(table.getComment()) 服务层。 + * + * @author #(javadocConfig.getAuthor()) + * @since #(javadocConfig.getSince()) + */ +public interface #(table.buildServiceClassName()) extends #(serviceConfig.buildSuperClassName())<#(table.buildEntityClassName())> { } \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/resources/templates/enjoy/serviceImpl.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/serviceImpl.tpl index 95722a2e..c5b45e48 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/serviceImpl.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/serviceImpl.tpl @@ -1,12 +1,18 @@ -package #(globalConfig.serviceImplPackage); +package #(packageConfig.serviceImplPackage); -import #(table.buildServiceImplImport()); -import #(globalConfig.entityPackage).#(table.buildEntityClassName()); -import #(globalConfig.mapperPackage).#(table.buildMapperClassName()); -import #(globalConfig.servicePackage).#(table.buildServiceClassName()); +import #(serviceImplConfig.buildSuperClassImport()); +import #(packageConfig.entityPackage).#(table.buildEntityClassName()); +import #(packageConfig.mapperPackage).#(table.buildMapperClassName()); +import #(packageConfig.servicePackage).#(table.buildServiceClassName()); import org.springframework.stereotype.Service; +/** + * #(table.getComment()) 服务层实现。 + * + * @author #(javadocConfig.getAuthor()) + * @since #(javadocConfig.getSince()) + */ @Service -public class #(table.buildServiceImplClassName()) extends #(table.buildServiceImplName())<#(table.buildMapperClassName()), #(table.buildEntityClassName())> implements #(table.buildServiceClassName()) { +public class #(table.buildServiceImplClassName()) extends #(serviceImplConfig.buildSuperClassName())<#(table.buildMapperClassName()), #(table.buildEntityClassName())> implements #(table.buildServiceClassName()) { } \ No newline at end of file diff --git a/mybatis-flex-codegen/src/main/resources/templates/enjoy/tableDef.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/tableDef.tpl index 25e9c5da..513eaca2 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/tableDef.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/tableDef.tpl @@ -1,9 +1,14 @@ -package #(globalConfig.entityPackage).tables; +package #(packageConfig.tableDefPackage); import com.mybatisflex.core.query.QueryColumn; import com.mybatisflex.core.table.TableDef; - +/** + * #(table.getComment()) 表定义层。 + * + * @author #(javadocConfig.getAuthor()) + * @since #(javadocConfig.getSince()) + */ public class #(table.buildTableDefClassName()) extends TableDef { public static final #(table.buildTableDefClassName()) #(table.name) = new #(table.buildTableDefClassName())("#(table.name)"); 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 a808a30c..2efebe31 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 @@ -1,85 +1,79 @@ -/** - * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ + package com.mybatisflex.codegen.test; import com.mybatisflex.codegen.Generator; -import com.mybatisflex.codegen.config.ColumnConfig; import com.mybatisflex.codegen.config.GlobalConfig; -import com.mybatisflex.codegen.config.TableConfig; import com.zaxxer.hikari.HikariDataSource; +import org.junit.Test; + +import java.util.function.UnaryOperator; public class GeneratorTest { - // @Test - public void testGenerator() { + //@Test + public void testCodeGen1() { //配置数据源 HikariDataSource dataSource = new HikariDataSource(); - dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/jbootadmin?characterEncoding=utf-8"); - // dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/hh-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowMultiQueries=true"); + dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8"); dataSource.setUsername("root"); - dataSource.setPassword("123456"); - - // JdbcTypeMapping.registerMapping(BigInteger.class, Long.class); - // JdbcTypeMapping.registerMapping(Integer.class, Long.class); + dataSource.setPassword("12345678"); GlobalConfig globalConfig = new GlobalConfig(); - globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java"); - // globalConfig.setTablePrefix("tb_"); - // globalConfig.setEntityWithLombok(true); + //用户信息表,用于存放用户信息。 -> 用户信息 + UnaryOperator tableFormat = (e) -> e.split(",")[0].replace("表", ""); + + //设置注解生成配置 + globalConfig.setAuthor("Michael Yang"); + globalConfig.setTableCommentFormat(tableFormat); + + //设置生成文件目录和根包 + globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java"); + globalConfig.setMapperXmlPath(System.getProperty("user.dir") + "/src/test/java/resources/mapper"); + globalConfig.setBasePackage("com.test"); + + //设置表前缀和只生成哪些表 + globalConfig.setTablePrefix("sys_"); + globalConfig.setGenerateTable("sys_user"); + + //设置模板路径 + globalConfig.setEntityTemplatePath("D:\\Documents\\配置文件\\entity.tpl"); + + //配置生成 entity + globalConfig.setEntityGenerateEnable(true); + globalConfig.setEntityWithLombok(true); globalConfig.setEntitySupperClass(BaseEntity.class); - //设置只生成哪些表 - globalConfig.addGenerateTable("account", "account_session"); - - //设置 entity 的包名 - globalConfig.setEntityPackage("com.test.entity"); - globalConfig.setEntityClassPrefix("My"); - globalConfig.setEntityClassSuffix("Entity"); - - //设置 entity 的包名 - globalConfig.setTableDefGenerateEnable(true); - globalConfig.setTableDefPackage("com.test.entity.tables"); - globalConfig.setTableDefClassPrefix("My"); - globalConfig.setTableDefClassSuffix("TableDef"); - - //是否生成 mapper 类,默认为 false + //配置生成 mapper globalConfig.setMapperGenerateEnable(true); - globalConfig.setMapperClassPrefix("Flex"); - globalConfig.setMapperClassSuffix("Dao"); - - //设置 mapper 类的包名 - globalConfig.setMapperPackage("com.test.mapper"); - globalConfig.setMapperSupperClass(MyBaseMapper.class); - - - TableConfig tableConfig = new TableConfig(); - tableConfig.setTableName("account"); - tableConfig.setUpdateListenerClass(MyUpdateListener.class); - globalConfig.addTableConfig(tableConfig); - - - //可以单独配置某个列 - ColumnConfig columnConfig = new ColumnConfig(); - columnConfig.setColumnName("tenant_id"); - columnConfig.setLarge(true); - columnConfig.setVersion(true); - globalConfig.addColumnConfig("account", columnConfig); - + //配置生成 service + globalConfig.setServiceGenerateEnable(true); + //配置生成 serviceImpl + globalConfig.setServiceImplGenerateEnable(true); + //配置生成 controller + globalConfig.setControllerGenerateEnable(true); + //配置生成 tableDef + globalConfig.setTableDefGenerateEnable(true); + //配置生成 mapperXml + globalConfig.setMapperXmlGenerateEnable(true); + //配置生成 package-info.java + globalConfig.setPackageInfoGenerateEnable(true); //通过 datasource 和 globalConfig 创建代码生成器 Generator generator = new Generator(dataSource, globalConfig); @@ -88,41 +82,64 @@ public class GeneratorTest { generator.generate(); } -// @Test - public void testCodeGen() { - // 配置数据源 + @Test + public void testCodeGen2() { + //配置数据源 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); + //用户信息表,用于存放用户信息。 -> 用户信息 + UnaryOperator tableFormat = (e) -> e.split(",")[0].replace("表", ""); - // 设置只生成哪些表 - globalConfig.addGenerateTable("sys_user"); + //设置注解生成配置 + globalConfig.getJavadocConfig() + .setAuthor("王帅") + .setTableCommentFormat(tableFormat); - // 设置 entity 的包名 - globalConfig.setTableDefGenerateEnable(true); + //设置生成文件目录和根包 + globalConfig.getPackageConfig() + .setSourceDir(System.getProperty("user.dir") + "/src/test/java") + .setMapperXmlPath(System.getProperty("user.dir") + "/src/test/java/resources/mapper") + .setBasePackage("com.test"); - // 是否生成 mapper 类,默认为 false - globalConfig.setMapperGenerateEnable(true); - // 是否生成 service 类,默认为 false - globalConfig.setServiceGenerateEnable(true); - // 是否生成 serviceImpl 类,默认为 false - globalConfig.setServiceImplGenerateEnable(true); - // 是否生成 controller 类,默认为 false - globalConfig.setControllerGenerateEnable(true); + //设置表前缀和只生成哪些表 + globalConfig.getStrategyConfig() + .setTablePrefix("sys_") + .setGenerateTable("sys_user"); - // 通过 datasource 和 globalConfig 创建代码生成器 + //设置模板路径 + globalConfig.getTemplateConfig() + .setEntity("D:\\Documents\\配置文件\\entity.tpl"); + + //配置生成 entity + globalConfig.enableEntity() + .setOverwriteEnable(true) + .setWithLombok(true) + .setSupperClass(BaseEntity.class); + + //配置生成 mapper + globalConfig.enableMapper(); + //配置生成 service + globalConfig.enableService(); + //配置生成 serviceImpl + globalConfig.enableServiceImpl(); + //配置生成 controller + globalConfig.enableController(); + //配置生成 tableDef + globalConfig.enableTableDef(); + //配置生成 mapperXml + globalConfig.enableMapperXml(); + //配置生成 package-info.java + globalConfig.enablePackageInfo(); + + //通过 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..b7a63d5f 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 @@ -1,17 +1,32 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.mybatisflex.codegen.test; 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 +39,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() + .setGenerateTable("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); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/FunctionQueryColumn.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/FunctionQueryColumn.java index e5a55c4e..d580fa2e 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/FunctionQueryColumn.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/FunctionQueryColumn.java @@ -24,7 +24,7 @@ import java.util.List; /** * 数据库 聚合函数,例如 count(id) ,max(account.age) 等等 */ -public class FunctionQueryColumn extends QueryColumn { +public class FunctionQueryColumn extends QueryColumn implements HasParamsColumn { protected String fnName; protected QueryColumn column; @@ -58,6 +58,14 @@ public class FunctionQueryColumn extends QueryColumn { this.column = column; } + @Override + public Object[] getParamValues() { + if (column instanceof HasParamsColumn){ + return ((HasParamsColumn) column).getParamValues(); + } + return WrapperUtil.NULL_PARA_ARRAY; + } + @Override public String toSelectSql(List queryTables, IDialect dialect) { String sql = column.toSelectSql(queryTables, dialect); @@ -78,4 +86,6 @@ public class FunctionQueryColumn extends QueryColumn { ", column=" + column + '}'; } + + }