From c37e96898e80c73ca05c6c4b4a3e403855fa1331 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Mon, 15 May 2023 19:42:14 +0800 Subject: [PATCH 01/28] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=94=9F=E6=88=90=E5=99=A8=EF=BC=8C=E4=BD=BF=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=88=86=E9=A1=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/codegen/Generator.java | 8 +- .../codegen/config/ControllerConfig.java | 59 ++ .../codegen/config/EntityConfig.java | 58 ++ .../codegen/config/GlobalConfig.java | 738 +++--------------- .../codegen/config/MapperConfig.java | 55 ++ .../codegen/config/PackageConfig.java | 106 +++ .../codegen/config/ServiceConfig.java | 55 ++ .../codegen/config/ServiceImplConfig.java | 55 ++ .../codegen/config/StrategyConfig.java | 173 ++++ .../codegen/config/TableDefConfig.java | 41 + .../com/mybatisflex/codegen/entity/Table.java | 97 +-- .../generator/impl/ControllerGenerator.java | 19 +- .../generator/impl/EntityGenerator.java | 34 +- .../generator/impl/MapperGenerator.java | 26 +- .../generator/impl/ServiceGenerator.java | 19 +- .../generator/impl/ServiceImplGenerator.java | 19 +- .../generator/impl/TableDefGenerator.java | 29 +- .../resources/templates/enjoy/controller.tpl | 14 +- .../main/resources/templates/enjoy/entity.tpl | 4 +- .../main/resources/templates/enjoy/mapper.tpl | 8 +- .../resources/templates/enjoy/service.tpl | 8 +- .../resources/templates/enjoy/serviceImpl.tpl | 12 +- .../resources/templates/enjoy/tableDef.tpl | 2 +- 23 files changed, 875 insertions(+), 764 deletions(-) create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ControllerConfig.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperConfig.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/PackageConfig.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceConfig.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceImplConfig.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableDefConfig.java 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..65af4dde 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,17 +84,18 @@ 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); @@ -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..38686ef4 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ControllerConfig.java @@ -0,0 +1,59 @@ +/** + * 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 lombok.Data; +import lombok.experimental.Accessors; + +/** + * 生成 Controller 的配置。 + * + * @author 王帅 + * @since 2023-05-15 + */ +@Data +@Accessors(chain = true) +public class ControllerConfig { + + /** + * Controller 类的前缀。 + */ + private String classPrefix = ""; + + /** + * Controller 类的后缀。 + */ + private String classSuffix = "Controller"; + + /** + * 自定义 Controller 的父类。 + */ + private Class supperClass; + + /** + * 生成 REST 风格的 Controller。 + */ + private boolean restStyle = true; + + public String buildSuperClassImport() { + return supperClass.getName(); + } + + public String buildSuperClassName() { + return supperClass.getSimpleName(); + } + +} \ 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..4cab2f59 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java @@ -0,0 +1,58 @@ +/** + * 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 lombok.Data; +import lombok.experimental.Accessors; + +import java.io.Serializable; + +/** + * 生成 Entity 的配置。 + * + * @author 王帅 + * @since 2023-05-15 + */ +@Data +@Accessors(chain = true) +public class EntityConfig { + + /** + * Entity 类的前缀。 + */ + private String classPrefix = ""; + + /** + * Entity 类的后缀。 + */ + private String classSuffix = ""; + + /** + * Entity 类的父类,可以自定义一些 BaseEntity 类。 + */ + private Class supperClass; + + /** + * Entity 默认实现的接口。 + */ + private Class[] implInterfaces = {Serializable.class}; + + /** + * Entity 是否使用 Lombok 注解。 + */ + private boolean withLombok; + +} \ 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 438b3de0..941e1adb 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 @@ -15,692 +15,154 @@ */ 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.spring.service.IService; -import com.mybatisflex.spring.service.impl.ServiceImpl; +import lombok.Getter; -import java.io.Serializable; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; -import java.util.Set; +/** + * 代码生成全局配置类。 + * + * @author 王帅 + * @since 2023-05-15 + */ +@Getter +@SuppressWarnings("unused") public class GlobalConfig { - //代码生成目录 - private String sourceDir; + // === 必须配置 === - //根包名 - private String basePackage = "com.mybatisflex"; + private final PackageConfig packageConfig; + private final StrategyConfig strategyConfig; - //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; - //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 tableDefGenerateEnable = false; + public GlobalConfig() { + this.packageConfig = new PackageConfig(); + this.strategyConfig = new StrategyConfig(); + } - //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; - - - public String getSourceDir() { - if (sourceDir == null || sourceDir.trim().length() == 0) { - return System.getProperty("user.dir") + "/src/main/java"; + public EntityConfig getEntityConfig() { + if (entityConfig == null) { + entityConfig = new EntityConfig(); } - return sourceDir; + return entityConfig; } - public void setSourceDir(String sourceDir) { - this.sourceDir = StringUtil.trimOrNull(sourceDir); - } - - public String getBasePackage() { - return basePackage; - } - - public void setBasePackage(String basePackage) { - this.basePackage = StringUtil.trimOrNull(basePackage); - } - - public String getEntityPackage() { - if (StringUtil.isBlank(entityPackage)) { - entityPackage = basePackage + ".entity"; + public MapperConfig getMapperConfig() { + if (mapperConfig == null) { + mapperConfig = new MapperConfig(); } - return entityPackage; + return mapperConfig; } - public void setEntityPackage(String entityPackage) { - this.entityPackage = StringUtil.trimOrNull(entityPackage); - } - - public String getEntityClassPrefix() { - if (StringUtil.isBlank(entityClassPrefix)) { - return ""; + public ServiceConfig getServiceConfig() { + if (serviceConfig == null) { + serviceConfig = new ServiceConfig(); } - return entityClassPrefix; + return serviceConfig; } - public void setEntityClassPrefix(String entityClassPrefix) { - this.entityClassPrefix = StringUtil.trimOrNull(entityClassPrefix); - } - - public String getEntityClassSuffix() { - if (StringUtil.isBlank(entityClassSuffix)) { - return ""; + public ServiceImplConfig getServiceImplConfig() { + if (serviceImplConfig == null) { + serviceImplConfig = new ServiceImplConfig(); } - return entityClassSuffix; + return serviceImplConfig; } - public void setEntityClassSuffix(String entityClassSuffix) { - this.entityClassSuffix = StringUtil.trimOrNull(entityClassSuffix); - } - - public Class getEntitySupperClass() { - return entitySupperClass; - } - - public void setEntitySupperClass(Class entitySupperClass) { - this.entitySupperClass = entitySupperClass; - } - - public Class[] getEntityInterfaces() { - return entityInterfaces; - } - - 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; - } - - public String getTableDefPackage() { - if (StringUtil.isBlank(tableDefPackage)) { - return getEntityPackage() + ".tables"; + public ControllerConfig getControllerConfig() { + if (controllerConfig == null) { + controllerConfig = new ControllerConfig(); } - return tableDefPackage; + return controllerConfig; } - public void setTableDefPackage(String tableDefPackage) { - this.tableDefPackage = StringUtil.trimOrNull(tableDefPackage); - } - - public String getTableDefClassPrefix() { - if (StringUtil.isBlank(tableDefClassPrefix)) { - return ""; + public TableDefConfig getTableDefConfig() { + if (tableDefConfig == null) { + tableDefConfig = new TableDefConfig(); } - return tableDefClassPrefix; + return tableDefConfig; } - public void setTableDefClassPrefix(String tableDefClassPrefix) { - this.tableDefClassPrefix = StringUtil.trimOrNull(tableDefClassPrefix); + public EntityConfig enableEntity() { + entityGenerateEnable = true; + return getEntityConfig(); } - public String getTableDefClassSuffix() { - return tableDefClassSuffix; + public MapperConfig enableMapper() { + mapperGenerateEnable = true; + return getMapperConfig(); } - public void setTableDefClassSuffix(String tableDefClassSuffix) { - this.tableDefClassSuffix = StringUtil.trimOrNull(tableDefClassSuffix); + public ServiceConfig enableService() { + serviceGenerateEnable = true; + return getServiceConfig(); } - public boolean isMapperGenerateEnable() { - return mapperGenerateEnable; + public ServiceImplConfig enableServiceImpl() { + serviceImplGenerateEnable = true; + return getServiceImplConfig(); } - public void setMapperGenerateEnable(boolean mapperGenerateEnable) { - this.mapperGenerateEnable = mapperGenerateEnable; + public ControllerConfig enableController() { + controllerGenerateEnable = true; + return getControllerConfig(); } - public boolean isMapperOverwriteEnable() { - return mapperOverwriteEnable; + public TableDefConfig enableTableDef() { + tableDefGenerateEnable = true; + return getTableDefConfig(); } - public void setMapperOverwriteEnable(boolean mapperOverwriteEnable) { - this.mapperOverwriteEnable = mapperOverwriteEnable; + public void disableEntity() { + entityGenerateEnable = false; } - public String getMapperClassPrefix() { - if (StringUtil.isBlank(mapperClassPrefix)) { - return ""; + public void disableMapper() { + mapperGenerateEnable = false; + } + + public void disableService() { + serviceGenerateEnable = false; + } + + public void disableServiceImpl() { + serviceImplGenerateEnable = false; + } + + public void disableController() { + controllerGenerateEnable = false; + } + + public void disableTableDef() { + tableDefGenerateEnable = false; + } + + public void addCustomConfig(String key, Object value) { + if (customConfig == null) { + customConfig = new HashMap<>(); } - return mapperClassPrefix; + customConfig.put(key, value); } - public void setMapperClassPrefix(String mapperClassPrefix) { - this.mapperClassPrefix = StringUtil.trimOrNull(mapperClassPrefix); - } - - public String getMapperClassSuffix() { - return mapperClassSuffix; - } - - 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); - } - - public Class getMapperSupperClass() { - return mapperSupperClass; - } - - public void setMapperSupperClass(Class mapperSupperClass) { - this.mapperSupperClass = mapperSupperClass; - } - - public boolean isServiceGenerateEnable() { - return serviceGenerateEnable; - } - - public void setServiceGenerateEnable(boolean serviceGenerateEnable) { - this.serviceGenerateEnable = serviceGenerateEnable; - } - - public boolean isServiceOverwriteEnable() { - return serviceOverwriteEnable; - } - - public void setServiceOverwriteEnable(boolean serviceOverwriteEnable) { - this.serviceOverwriteEnable = serviceOverwriteEnable; - } - - public String getServiceClassPrefix() { - if (StringUtil.isBlank(serviceClassPrefix)) { - return ""; - } - return serviceClassPrefix; - } - - public void setServiceClassPrefix(String serviceClassPrefix) { - this.serviceClassPrefix = StringUtil.trimOrNull(serviceClassPrefix); - } - - public String getServiceClassSuffix() { - return serviceClassSuffix; - } - - 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); - } - - public Class getServiceSupperClass() { - return serviceSupperClass; - } - - public void setServiceSupperClass(Class serviceSupperClass) { - this.serviceSupperClass = serviceSupperClass; - } - - public boolean isServiceImplGenerateEnable() { - return serviceImplGenerateEnable; - } - - public void setServiceImplGenerateEnable(boolean serviceImplGenerateEnable) { - this.serviceImplGenerateEnable = serviceImplGenerateEnable; - } - - public boolean isServiceImplOverwriteEnable() { - return serviceImplOverwriteEnable; - } - - public void setServiceImplOverwriteEnable(boolean serviceImplOverwriteEnable) { - this.serviceImplOverwriteEnable = serviceImplOverwriteEnable; - } - - public String getServiceImplClassPrefix() { - if (StringUtil.isBlank(serviceImplClassPrefix)) { - return ""; - } - return serviceImplClassPrefix; - } - - public void setServiceImplClassPrefix(String serviceImplClassPrefix) { - this.serviceImplClassPrefix = StringUtil.trimOrNull(serviceImplClassPrefix); - } - - public String getServiceImplClassSuffix() { - return serviceImplClassSuffix; - } - - 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); - } - - public Class getServiceImplSupperClass() { - return serviceImplSupperClass; - } - - public void setServiceImplSupperClass(Class serviceImplSupperClass) { - this.serviceImplSupperClass = serviceImplSupperClass; - } - - public boolean isControllerGenerateEnable() { - return controllerGenerateEnable; - } - - public void setControllerGenerateEnable(boolean controllerGenerateEnable) { - this.controllerGenerateEnable = controllerGenerateEnable; - } - - public boolean isControllerOverwriteEnable() { - return controllerOverwriteEnable; - } - - public void setControllerOverwriteEnable(boolean controllerOverwriteEnable) { - this.controllerOverwriteEnable = controllerOverwriteEnable; - } - - public String getControllerClassPrefix() { - if (StringUtil.isBlank(controllerClassPrefix)) { - return ""; - } - return controllerClassPrefix; - } - - public void setControllerClassPrefix(String controllerClassPrefix) { - this.controllerClassPrefix = StringUtil.trimOrNull(controllerClassPrefix); - } - - public String getControllerClassSuffix() { - return controllerClassSuffix; - } - - 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); - } - - public Class getControllerSupperClass() { - return controllerSupperClass; - } - - public void setControllerSupperClass(Class controllerSupperClass) { - this.controllerSupperClass = controllerSupperClass; - } - - public boolean isRestStyleController() { - return restStyleController; - } - - public void setRestStyleController(boolean restStyleController) { - this.restStyleController = restStyleController; - } - - public String getTablePrefix() { - return tablePrefix; - } - - public void setTablePrefix(String tablePrefix) { - this.tablePrefix = StringUtil.trimOrNull(tablePrefix); - } - - public String getLogicDeleteColumn() { - return logicDeleteColumn; - } - - public void setLogicDeleteColumn(String logicDeleteColumn) { - this.logicDeleteColumn = StringUtil.trimOrNull(logicDeleteColumn); - } - - public String getVersionColumn() { - return versionColumn; - } - - public void setVersionColumn(String versionColumn) { - this.versionColumn = StringUtil.trimOrNull(versionColumn); - } - - public Map getTableConfigMap() { - return tableConfigMap; - } - - public void setTableConfigMap(Map tableConfigMap) { - this.tableConfigMap = tableConfigMap; - } - - public void addTableConfig(TableConfig tableConfig) { - if (tableConfigMap == null) { - tableConfigMap = new HashMap<>(); - } - tableConfigMap.put(tableConfig.getTableName(), tableConfig); - } - - public TableConfig getTableConfig(String tableName) { - return tableConfigMap == null ? null : tableConfigMap.get(tableName); - } - - public Map getDefaultColumnConfigMap() { - return defaultColumnConfigMap; - } - - public void setDefaultColumnConfigMap(Map defaultColumnConfigMap) { - this.defaultColumnConfigMap = defaultColumnConfigMap; - } - - - public void addColumnConfig(ColumnConfig columnConfig) { - if (defaultColumnConfigMap == null) { - defaultColumnConfigMap = new HashMap<>(); - } - defaultColumnConfigMap.put(columnConfig.getColumnName(), columnConfig); - } - - 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); - } - - - 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; - } - - public boolean isGenerateForView() { - return generateForView; - } - - public void setGenerateForView(boolean generateForView) { - this.generateForView = generateForView; - } - - public Set getGenerateTables() { - return generateTables; - } - - 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/MapperConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperConfig.java new file mode 100644 index 00000000..6f6e8745 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperConfig.java @@ -0,0 +1,55 @@ +/** + * 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; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 生成 Mapper 的配置。 + * + * @author 王帅 + * @since 2023-05-15 + */ +@Data +@Accessors(chain = true) +public class MapperConfig { + + /** + * Mapper 类的前缀。 + */ + private String classPrefix = ""; + + /** + * Mapper 类的后缀。 + */ + private String classSuffix = "Mapper"; + + /** + * 自定义 Mapper 的父类。 + */ + private Class supperClass = BaseMapper.class; + + public String buildSuperClassImport() { + return supperClass.getName(); + } + + public String buildSuperClassName() { + return supperClass.getSimpleName(); + } + +} \ 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..4289ec1c --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/PackageConfig.java @@ -0,0 +1,106 @@ +package com.mybatisflex.codegen.config; + +import com.mybatisflex.core.util.StringUtil; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 生成软件包的配置。 + * + * @author 王帅 + * @since 2023-05-15 + */ +@Data +@Accessors(chain = true) +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; + + public String getSourceDir() { + if (sourceDir == null || StringUtil.isBlank(sourceDir)) { + return System.getProperty("user.dir") + "/src/main/java"; + } + return sourceDir; + } + + public String getEntityPackage() { + if (StringUtil.isBlank(entityPackage)) { + return basePackage.concat(".entity"); + } + return entityPackage; + } + + public String getMapperPackage() { + if (StringUtil.isBlank(mapperPackage)) { + return basePackage.concat(".mapper"); + } + return mapperPackage; + } + + public String getServicePackage() { + if (StringUtil.isBlank(servicePackage)) { + return basePackage.concat(".service"); + } + return servicePackage; + } + + public String getServiceImplPackage() { + if (StringUtil.isBlank(serviceImplPackage)) { + return basePackage.concat(".service.impl"); + } + return serviceImplPackage; + } + + public String getControllerPackage() { + if (StringUtil.isBlank(controllerPackage)) { + return basePackage.concat(".controller"); + } + return controllerPackage; + } + + public String getTableDefPackage() { + if (StringUtil.isBlank(tableDefPackage)) { + return getEntityPackage().concat(".tables"); + } + return tableDefPackage; + } + +} \ 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..5c17b8df --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceConfig.java @@ -0,0 +1,55 @@ +/** + * 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.IService; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 生成 Service 的配置。 + * + * @author 王帅 + * @since 2023-05-15 + */ +@Data +@Accessors(chain = true) +public class ServiceConfig { + + /** + * Service 类的前缀。 + */ + private String classPrefix = ""; + + /** + * Service 类的后缀。 + */ + private String classSuffix = "Service"; + + /** + * 自定义 Service 的父类。 + */ + private Class supperClass = IService.class; + + public String buildSuperClassImport() { + return supperClass.getName(); + } + + public String buildSuperClassName() { + return supperClass.getSimpleName(); + } + +} \ 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..6956cac5 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceImplConfig.java @@ -0,0 +1,55 @@ +/** + * 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; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 生成 ServiceImpl 的配置。 + * + * @author 王帅 + * @since 2023-05-15 + */ +@Data +@Accessors(chain = true) +public class ServiceImplConfig { + + /** + * ServiceImpl 类的前缀。 + */ + private String classPrefix = ""; + + /** + * ServiceImpl 类的后缀。 + */ + private String classSuffix = "ServiceImpl"; + + /** + * 自定义 ServiceImpl 的父类。 + */ + private Class supperClass = ServiceImpl.class; + + public String buildSuperClassImport() { + return supperClass.getName(); + } + + public String buildSuperClassName() { + return supperClass.getSimpleName(); + } + +} \ 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..97484d9d --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/StrategyConfig.java @@ -0,0 +1,173 @@ +package com.mybatisflex.codegen.config; + +import com.mybatisflex.codegen.template.EnjoyTemplate; +import com.mybatisflex.codegen.template.ITemplate; +import lombok.Data; +import lombok.experimental.Accessors; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * 表策略配置。 + * + * @author 王帅 + * @since 2023-05-14 + */ +@Data +@Accessors(chain = true) +public class StrategyConfig { + + /** + * 使用哪个模板引擎来生成代码。 + */ + protected ITemplate templateEngine; + /** + * 数据库表前缀,多个前缀用英文逗号(,) 隔开。 + */ + private String tablePrefix; + /** + * 逻辑删除的默认字段名称。 + */ + private String logicDeleteColumn; + /** + * 乐观锁的字段名称。 + */ + private String versionColumn; + /** + * 是否生成视图映射。 + */ + private boolean generateForView; + /** + * 是否覆盖之前生成的文件。 + */ + private boolean overwriteEnable; + /** + * 单独为某张表添加独立的配置。 + */ + private Map tableConfigMap; + /** + * 设置某个列的全局配置。 + */ + private Map columnConfigMap; + /** + * 生成那些表,白名单。 + */ + private Set generateTables; + /** + * 不生成那些表,黑名单。 + */ + private Set unGenerateTables; + + public void addTableConfig(TableConfig tableConfig) { + if (tableConfigMap == null) { + tableConfigMap = new HashMap<>(); + } + tableConfigMap.put(tableConfig.getTableName(), tableConfig); + } + + public TableConfig getTableConfig(String tableName) { + return tableConfigMap == null ? null : tableConfigMap.get(tableName); + } + + public void addColumnConfig(ColumnConfig columnConfig) { + if (columnConfigMap == null) { + columnConfigMap = new HashMap<>(); + } + columnConfigMap.put(columnConfig.getColumnName(), columnConfig); + } + + 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); + } + + 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 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 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; + } + +} \ 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..ea8e7c28 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TableDefConfig.java @@ -0,0 +1,41 @@ +/** + * 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 lombok.Data; +import lombok.experimental.Accessors; + +/** + * 生成 TableDef 的配置。 + * + * @author 王帅 + * @since 2023-05-15 + */ +@Data +@Accessors(chain = true) +public class TableDefConfig { + + /** + * TableDef 类的前缀。 + */ + private String classPrefix = ""; + + /** + * TableDef 类的后缀。 + */ + private String classSuffix = "Def"; + +} \ No newline at end of file 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..c82272bc 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; @@ -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()); } } @@ -168,7 +169,7 @@ public class Table { 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 +190,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 +203,23 @@ public class Table { */ public String buildTableDefClassName() { String tableDefJavaFileName = getEntityJavaFileName(); - return globalConfig.getTableDefClassPrefix() + TableDefConfig tableDefConfig = globalConfig.getTableDefConfig(); + return tableDefConfig.getClassPrefix() + tableDefJavaFileName - + globalConfig.getTableDefClassSuffix(); + + tableDefConfig.getClassSuffix(); } 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 +231,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 +266,7 @@ public class Table { */ public String buildTableAnnotation() { StringBuilder tableAnnotation = new StringBuilder(); - if (globalConfig.isEntityWithLombok()) { + if (globalConfig.getEntityConfig().isWithLombok()) { //@Data //@Builder //@NoArgsConstructor @@ -273,19 +281,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,35 +302,6 @@ 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{" + 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..949e06f0 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 @@ -16,6 +16,8 @@ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.config.PackageConfig; +import com.mybatisflex.codegen.config.StrategyConfig; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -29,7 +31,6 @@ import java.util.Map; * @author 王帅 * @since 2023-05-14 */ -@SuppressWarnings("unused") public class ControllerGenerator implements IGenerator { private String templatePath = "/templates/enjoy/controller.tpl"; @@ -47,21 +48,25 @@ public class ControllerGenerator implements IGenerator { if (!globalConfig.isControllerGenerateEnable()) { return; } + + PackageConfig packageConfig = globalConfig.getPackageConfig(); + StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); - String controllerPackagePath = globalConfig.getControllerPackage().replace(".", "/"); - File controllerJavaFile = new File(globalConfig.getSourceDir(), controllerPackagePath + "/" + + String controllerPackagePath = packageConfig.getControllerPackage().replace(".", "/"); + File controllerJavaFile = new File(packageConfig.getSourceDir(), controllerPackagePath + "/" + table.buildControllerClassName() + ".java"); - if (controllerJavaFile.exists() && !globalConfig.isControllerOverwriteEnable()) { + if (controllerJavaFile.exists() && strategyConfig.isOverwriteEnable()) { return; } - Map params = new HashMap<>(2); + Map params = new HashMap<>(3); params.put("table", table); - params.put("globalConfig", globalConfig); + params.put("packageConfig", packageConfig); + params.put("controllerConfig", globalConfig.getControllerConfig()); - globalConfig.getTemplateEngine().generate(params, templatePath, controllerJavaFile); + strategyConfig.getTemplateEngine().generate(params, templatePath, controllerJavaFile); } } 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..18aeb330 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 @@ -16,6 +16,8 @@ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.config.PackageConfig; +import com.mybatisflex.codegen.config.StrategyConfig; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -23,6 +25,12 @@ 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"; @@ -37,14 +45,28 @@ 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(); + StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); + + 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() && !strategyConfig.isOverwriteEnable()) { + return; + } + + + Map params = new HashMap<>(3); + params.put("table", table); + params.put("packageConfig", packageConfig); + params.put("entityConfig", globalConfig.getEntityConfig()); + + strategyConfig.getTemplateEngine().generate(params, templatePath, entityJavaFile); } } 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..b2ac2fac 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 @@ -16,6 +16,8 @@ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.config.PackageConfig; +import com.mybatisflex.codegen.config.StrategyConfig; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -23,6 +25,12 @@ 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"; @@ -40,21 +48,25 @@ public class MapperGenerator implements IGenerator { if (!globalConfig.isMapperGenerateEnable()) { return; } + + PackageConfig packageConfig = globalConfig.getPackageConfig(); + StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); - 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() && !strategyConfig.isOverwriteEnable()) { + return; } - Map params = new HashMap<>(); + Map params = new HashMap<>(3); params.put("table", table); - params.put("globalConfig", globalConfig); + params.put("packageConfig", packageConfig); + params.put("mapperConfig", globalConfig.getMapperConfig()); - globalConfig.getTemplateEngine().generate(params, templatePath, mapperJavaFile); + strategyConfig.getTemplateEngine().generate(params, templatePath, mapperJavaFile); } } 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..7176001c 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 @@ -16,6 +16,8 @@ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.config.PackageConfig; +import com.mybatisflex.codegen.config.StrategyConfig; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -29,7 +31,6 @@ import java.util.Map; * @author 王帅 * @since 2023-05-14 */ -@SuppressWarnings("unused") public class ServiceGenerator implements IGenerator { private String templatePath = "/templates/enjoy/service.tpl"; @@ -47,21 +48,25 @@ public class ServiceGenerator implements IGenerator { if (!globalConfig.isServiceGenerateEnable()) { return; } + + PackageConfig packageConfig = globalConfig.getPackageConfig(); + StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); - 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() && !strategyConfig.isOverwriteEnable()) { return; } - Map params = new HashMap<>(2); + Map params = new HashMap<>(3); params.put("table", table); - params.put("globalConfig", globalConfig); + params.put("packageConfig", packageConfig); + params.put("serviceConfig", globalConfig.getServiceConfig()); - globalConfig.getTemplateEngine().generate(params, templatePath, serviceJavaFile); + strategyConfig.getTemplateEngine().generate(params, templatePath, serviceJavaFile); } } 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..15530716 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 @@ -16,6 +16,8 @@ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.config.PackageConfig; +import com.mybatisflex.codegen.config.StrategyConfig; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -29,7 +31,6 @@ import java.util.Map; * @author 王帅 * @since 2023-05-14 */ -@SuppressWarnings("unused") public class ServiceImplGenerator implements IGenerator { private String templatePath = "/templates/enjoy/serviceImpl.tpl"; @@ -47,21 +48,25 @@ public class ServiceImplGenerator implements IGenerator { if (!globalConfig.isServiceImplGenerateEnable()) { return; } + + PackageConfig packageConfig = globalConfig.getPackageConfig(); + StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); - 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() && !strategyConfig.isOverwriteEnable()) { return; } - Map params = new HashMap<>(2); + Map params = new HashMap<>(3); params.put("table", table); - params.put("globalConfig", globalConfig); + params.put("packageConfig", packageConfig); + params.put("serviceImplConfig", globalConfig.getServiceImplConfig()); - globalConfig.getTemplateEngine().generate(params, templatePath, serviceImplJavaFile); + strategyConfig.getTemplateEngine().generate(params, templatePath, serviceImplJavaFile); } } 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..d4a146b2 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 @@ -16,6 +16,8 @@ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; +import com.mybatisflex.codegen.config.PackageConfig; +import com.mybatisflex.codegen.config.StrategyConfig; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -23,6 +25,12 @@ 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"; @@ -41,15 +49,24 @@ public class TableDefGenerator implements IGenerator { return; } - String tableDefPackagePath = globalConfig.getTableDefPackage().replace(".", "/"); - File tableDefJavaFile = new File(globalConfig.getSourceDir(), tableDefPackagePath + "/" + + PackageConfig packageConfig = globalConfig.getPackageConfig(); + StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); + + 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() && !strategyConfig.isOverwriteEnable()) { + return; + } - globalConfig.getTemplateEngine().generate(params, templatePath, tableDefJavaFile); + + Map params = new HashMap<>(3); + params.put("table", table); + params.put("packageConfig", packageConfig); + params.put("tableDefConfig", globalConfig.getTableDefConfig()); + + strategyConfig.getTemplateEngine().generate(params, templatePath, tableDefJavaFile); } } 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..6c730159 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/controller.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/controller.tpl @@ -1,23 +1,23 @@ -package #(globalConfig.controllerPackage); +package #(packageConfig.controllerPackage); import org.springframework.web.bind.annotation.RequestMapping; -#if(globalConfig.restStyleController) +#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) +#if(controllerConfig.restStyle) @RestController #else @Controller #end @RequestMapping("/#(table.getEntityJavaFileName())") -#if(globalConfig.controllerSupperClass) -public class #(table.table.buildControllerClassName()) extends #(table.buildControllerName()) { +#if(controllerConfig.supperClass) +public class #(table.buildControllerClassName()) extends #(controllerConfig.buildSuperClassName()) { } #else 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..a5e3a035 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl @@ -1,4 +1,4 @@ -package #(globalConfig.entityPackage); +package #(packageConfig.entityPackage); #for(importClass:table.buildImports()) import #(importClass); @@ -13,7 +13,7 @@ public class #(table.buildEntityClassName())#(table.buildExtends())#(table.build #(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..227a59d6 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,8 @@ -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())> { +public interface #(table.buildMapperClassName()) extends #(mapperConfig.buildSuperClassName())<#(table.buildEntityClassName())> { } 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..7b08c377 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,8 @@ -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())> { +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..7de44a69 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,12 @@ -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; @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..8c40f9cf 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/tableDef.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/tableDef.tpl @@ -1,4 +1,4 @@ -package #(globalConfig.entityPackage).tables; +package #(packageConfig.tableDefPackage); import com.mybatisflex.core.query.QueryColumn; import com.mybatisflex.core.table.TableDef; From 19069a129adf51450229b339e4de70585d977d16 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Mon, 15 May 2023 19:43:02 +0800 Subject: [PATCH 02/28] =?UTF-8?q?doc:=20=E6=96=B0=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=99=A8=E4=BB=8B=E7=BB=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/others/codegen.md | 432 ++++++++++++++++++++++++-------------- 1 file changed, 272 insertions(+), 160 deletions(-) diff --git a/docs/zh/others/codegen.md b/docs/zh/others/codegen.md index 3aeb6745..790b0a6d 100644 --- a/docs/zh/others/codegen.md +++ b/docs/zh/others/codegen.md @@ -45,30 +45,29 @@ public class Codegen { //创建配置内容 GlobalConfig globalConfig = new GlobalConfig(); - //设置只生成哪些表 - globalConfig.addGenerateTable("account", "account_session"); + //设置根包 + globalConfig.getPackageConfig() + .setBasePackage("com.test"); - //设置 entity 的包名 - globalConfig.setEntityPackage("com.test.entity"); + //设置表前缀和只生成哪些表 + globalConfig.getStrategyConfig() + .setTablePrefix("tb_") + .addGenerateTable("account", "account_session"); - //设置表前缀 - //globalConfig.setTablePrefix("tb_"); - - //设置 entity 是否使用 Lombok - //globalConfig.setEntityWithLombok(true); + //设置生成 entity 并启用 Lombok + globalConfig.enableEntity() + .setWithLombok(true); - //是否生成 mapper 类,默认为 false - //globalConfig.setMapperGenerateEnable(true); - - //设置 mapper 类的包名 - globalConfig.setMapperPackage("com.test.mapper"); + //设置生成 mapper + globalConfig.enableMapper(); //可以单独配置某个列 ColumnConfig columnConfig = new ColumnConfig(); columnConfig.setColumnName("tenant_id"); columnConfig.setLarge(true); columnConfig.setVersion(true); - globalConfig.addColumnConfig("account", columnConfig); + globalConfig.getStrategyConfig() + .addColumnConfig("account", columnConfig); //通过 datasource 和 globalConfig 创建代码生成器 Generator generator = new Generator(dataSource, globalConfig); @@ -84,158 +83,266 @@ public class Codegen { 关闭 APT 的 Mapper 类文件生成,请参考:[APT 设置章节](../others/apt.md) -## 全局配置 GlobalConfig +## 使用介绍 -GlobalConfig 支持更多的配置如下: +在 Mybatis-Flex 的代码生成器中,支持如下 6 种类型的的产物生成: + +- Entity 实体类 +- Mapper 映射类 +- TableDef 表定义辅助类 +- Service 服务类 +- ServiceImpl 服务实现类 +- Controller 控制类 + +启用或关闭某种类型产物的生成,代码如下: + +```java + +// 开启 Entity 的生成 +globalConfig.enableEntity(); +// 关闭 Entity 的生成 +globalConfig.disableEntity(); + +``` + +所有方法均支持链式调用配置,代码如下: + +```java + +// 设置生成 Entity 并启用 Lombok、设置父类 +globalConfig.enableEntity() + .setWithLombok(true) + .setSupperClass(BaseEntity.class); + +``` + +## 全局配置 `GlobalConfig` + +GlobalConfig 全局配置项包含多个子配置项: ```java public class GlobalConfig { - //代码生成目录 + private final PackageConfig packageConfig; + private final StrategyConfig strategyConfig; + + private EntityConfig entityConfig; + private MapperConfig mapperConfig; + private ServiceConfig serviceConfig; + private ServiceImplConfig serviceImplConfig; + private ControllerConfig controllerConfig; + private TableDefConfig tableDefConfig; + + private Map customConfig; + +} + +``` + +## 包配置 `PackageConfig` + +PackageConfig 支持的配置如下: + +```java + +public class PackageConfig { + + //代码生成目录。 private String sourceDir; - //根包名 + //根包。 private String basePackage = "com.mybatisflex"; - //entity 的包名 + //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 的包名 + //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 的包名 + //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 的包名 + //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 的包名 + //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; + //TableDef 所在包。 + private String tableDefPackage; } ``` -## 表配置 TableConfig +## 策略配置 `StrategyConfig` + +StrategyConfig 支持的配置如下: + +```java + +public class StrategyConfig { + + //数据库表前缀,多个前缀用英文逗号(,) 隔开。 + private String tablePrefix; + + //逻辑删除的默认字段名称。 + private String logicDeleteColumn; + + //乐观锁的字段名称。 + private String versionColumn; + + //是否生成视图映射。 + private boolean generateForView; + + //是否覆盖之前生成的文件。 + private boolean overwriteEnable; + + //单独为某张表添加独立的配置。 + private Map tableConfigMap; + + //设置某个列的全局配置。 + private Map defaultColumnConfigMap; + + //生成那些表,白名单。 + private Set generateTables; + + //不生成那些表,黑名单。 + private Set unGenerateTables; + + //使用哪个模板引擎来生成代码。 + protected ITemplate templateEngine; + +} +``` + +## Entity 生成配置 `EntityConfig` + +EntityConfig 支持的配置如下: + +```java + +public class EntityConfig { + + //Entity 类的前缀。 + private String classPrefix = ""; + + //Entity 类的后缀。 + private String classSuffix = ""; + + //Entity 类的父类,可以自定义一些 BaseEntity 类。 + private Class supperClass; + + //Entity 默认实现的接口。 + private Class[] implInterfaces = {Serializable.class}; + + //Entity 是否使用 Lombok 注解。 + private boolean withLombok; + +} +``` + +## Mapper 生成配置 `MapperConfig` + +MapperConfig 支持的配置如下: + +```java + +public class MapperConfig { + + //Mapper 类的前缀。 + private String classPrefix = ""; + + //Mapper 类的后缀。 + private String classSuffix = "Mapper"; + + //自定义 Mapper 的父类。 + private Class supperClass = BaseMapper.class; + +} +``` + +## Service 生成配置 `ServiceConfig` + +ServiceConfig 支持的配置如下: + +```java + +public class ServiceConfig { + + //Service 类的前缀。 + private String classPrefix = ""; + + //Service 类的后缀。 + private String classSuffix = "Service"; + + //自定义 Service 的父类。 + private Class supperClass = IService.class; + +} +``` + +## ServiceImpl 生成配置 `ServiceImplConfig` + +ServiceImplConfig 支持的配置如下: + +```java + +public class ServiceImplConfig { + + //ServiceImpl 类的前缀。 + private String classPrefix = ""; + + //ServiceImpl 类的后缀。 + private String classSuffix = "ServiceImpl"; + + //自定义 ServiceImpl 的父类。 + private Class supperClass = ServiceImpl.class; + +} +``` + +## Controller 生成配置 `ControllerConfig` + +ControllerConfig 支持的配置如下: + +```java + +public class ControllerConfig { + + //Controller 类的前缀。 + private String classPrefix = ""; + + //Controller 类的后缀。 + private String classSuffix = "Controller"; + + //自定义 Controller 的父类。 + private Class supperClass; + + //生成 REST 风格的 Controller。 + private boolean restStyle = true; + +} +``` + +## TableDef 生成配置 `TableDefConfig` + +TableDefConfig 支持的配置如下: + +```java + +public class TableDefConfig { + + //TableDef 类的前缀。 + private String classPrefix = ""; + + //TableDef 类的后缀。 + private String classSuffix = "Def"; + +} +``` + +## 表配置 `TableConfig` TableConfig 支持的配置如下: @@ -265,7 +372,7 @@ public class TableConfig { } ``` -## 列配置 ColumnConfig +## 列配置 `ColumnConfig` ColumnConfig 支持的配置如下: @@ -354,16 +461,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 +471,29 @@ 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(); + StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); + + 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() && !strategyConfig.isOverwriteEnable()) { + return; + } + + + Map params = new HashMap<>(3); + params.put("table", table); + params.put("packageConfig", packageConfig); + params.put("entityConfig", globalConfig.getEntityConfig()); + + strategyConfig.getTemplateEngine().generate(params, templatePath, entityJavaFile); } } ``` From 9165d8d44a6b9229f1d6fb1322cab6b8d60ef785 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Mon, 15 May 2023 20:04:55 +0800 Subject: [PATCH 03/28] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=94=AF=E6=8C=81=E6=96=B0?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=99=A8=E6=A0=BC=E5=BC=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/test/GeneratorTest.java | 91 ++++++++++--------- .../codegen/test/SqliteGeneratorTest.java | 24 ++--- 2 files changed, 58 insertions(+), 57 deletions(-) diff --git a/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java b/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java index ccab77f0..856d9247 100644 --- a/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java +++ b/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/GeneratorTest.java @@ -38,40 +38,36 @@ public class GeneratorTest { // JdbcTypeMapping.registerMapping(Integer.class, Long.class); GlobalConfig globalConfig = new GlobalConfig(); - globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java"); - // globalConfig.setTablePrefix("tb_"); - // globalConfig.setEntityWithLombok(true); - globalConfig.setEntitySupperClass(BaseEntity.class); + //设置生成文件目录和根包 + globalConfig.getPackageConfig() + .setSourceDir(System.getProperty("user.dir") + "/src/test/java") + .setBasePackage("com.test"); //设置只生成哪些表 - globalConfig.addGenerateTable("account", "account_session"); + globalConfig.getStrategyConfig() + .addGenerateTable("account", "account_session"); - //设置 entity 的包名 - globalConfig.setEntityPackage("com.test.entity"); - globalConfig.setEntityClassPrefix("My"); - globalConfig.setEntityClassSuffix("Entity"); + //设置生成 entity + globalConfig.enableEntity() + .setWithLombok(true) + .setClassPrefix("My") + .setClassSuffix("Entity") + .setSupperClass(BaseEntity.class); - //设置 entity 的包名 - globalConfig.setTableDefGenerateEnable(true); - globalConfig.setTableDefPackage("com.test.entity.tables"); - globalConfig.setTableDefClassPrefix("My"); - globalConfig.setTableDefClassSuffix("TableDef"); - - //是否生成 mapper 类,默认为 false - globalConfig.setMapperGenerateEnable(true); - globalConfig.setMapperClassPrefix("Flex"); - globalConfig.setMapperClassSuffix("Dao"); - - //设置 mapper 类的包名 - globalConfig.setMapperPackage("com.test.mapper"); - globalConfig.setMapperSupperClass(MyBaseMapper.class); + //设置生成 tableDef + globalConfig.enableTableDef(); + //设置生成 mapper + globalConfig.enableMapper() + .setClassPrefix("Flex") + .setClassSuffix("Dao") + .setSupperClass(MyBaseMapper.class); TableConfig tableConfig = new TableConfig(); tableConfig.setTableName("account"); tableConfig.setUpdateListenerClass(MyUpdateListener.class); - globalConfig.addTableConfig(tableConfig); + globalConfig.getStrategyConfig().addTableConfig(tableConfig); //可以单独配置某个列 @@ -79,7 +75,7 @@ public class GeneratorTest { columnConfig.setColumnName("tenant_id"); columnConfig.setLarge(true); columnConfig.setVersion(true); - globalConfig.addColumnConfig("account", columnConfig); + globalConfig.getStrategyConfig().addColumnConfig("account", columnConfig); //通过 datasource 和 globalConfig 创建代码生成器 @@ -91,39 +87,44 @@ public class GeneratorTest { @Test public void testCodeGen() { - // 配置数据源 + //配置数据源 HikariDataSource dataSource = new HikariDataSource(); dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8"); dataSource.setUsername("root"); dataSource.setPassword("12345678"); GlobalConfig globalConfig = new GlobalConfig(); - globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java"); - globalConfig.setTablePrefix("sys_"); - globalConfig.setBasePackage("com.test"); - globalConfig.setEntityWithLombok(true); - globalConfig.setEntitySupperClass(BaseEntity.class); + //设置生成文件目录和根包 + globalConfig.getPackageConfig() + .setSourceDir(System.getProperty("user.dir") + "/src/test/java") + .setBasePackage("com.test"); - // 设置只生成哪些表 - globalConfig.addGenerateTable("sys_user"); + //设置表前缀和只生成哪些表 + globalConfig.getStrategyConfig() + .setTablePrefix("sys_") + .addGenerateTable("sys_user"); - // 设置 entity 的包名 - globalConfig.setTableDefGenerateEnable(true); + //配置生成 entity + globalConfig.enableEntity() + .setWithLombok(true) + .setSupperClass(BaseEntity.class); - // 是否生成 mapper 类,默认为 false - globalConfig.setMapperGenerateEnable(true); - // 是否生成 service 类,默认为 false - globalConfig.setServiceGenerateEnable(true); - // 是否生成 serviceImpl 类,默认为 false - globalConfig.setServiceImplGenerateEnable(true); - // 是否生成 controller 类,默认为 false - globalConfig.setControllerGenerateEnable(true); + //配置生成 mapper + globalConfig.enableMapper(); + //配置生成 service + globalConfig.enableService(); + //配置生成 serviceImpl + globalConfig.enableServiceImpl(); + //配置生成 controller + globalConfig.enableController(); + //配置生成 tableDef + globalConfig.enableTableDef(); - // 通过 datasource 和 globalConfig 创建代码生成器 + //通过 datasource 和 globalConfig 创建代码生成器 Generator generator = new Generator(dataSource, globalConfig); - // 开始生成代码 + //开始生成代码 generator.generate(); } diff --git a/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/SqliteGeneratorTest.java b/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/SqliteGeneratorTest.java index 00f086f6..f8cdde78 100644 --- a/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/SqliteGeneratorTest.java +++ b/mybatis-flex-codegen/src/test/java/com/mybatisflex/codegen/test/SqliteGeneratorTest.java @@ -4,14 +4,13 @@ import com.mybatisflex.codegen.Generator; import com.mybatisflex.codegen.config.GlobalConfig; import com.mybatisflex.codegen.dialect.IDialect; import com.zaxxer.hikari.HikariDataSource; -import org.junit.Test; import java.sql.Connection; import java.sql.Statement; public class SqliteGeneratorTest { -// @Test + // @Test public void testGenerator3() { //配置数据源 @@ -24,20 +23,21 @@ public class SqliteGeneratorTest { GlobalConfig globalConfig = new GlobalConfig(); - globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java"); + + //配置生成文件目录与根包 + globalConfig.getPackageConfig() + .setSourceDir(System.getProperty("user.dir") + "/src/test/java") + .setBasePackage("com.test"); //设置只生成哪些表 - globalConfig.addGenerateTable("person"); + globalConfig.getStrategyConfig() + .addGenerateTable("person"); - //设置 entity 的包名 - globalConfig.setEntityPackage("com.test.entity"); + globalConfig.enableEntity() + .setWithLombok(true); - //是否生成 mapper 类,默认为 false - globalConfig.setMapperGenerateEnable(true); - globalConfig.setEntityWithLombok(true); - - //设置 mapper 类的包名 - globalConfig.setMapperPackage("com.test.mapper"); + //设置生成 mapper 类 + globalConfig.enableMapper(); Generator generator = new Generator(dataSource, globalConfig, IDialect.SQLITE); From 92f2cb8c078f509d6aae0172132f11f923cfaf1a Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Tue, 16 May 2023 19:51:15 +0800 Subject: [PATCH 04/28] =?UTF-8?q?refactor:=20=E5=8E=BB=E6=8E=89=20Lombok?= =?UTF-8?q?=20=E6=B3=A8=E8=A7=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/ControllerConfig.java | 42 ++++++++- .../codegen/config/EntityConfig.java | 51 ++++++++++- .../codegen/config/GlobalConfig.java | 35 ++++++- .../codegen/config/MapperConfig.java | 32 ++++++- .../codegen/config/PackageConfig.java | 49 +++++++++- .../codegen/config/ServiceConfig.java | 32 ++++++- .../codegen/config/ServiceImplConfig.java | 32 ++++++- .../codegen/config/StrategyConfig.java | 91 ++++++++++++++++++- .../codegen/config/TableDefConfig.java | 24 ++++- 9 files changed, 350 insertions(+), 38 deletions(-) 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 index 38686ef4..2dd47e76 100644 --- 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 @@ -15,17 +15,13 @@ */ package com.mybatisflex.codegen.config; -import lombok.Data; -import lombok.experimental.Accessors; - /** * 生成 Controller 的配置。 * * @author 王帅 * @since 2023-05-15 */ -@Data -@Accessors(chain = true) +@SuppressWarnings("unused") public class ControllerConfig { /** @@ -56,4 +52,40 @@ public class ControllerConfig { 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 isRestStyle() { + return restStyle; + } + + 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 index 4cab2f59..02e3ce1f 100644 --- 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 @@ -15,9 +15,6 @@ */ package com.mybatisflex.codegen.config; -import lombok.Data; -import lombok.experimental.Accessors; - import java.io.Serializable; /** @@ -26,8 +23,7 @@ import java.io.Serializable; * @author 王帅 * @since 2023-05-15 */ -@Data -@Accessors(chain = true) +@SuppressWarnings("unused") public class EntityConfig { /** @@ -55,4 +51,49 @@ public class EntityConfig { */ 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 Class[] getImplInterfaces() { + return implInterfaces; + } + + public EntityConfig setImplInterfaces(Class[] implInterfaces) { + this.implInterfaces = implInterfaces; + return this; + } + + public boolean isWithLombok() { + return withLombok; + } + + 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 941e1adb..64a6f988 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 @@ -15,8 +15,6 @@ */ package com.mybatisflex.codegen.config; -import lombok.Getter; - import java.util.HashMap; import java.util.Map; @@ -26,7 +24,6 @@ import java.util.Map; * @author 王帅 * @since 2023-05-15 */ -@Getter @SuppressWarnings("unused") public class GlobalConfig { @@ -62,6 +59,14 @@ public class GlobalConfig { this.strategyConfig = new StrategyConfig(); } + public PackageConfig getPackageConfig() { + return packageConfig; + } + + public StrategyConfig getStrategyConfig() { + return strategyConfig; + } + public EntityConfig getEntityConfig() { if (entityConfig == null) { entityConfig = new EntityConfig(); @@ -158,6 +163,30 @@ public class GlobalConfig { tableDefGenerateEnable = false; } + public boolean isEntityGenerateEnable() { + return entityGenerateEnable; + } + + public boolean isMapperGenerateEnable() { + return mapperGenerateEnable; + } + + public boolean isServiceGenerateEnable() { + return serviceGenerateEnable; + } + + public boolean isServiceImplGenerateEnable() { + return serviceImplGenerateEnable; + } + + public boolean isControllerGenerateEnable() { + return controllerGenerateEnable; + } + + public boolean isTableDefGenerateEnable() { + return tableDefGenerateEnable; + } + public void addCustomConfig(String key, Object value) { if (customConfig == null) { customConfig = new HashMap<>(); 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 index 6f6e8745..2aed21b8 100644 --- 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 @@ -16,8 +16,6 @@ package com.mybatisflex.codegen.config; import com.mybatisflex.core.BaseMapper; -import lombok.Data; -import lombok.experimental.Accessors; /** * 生成 Mapper 的配置。 @@ -25,8 +23,7 @@ import lombok.experimental.Accessors; * @author 王帅 * @since 2023-05-15 */ -@Data -@Accessors(chain = true) +@SuppressWarnings("unused") public class MapperConfig { /** @@ -52,4 +49,31 @@ public class MapperConfig { 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; + } + } \ 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 index 4289ec1c..5b24be30 100644 --- 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 @@ -1,8 +1,6 @@ package com.mybatisflex.codegen.config; import com.mybatisflex.core.util.StringUtil; -import lombok.Data; -import lombok.experimental.Accessors; /** * 生成软件包的配置。 @@ -10,8 +8,7 @@ import lombok.experimental.Accessors; * @author 王帅 * @since 2023-05-15 */ -@Data -@Accessors(chain = true) +@SuppressWarnings("unused") public class PackageConfig { /** @@ -61,6 +58,20 @@ public class PackageConfig { 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"); @@ -68,6 +79,11 @@ public class PackageConfig { return entityPackage; } + public PackageConfig setEntityPackage(String entityPackage) { + this.entityPackage = entityPackage; + return this; + } + public String getMapperPackage() { if (StringUtil.isBlank(mapperPackage)) { return basePackage.concat(".mapper"); @@ -75,6 +91,11 @@ public class PackageConfig { return mapperPackage; } + public PackageConfig setMapperPackage(String mapperPackage) { + this.mapperPackage = mapperPackage; + return this; + } + public String getServicePackage() { if (StringUtil.isBlank(servicePackage)) { return basePackage.concat(".service"); @@ -82,6 +103,11 @@ public class PackageConfig { return servicePackage; } + public PackageConfig setServicePackage(String servicePackage) { + this.servicePackage = servicePackage; + return this; + } + public String getServiceImplPackage() { if (StringUtil.isBlank(serviceImplPackage)) { return basePackage.concat(".service.impl"); @@ -89,6 +115,11 @@ public class PackageConfig { return serviceImplPackage; } + public PackageConfig setServiceImplPackage(String serviceImplPackage) { + this.serviceImplPackage = serviceImplPackage; + return this; + } + public String getControllerPackage() { if (StringUtil.isBlank(controllerPackage)) { return basePackage.concat(".controller"); @@ -96,6 +127,11 @@ public class PackageConfig { return controllerPackage; } + public PackageConfig setControllerPackage(String controllerPackage) { + this.controllerPackage = controllerPackage; + return this; + } + public String getTableDefPackage() { if (StringUtil.isBlank(tableDefPackage)) { return getEntityPackage().concat(".tables"); @@ -103,4 +139,9 @@ public class PackageConfig { return tableDefPackage; } + public PackageConfig 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/ServiceConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceConfig.java index 5c17b8df..c3084b0f 100644 --- 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 @@ -16,8 +16,6 @@ package com.mybatisflex.codegen.config; import com.mybatisflex.spring.service.IService; -import lombok.Data; -import lombok.experimental.Accessors; /** * 生成 Service 的配置。 @@ -25,8 +23,7 @@ import lombok.experimental.Accessors; * @author 王帅 * @since 2023-05-15 */ -@Data -@Accessors(chain = true) +@SuppressWarnings("unused") public class ServiceConfig { /** @@ -52,4 +49,31 @@ public class ServiceConfig { 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; + } + } \ 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 index 6956cac5..972d77c4 100644 --- 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 @@ -16,8 +16,6 @@ package com.mybatisflex.codegen.config; import com.mybatisflex.spring.service.impl.ServiceImpl; -import lombok.Data; -import lombok.experimental.Accessors; /** * 生成 ServiceImpl 的配置。 @@ -25,8 +23,7 @@ import lombok.experimental.Accessors; * @author 王帅 * @since 2023-05-15 */ -@Data -@Accessors(chain = true) +@SuppressWarnings("unused") public class ServiceImplConfig { /** @@ -52,4 +49,31 @@ public class ServiceImplConfig { 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; + } + } \ 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 index 97484d9d..2879fbf6 100644 --- 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 @@ -2,8 +2,6 @@ package com.mybatisflex.codegen.config; import com.mybatisflex.codegen.template.EnjoyTemplate; import com.mybatisflex.codegen.template.ITemplate; -import lombok.Data; -import lombok.experimental.Accessors; import java.util.HashMap; import java.util.HashSet; @@ -16,8 +14,7 @@ import java.util.Set; * @author 王帅 * @since 2023-05-14 */ -@Data -@Accessors(chain = true) +@SuppressWarnings("unused") public class StrategyConfig { /** @@ -170,4 +167,90 @@ public class StrategyConfig { return templateEngine; } + public StrategyConfig setTemplateEngine(ITemplate templateEngine) { + this.templateEngine = templateEngine; + return this; + } + + 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 boolean isOverwriteEnable() { + return overwriteEnable; + } + + public StrategyConfig setOverwriteEnable(boolean overwriteEnable) { + this.overwriteEnable = overwriteEnable; + 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 index ea8e7c28..dd0abb9c 100644 --- 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 @@ -15,17 +15,13 @@ */ package com.mybatisflex.codegen.config; -import lombok.Data; -import lombok.experimental.Accessors; - /** * 生成 TableDef 的配置。 * * @author 王帅 * @since 2023-05-15 */ -@Data -@Accessors(chain = true) +@SuppressWarnings("unused") public class TableDefConfig { /** @@ -38,4 +34,22 @@ public class TableDefConfig { */ private String classSuffix = "Def"; + 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; + } + } \ No newline at end of file From e79cfea312ad831909716d2fa8d7292564478568 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Tue, 16 May 2023 19:52:33 +0800 Subject: [PATCH 05/28] =?UTF-8?q?refactor:=20=E6=8F=90=E5=8F=96=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E4=BD=8D=E7=BD=AE=E5=B8=B8=E9=87=8F=EF=BC=8C=E4=BB=A5?= =?UTF-8?q?=E4=BE=BF=E7=94=A8=E6=88=B7=E6=94=B9=E5=9B=9E=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/constant/TemplateConst.java | 20 +++++++++++++++++++ .../generator/impl/ControllerGenerator.java | 4 +++- .../generator/impl/EntityGenerator.java | 4 +++- .../generator/impl/MapperGenerator.java | 4 +++- .../generator/impl/ServiceGenerator.java | 4 +++- .../generator/impl/ServiceImplGenerator.java | 4 +++- .../generator/impl/TableDefGenerator.java | 4 +++- 7 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/TemplateConst.java 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..9e77020a --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/TemplateConst.java @@ -0,0 +1,20 @@ +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"; + private TemplateConst() { + } + +} \ No newline at end of file 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 949e06f0..3fb95821 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 @@ -18,6 +18,7 @@ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; import com.mybatisflex.codegen.config.PackageConfig; import com.mybatisflex.codegen.config.StrategyConfig; +import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -33,9 +34,10 @@ import java.util.Map; */ public class ControllerGenerator implements IGenerator { - private String templatePath = "/templates/enjoy/controller.tpl"; + private final String templatePath; public ControllerGenerator() { + this(TemplateConst.CONTROLLER); } public ControllerGenerator(String 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 18aeb330..d43e3390 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 @@ -18,6 +18,7 @@ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; import com.mybatisflex.codegen.config.PackageConfig; import com.mybatisflex.codegen.config.StrategyConfig; +import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -33,9 +34,10 @@ import java.util.Map; */ public class EntityGenerator implements IGenerator { - private String templatePath = "/templates/enjoy/entity.tpl"; + private final String templatePath; public EntityGenerator() { + this(TemplateConst.ENTITY); } public EntityGenerator(String 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 b2ac2fac..f6b2b3f6 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 @@ -18,6 +18,7 @@ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; import com.mybatisflex.codegen.config.PackageConfig; import com.mybatisflex.codegen.config.StrategyConfig; +import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -33,9 +34,10 @@ import java.util.Map; */ public class MapperGenerator implements IGenerator { - private String templatePath = "/templates/enjoy/mapper.tpl"; + private final String templatePath; public MapperGenerator() { + this(TemplateConst.MAPPER); } public MapperGenerator(String templatePath) { 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 7176001c..9f829e06 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 @@ -18,6 +18,7 @@ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; import com.mybatisflex.codegen.config.PackageConfig; import com.mybatisflex.codegen.config.StrategyConfig; +import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -33,9 +34,10 @@ import java.util.Map; */ public class ServiceGenerator implements IGenerator { - private String templatePath = "/templates/enjoy/service.tpl"; + private final String templatePath; public ServiceGenerator() { + this(TemplateConst.SERVICE); } public ServiceGenerator(String 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 15530716..8bd6a7f0 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 @@ -18,6 +18,7 @@ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; import com.mybatisflex.codegen.config.PackageConfig; import com.mybatisflex.codegen.config.StrategyConfig; +import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -33,9 +34,10 @@ import java.util.Map; */ public class ServiceImplGenerator implements IGenerator { - private String templatePath = "/templates/enjoy/serviceImpl.tpl"; + private final String templatePath; public ServiceImplGenerator() { + this(TemplateConst.SERVICE_IMPL); } public ServiceImplGenerator(String 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 d4a146b2..404f304a 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 @@ -18,6 +18,7 @@ package com.mybatisflex.codegen.generator.impl; import com.mybatisflex.codegen.config.GlobalConfig; import com.mybatisflex.codegen.config.PackageConfig; import com.mybatisflex.codegen.config.StrategyConfig; +import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -33,9 +34,10 @@ import java.util.Map; */ public class TableDefGenerator implements IGenerator { - private String templatePath = "/templates/enjoy/tableDef.tpl"; + private final String templatePath; public TableDefGenerator() { + this(TemplateConst.TABLE_DEF); } public TableDefGenerator(String templatePath) { From 1fbd11cfd8456a916b32375d2f1915040f5a57fc Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 17 May 2023 14:18:18 +0800 Subject: [PATCH 06/28] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=E9=85=8D=E7=BD=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/GlobalConfig.java | 6 ++ .../codegen/config/JavadocConfig.java | 68 +++++++++++++++++++ .../codegen/config/StrategyConfig.java | 9 +++ .../com/mybatisflex/codegen/entity/Table.java | 13 +--- .../generator/impl/ControllerGenerator.java | 3 +- .../generator/impl/EntityGenerator.java | 3 +- .../generator/impl/MapperGenerator.java | 3 +- .../generator/impl/ServiceGenerator.java | 3 +- .../generator/impl/ServiceImplGenerator.java | 3 +- .../generator/impl/TableDefGenerator.java | 3 +- .../resources/templates/enjoy/controller.tpl | 6 ++ .../main/resources/templates/enjoy/entity.tpl | 7 +- .../main/resources/templates/enjoy/mapper.tpl | 6 ++ .../resources/templates/enjoy/service.tpl | 6 ++ .../resources/templates/enjoy/serviceImpl.tpl | 6 ++ .../resources/templates/enjoy/tableDef.tpl | 7 +- .../codegen/test/GeneratorTest.java | 10 +++ 17 files changed, 142 insertions(+), 20 deletions(-) create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/JavadocConfig.java 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 64a6f988..107d1315 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 @@ -29,6 +29,7 @@ public class GlobalConfig { // === 必须配置 === + private final JavadocConfig javadocConfig; private final PackageConfig packageConfig; private final StrategyConfig strategyConfig; @@ -55,10 +56,15 @@ public class GlobalConfig { private boolean tableDefGenerateEnable; public GlobalConfig() { + this.javadocConfig = new JavadocConfig(); this.packageConfig = new PackageConfig(); this.strategyConfig = new StrategyConfig(); } + public JavadocConfig getJavadocConfig() { + return javadocConfig; + } + public PackageConfig getPackageConfig() { return packageConfig; } 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..5c25940a --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/JavadocConfig.java @@ -0,0 +1,68 @@ +package com.mybatisflex.codegen.config; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.function.Function; +import java.util.function.Supplier; + +/** + * 注释配置类。 + * + * @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 Function tableRemarkFormat = Function.identity(); + + 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 tableRemarkFormat.apply(comment); + } + + public Function getTableRemarkFormat() { + return tableRemarkFormat; + } + + public JavadocConfig setTableRemarkFormat(Function tableRemarkFormat) { + this.tableRemarkFormat = tableRemarkFormat; + 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 index 2879fbf6..0b1c53ed 100644 --- 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 @@ -21,38 +21,47 @@ public class StrategyConfig { * 使用哪个模板引擎来生成代码。 */ protected ITemplate templateEngine; + /** * 数据库表前缀,多个前缀用英文逗号(,) 隔开。 */ private String tablePrefix; + /** * 逻辑删除的默认字段名称。 */ private String logicDeleteColumn; + /** * 乐观锁的字段名称。 */ private String versionColumn; + /** * 是否生成视图映射。 */ private boolean generateForView; + /** * 是否覆盖之前生成的文件。 */ private boolean overwriteEnable; + /** * 单独为某张表添加独立的配置。 */ private Map tableConfigMap; + /** * 设置某个列的全局配置。 */ private Map columnConfigMap; + /** * 生成那些表,白名单。 */ private Set generateTables; + /** * 不生成那些表,黑名单。 */ 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 c82272bc..263f3cc5 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 @@ -41,7 +41,7 @@ public class Table { } public String getRemarks() { - return remarks; + return globalConfig.getJavadocConfig().formatTableComment(remarks); } public void setRemarks(String remarks) { @@ -156,17 +156,6 @@ 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.getStrategyConfig().getTablePrefix(); 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 3fb95821..cc03dc06 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 @@ -64,9 +64,10 @@ public class ControllerGenerator implements IGenerator { } - Map params = new HashMap<>(3); + Map params = new HashMap<>(4); params.put("table", table); params.put("packageConfig", packageConfig); + params.put("javadocConfig", globalConfig.getJavadocConfig()); params.put("controllerConfig", globalConfig.getControllerConfig()); strategyConfig.getTemplateEngine().generate(params, templatePath, controllerJavaFile); 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 d43e3390..5e0c56a6 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 @@ -64,10 +64,11 @@ public class EntityGenerator implements IGenerator { } - Map params = new HashMap<>(3); + Map params = new HashMap<>(4); params.put("table", table); params.put("packageConfig", packageConfig); params.put("entityConfig", globalConfig.getEntityConfig()); + params.put("javadocConfig", globalConfig.getJavadocConfig()); strategyConfig.getTemplateEngine().generate(params, templatePath, entityJavaFile); } 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 f6b2b3f6..2511e3bc 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 @@ -64,10 +64,11 @@ public class MapperGenerator implements IGenerator { } - Map params = new HashMap<>(3); + Map params = new HashMap<>(4); params.put("table", table); params.put("packageConfig", packageConfig); params.put("mapperConfig", globalConfig.getMapperConfig()); + params.put("javadocConfig", globalConfig.getJavadocConfig()); strategyConfig.getTemplateEngine().generate(params, templatePath, mapperJavaFile); } 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 9f829e06..1a479112 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 @@ -64,10 +64,11 @@ public class ServiceGenerator implements IGenerator { } - Map params = new HashMap<>(3); + Map params = new HashMap<>(4); params.put("table", table); params.put("packageConfig", packageConfig); params.put("serviceConfig", globalConfig.getServiceConfig()); + params.put("javadocConfig", globalConfig.getJavadocConfig()); strategyConfig.getTemplateEngine().generate(params, templatePath, serviceJavaFile); } 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 8bd6a7f0..13f921b4 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 @@ -64,9 +64,10 @@ public class ServiceImplGenerator implements IGenerator { } - Map params = new HashMap<>(3); + Map params = new HashMap<>(4); params.put("table", table); params.put("packageConfig", packageConfig); + params.put("javadocConfig", globalConfig.getJavadocConfig()); params.put("serviceImplConfig", globalConfig.getServiceImplConfig()); strategyConfig.getTemplateEngine().generate(params, templatePath, serviceImplJavaFile); 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 404f304a..ebb8dc70 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 @@ -64,9 +64,10 @@ public class TableDefGenerator implements IGenerator { } - Map params = new HashMap<>(3); + Map params = new HashMap<>(4); params.put("table", table); params.put("packageConfig", packageConfig); + params.put("javadocConfig", globalConfig.getJavadocConfig()); params.put("tableDefConfig", globalConfig.getTableDefConfig()); strategyConfig.getTemplateEngine().generate(params, templatePath, tableDefJavaFile); 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 6c730159..15f49655 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/controller.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/controller.tpl @@ -10,6 +10,12 @@ import org.springframework.stereotype.Controller; import #(controllerConfig.buildSuperClassImport()) #end +/** + * #(table.getRemarks()) 控制层。 + * + * @author #(javadocConfig.getAuthor()) + * @since #(javadocConfig.getSince()) + */ #if(controllerConfig.restStyle) @RestController #else 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 a5e3a035..74621465 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl @@ -4,7 +4,12 @@ package #(packageConfig.entityPackage); import #(importClass); #end -#(table.buildRemarks()) +/** + * #(table.getRemarks()) 实体类。 + * + * @author #(javadocConfig.getAuthor()) + * @since #(javadocConfig.getSince()) + */ #(table.buildTableAnnotation()) public class #(table.buildEntityClassName())#(table.buildExtends())#(table.buildImplements()) { #for(column: table.columns) 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 227a59d6..724c81bc 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/mapper.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/mapper.tpl @@ -3,6 +3,12 @@ package #(packageConfig.mapperPackage); import #(mapperConfig.buildSuperClassImport()); import #(packageConfig.entityPackage).#(table.buildEntityClassName()); +/** + * #(table.getRemarks()) 映射层。 + * + * @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/service.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/service.tpl index 7b08c377..678cf3a5 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/service.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/service.tpl @@ -3,6 +3,12 @@ package #(packageConfig.servicePackage); import #(serviceConfig.buildSuperClassImport()); import #(packageConfig.entityPackage).#(table.buildEntityClassName()); +/** + * #(table.getRemarks()) 服务层。 + * + * @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 7de44a69..a2dcdf11 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/serviceImpl.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/serviceImpl.tpl @@ -6,6 +6,12 @@ import #(packageConfig.mapperPackage).#(table.buildMapperClassName()); import #(packageConfig.servicePackage).#(table.buildServiceClassName()); import org.springframework.stereotype.Service; +/** + * #(table.getRemarks()) 服务层实现。 + * + * @author #(javadocConfig.getAuthor()) + * @since #(javadocConfig.getSince()) + */ @Service public class #(table.buildServiceImplClassName()) extends #(serviceImplConfig.buildSuperClassName())<#(table.buildMapperClassName()), #(table.buildEntityClassName())> implements #(table.buildServiceClassName()) { 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 8c40f9cf..23b5f031 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/tableDef.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/tableDef.tpl @@ -3,7 +3,12 @@ package #(packageConfig.tableDefPackage); import com.mybatisflex.core.query.QueryColumn; import com.mybatisflex.core.table.TableDef; - +/** + * #(table.getRemarks()) 表定义层。 + * + * @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 856d9247..5b0475f3 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 @@ -22,6 +22,8 @@ import com.mybatisflex.codegen.config.TableConfig; import com.zaxxer.hikari.HikariDataSource; import org.junit.Test; +import java.util.function.Function; + public class GeneratorTest { @@ -95,6 +97,14 @@ public class GeneratorTest { GlobalConfig globalConfig = new GlobalConfig(); + //用户信息表,用于存放用户信息。 -> 用户信息 + Function format = (e) -> e.split(",")[0].replace("表", ""); + + //设置注解生成配置 + globalConfig.getJavadocConfig() + .setAuthor("王帅") + .setTableRemarkFormat(format); + //设置生成文件目录和根包 globalConfig.getPackageConfig() .setSourceDir(System.getProperty("user.dir") + "/src/test/java") From 04c6a363b983ffeca50ca62fdc60593c97c80df6 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 17 May 2023 14:25:34 +0800 Subject: [PATCH 07/28] =?UTF-8?q?refactor:=20=E6=B3=A8=E9=87=8A=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E7=94=B1=20remarks=20=E6=94=B9=E4=B8=BA=20comment=20?= =?UTF-8?q?=EF=BC=88ALTER=20TABLE=20`user`=20COMMENT=20=3D=20'=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E4=BF=A1=E6=81=AF=E8=A1=A8';=EF=BC=89=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/codegen/Generator.java | 2 +- .../codegen/config/JavadocConfig.java | 12 ++++++------ .../codegen/dialect/JdbcDialect.java | 2 +- .../com/mybatisflex/codegen/entity/Column.java | 18 +++++++++--------- .../com/mybatisflex/codegen/entity/Table.java | 12 ++++++------ .../resources/templates/enjoy/controller.tpl | 2 +- .../main/resources/templates/enjoy/entity.tpl | 4 ++-- .../main/resources/templates/enjoy/mapper.tpl | 2 +- .../main/resources/templates/enjoy/service.tpl | 2 +- .../resources/templates/enjoy/serviceImpl.tpl | 2 +- .../resources/templates/enjoy/tableDef.tpl | 2 +- .../codegen/test/GeneratorTest.java | 2 +- 12 files changed, 31 insertions(+), 31 deletions(-) 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 65af4dde..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 @@ -100,7 +100,7 @@ public class Generator { table.setName(tableName); String remarks = rs.getString("REMARKS"); - table.setRemarks(remarks); + table.setComment(remarks); buildPrimaryKey(table); 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 index 5c25940a..85b81850 100644 --- 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 @@ -27,7 +27,7 @@ public class JavadocConfig { /** * 表名格式化。 */ - private Function tableRemarkFormat = Function.identity(); + private Function tableCommentFormat = Function.identity(); public String getAuthor() { return author; @@ -53,15 +53,15 @@ public class JavadocConfig { } public String formatTableComment(String comment) { - return tableRemarkFormat.apply(comment); + return tableCommentFormat.apply(comment); } - public Function getTableRemarkFormat() { - return tableRemarkFormat; + public Function getTableCommentFormat() { + return tableCommentFormat; } - public JavadocConfig setTableRemarkFormat(Function tableRemarkFormat) { - this.tableRemarkFormat = tableRemarkFormat; + public JavadocConfig setTableCommentFormat(Function tableCommentFormat) { + this.tableCommentFormat = tableCommentFormat; return this; } 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 263f3cc5..001b0005 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 @@ -25,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<>(); @@ -40,12 +40,12 @@ public class Table { this.name = name; } - public String getRemarks() { - return globalConfig.getJavadocConfig().formatTableComment(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; } @@ -295,7 +295,7 @@ public class Table { public String toString() { return "Table{" + "name='" + name + '\'' + - ", remarks='" + remarks + '\'' + + ", remarks='" + comment + '\'' + ", primaryKeys='" + primaryKeys + '\'' + ", columns=" + columns + '}'; 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 15f49655..338c0811 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/controller.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/controller.tpl @@ -11,7 +11,7 @@ import #(controllerConfig.buildSuperClassImport()) #end /** - * #(table.getRemarks()) 控制层。 + * #(table.getComment()) 控制层。 * * @author #(javadocConfig.getAuthor()) * @since #(javadocConfig.getSince()) 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 74621465..14fbd65b 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl @@ -5,7 +5,7 @@ import #(importClass); #end /** - * #(table.getRemarks()) 实体类。 + * #(table.getComment()) 实体类。 * * @author #(javadocConfig.getAuthor()) * @since #(javadocConfig.getSince()) @@ -14,7 +14,7 @@ import #(importClass); public class #(table.buildEntityClassName())#(table.buildExtends())#(table.buildImplements()) { #for(column: table.columns) - #(column.buildRemarks()) + #(column.buildComment()) #(column.buildAnnotations())private #(column.propertySimpleType) #(column.property); #end 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 724c81bc..24040c98 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/mapper.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/mapper.tpl @@ -4,7 +4,7 @@ import #(mapperConfig.buildSuperClassImport()); import #(packageConfig.entityPackage).#(table.buildEntityClassName()); /** - * #(table.getRemarks()) 映射层。 + * #(table.getComment()) 映射层。 * * @author #(javadocConfig.getAuthor()) * @since #(javadocConfig.getSince()) 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 678cf3a5..79d3909a 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/service.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/service.tpl @@ -4,7 +4,7 @@ import #(serviceConfig.buildSuperClassImport()); import #(packageConfig.entityPackage).#(table.buildEntityClassName()); /** - * #(table.getRemarks()) 服务层。 + * #(table.getComment()) 服务层。 * * @author #(javadocConfig.getAuthor()) * @since #(javadocConfig.getSince()) 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 a2dcdf11..c5b45e48 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/serviceImpl.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/serviceImpl.tpl @@ -7,7 +7,7 @@ import #(packageConfig.servicePackage).#(table.buildServiceClassName()); import org.springframework.stereotype.Service; /** - * #(table.getRemarks()) 服务层实现。 + * #(table.getComment()) 服务层实现。 * * @author #(javadocConfig.getAuthor()) * @since #(javadocConfig.getSince()) 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 23b5f031..513eaca2 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/tableDef.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/tableDef.tpl @@ -4,7 +4,7 @@ import com.mybatisflex.core.query.QueryColumn; import com.mybatisflex.core.table.TableDef; /** - * #(table.getRemarks()) 表定义层。 + * #(table.getComment()) 表定义层。 * * @author #(javadocConfig.getAuthor()) * @since #(javadocConfig.getSince()) 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 5b0475f3..24d51ba7 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 @@ -103,7 +103,7 @@ public class GeneratorTest { //设置注解生成配置 globalConfig.getJavadocConfig() .setAuthor("王帅") - .setTableRemarkFormat(format); + .setTableCommentFormat(format); //设置生成文件目录和根包 globalConfig.getPackageConfig() From 01f5a22a576fe15693e7a5c8618702edf38202cb Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 17 May 2023 14:58:48 +0800 Subject: [PATCH 08/28] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E7=94=9F?= =?UTF-8?q?=E6=88=90=20mapperXml=20=E6=96=87=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/GlobalConfig.java | 22 ++++++ .../codegen/config/MapperXmlConfig.java | 39 ++++++++++ .../codegen/config/PackageConfig.java | 19 ++++- .../codegen/constant/TemplateConst.java | 2 + .../com/mybatisflex/codegen/entity/Table.java | 13 ++++ .../codegen/generator/GeneratorFactory.java | 1 + .../generator/impl/MapperXmlGenerator.java | 72 +++++++++++++++++++ .../resources/templates/enjoy/mapperXml.tpl | 7 ++ 8 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperXmlConfig.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperXmlGenerator.java create mode 100644 mybatis-flex-codegen/src/main/resources/templates/enjoy/mapperXml.tpl 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 107d1315..c6b482d5 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 @@ -41,6 +41,7 @@ public class GlobalConfig { private ServiceImplConfig serviceImplConfig; private ControllerConfig controllerConfig; private TableDefConfig tableDefConfig; + private MapperXmlConfig mapperXmlConfig; // === 其他配置 === @@ -54,6 +55,7 @@ public class GlobalConfig { private boolean serviceImplGenerateEnable; private boolean controllerGenerateEnable; private boolean tableDefGenerateEnable; + private boolean mapperXmlGenerateEnable; public GlobalConfig() { this.javadocConfig = new JavadocConfig(); @@ -115,6 +117,13 @@ public class GlobalConfig { return tableDefConfig; } + public MapperXmlConfig getMapperXmlConfig() { + if (mapperXmlConfig == null) { + mapperXmlConfig = new MapperXmlConfig(); + } + return mapperXmlConfig; + } + public EntityConfig enableEntity() { entityGenerateEnable = true; return getEntityConfig(); @@ -145,6 +154,11 @@ public class GlobalConfig { return getTableDefConfig(); } + public MapperXmlConfig enableMapperXml() { + mapperXmlGenerateEnable = true; + return mapperXmlConfig; + } + public void disableEntity() { entityGenerateEnable = false; } @@ -169,6 +183,10 @@ public class GlobalConfig { tableDefGenerateEnable = false; } + public void disableMapperXml() { + mapperXmlGenerateEnable = false; + } + public boolean isEntityGenerateEnable() { return entityGenerateEnable; } @@ -193,6 +211,10 @@ public class GlobalConfig { return tableDefGenerateEnable; } + public boolean isMapperXmlGenerateEnable() { + return mapperXmlGenerateEnable; + } + public void addCustomConfig(String key, Object value) { if (customConfig == null) { customConfig = new HashMap<>(); 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..07a43d89 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/MapperXmlConfig.java @@ -0,0 +1,39 @@ +package com.mybatisflex.codegen.config; + +/** + * 生成 MapperXml 的配置。 + * + * @author 王帅 + * @since 2023-05-17 + */ +public class MapperXmlConfig { + + /** + * MapperXml 文件的前缀。 + */ + private String filePrefix = ""; + + /** + * MapperXml 文件的后缀。 + */ + private String fileSuffix = "Mapper"; + + 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; + } + +} \ 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 index 5b24be30..6ff36607 100644 --- 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 @@ -51,8 +51,13 @@ public class PackageConfig { */ private String tableDefPackage; + /** + * MapperXml 文件所在位置。 + */ + private String mapperXmlPath; + public String getSourceDir() { - if (sourceDir == null || StringUtil.isBlank(sourceDir)) { + if (StringUtil.isBlank(sourceDir)) { return System.getProperty("user.dir") + "/src/main/java"; } return sourceDir; @@ -144,4 +149,16 @@ public class PackageConfig { return this; } + public String getMapperXmlPath() { + if (StringUtil.isBlank(mapperXmlPath)) { + return getSourceDir().concat("/resources/mapper"); + } + return mapperXmlPath; + } + + 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/constant/TemplateConst.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/TemplateConst.java index 9e77020a..bbd01f07 100644 --- 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 @@ -14,6 +14,8 @@ public final class TemplateConst { 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"; + private TemplateConst() { } 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 001b0005..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 @@ -198,6 +198,19 @@ public class Table { + tableDefConfig.getClassSuffix(); } + /** + * 构建 MapperXml 的文件名称 + * + * @return fileName + */ + public String buildMapperXmlFileName() { + String tableDefJavaFileName = getEntityJavaFileName(); + MapperXmlConfig mapperXmlConfig = globalConfig.getMapperXmlConfig(); + return mapperXmlConfig.getFilePrefix() + + tableDefJavaFileName + + mapperXmlConfig.getFileSuffix(); + } + public String buildExtends() { EntityConfig entityConfig = globalConfig.getEntityConfig(); if (entityConfig.getSupperClass() != null) { 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..e44fcd6e 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 @@ -32,6 +32,7 @@ public class GeneratorFactory { registerGenerator("serviceImpl", new ServiceImplGenerator()); registerGenerator("controller", new ControllerGenerator()); registerGenerator("tableDef", new TableDefGenerator()); + registerGenerator("mapperXml", new MapperXmlGenerator()); } 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..2713c721 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/MapperXmlGenerator.java @@ -0,0 +1,72 @@ +/** + * 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.StrategyConfig; +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 final 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(); + StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); + + File mapperXmlFile = new File(packageConfig.getMapperXmlPath() + "/" + + table.buildMapperXmlFileName() + ".xml"); + + + if (mapperXmlFile.exists() && !strategyConfig.isOverwriteEnable()) { + return; + } + + + Map params = new HashMap<>(2); + params.put("table", table); + params.put("packageConfig", packageConfig); + + strategyConfig.getTemplateEngine().generate(params, templatePath, mapperXmlFile); + } +} 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 From 328086cec1d2c7346f2664bf6c81f2ceb84c9c05 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 17 May 2023 20:15:11 +0800 Subject: [PATCH 09/28] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E6=A8=A1=E6=9D=BF=E6=96=87=E4=BB=B6=E4=BD=8D=E7=BD=AE?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E7=94=A8=E6=8C=87=E5=AE=9A=E7=9A=84=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E7=94=9F=E6=88=90=E6=96=87=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/GlobalConfig.java | 6 + .../codegen/config/StrategyConfig.java | 2 +- .../codegen/config/TemplateConfig.java | 157 ++++++++++++++++++ .../codegen/constant/GenTypeConst.java | 36 ++++ .../codegen/generator/GeneratorFactory.java | 19 ++- .../codegen/generator/IGenerator.java | 28 ++++ .../generator/impl/ControllerGenerator.java | 13 +- .../generator/impl/EntityGenerator.java | 13 +- .../generator/impl/MapperGenerator.java | 13 +- .../generator/impl/MapperXmlGenerator.java | 13 +- .../generator/impl/ServiceGenerator.java | 13 +- .../generator/impl/ServiceImplGenerator.java | 13 +- .../generator/impl/TableDefGenerator.java | 13 +- .../template/{ => impl}/EnjoyTemplate.java | 23 ++- 14 files changed, 338 insertions(+), 24 deletions(-) create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/TemplateConfig.java create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/GenTypeConst.java rename mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/template/{ => impl}/EnjoyTemplate.java (67%) 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 c6b482d5..2b1de937 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 @@ -32,6 +32,7 @@ public class GlobalConfig { private final JavadocConfig javadocConfig; private final PackageConfig packageConfig; private final StrategyConfig strategyConfig; + private final TemplateConfig templateConfig; // === 可选配置 === @@ -61,6 +62,7 @@ public class GlobalConfig { this.javadocConfig = new JavadocConfig(); this.packageConfig = new PackageConfig(); this.strategyConfig = new StrategyConfig(); + this.templateConfig = new TemplateConfig(); } public JavadocConfig getJavadocConfig() { @@ -75,6 +77,10 @@ public class GlobalConfig { return strategyConfig; } + public TemplateConfig getTemplateConfig() { + return templateConfig; + } + public EntityConfig getEntityConfig() { if (entityConfig == null) { entityConfig = new EntityConfig(); 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 index 0b1c53ed..d538e0be 100644 --- 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 @@ -1,7 +1,7 @@ package com.mybatisflex.codegen.config; -import com.mybatisflex.codegen.template.EnjoyTemplate; import com.mybatisflex.codegen.template.ITemplate; +import com.mybatisflex.codegen.template.impl.EnjoyTemplate; import java.util.HashMap; import java.util.HashSet; 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..915b1550 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/constant/GenTypeConst.java @@ -0,0 +1,36 @@ +/** + * 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"; + private GenTypeConst() { + } + +} \ No newline at end of file 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 e44fcd6e..ce5b4e85 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 @@ -15,6 +15,7 @@ */ package com.mybatisflex.codegen.generator; +import com.mybatisflex.codegen.constant.GenTypeConst; import com.mybatisflex.codegen.generator.impl.*; import java.util.Collection; @@ -26,13 +27,17 @@ 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("mapperXml", new MapperXmlGenerator()); + 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()); + } + + 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..4d653e85 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 @@ -18,6 +18,34 @@ package com.mybatisflex.codegen.generator; import com.mybatisflex.codegen.config.GlobalConfig; import com.mybatisflex.codegen.entity.Table; +/** + * 文件生成器接口。 + * + * @author Michael Yang + * @author 王帅 + */ public interface IGenerator { + + /** + * 获取模板文件位置。 + * + * @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 cc03dc06..885cc130 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 @@ -34,7 +34,7 @@ import java.util.Map; */ public class ControllerGenerator implements IGenerator { - private final String templatePath; + private String templatePath; public ControllerGenerator() { this(TemplateConst.CONTROLLER); @@ -70,6 +70,15 @@ public class ControllerGenerator implements IGenerator { params.put("javadocConfig", globalConfig.getJavadocConfig()); params.put("controllerConfig", globalConfig.getControllerConfig()); - strategyConfig.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 5e0c56a6..e2a11003 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 @@ -34,7 +34,7 @@ import java.util.Map; */ public class EntityGenerator implements IGenerator { - private final String templatePath; + private String templatePath; public EntityGenerator() { this(TemplateConst.ENTITY); @@ -70,6 +70,15 @@ public class EntityGenerator implements IGenerator { params.put("entityConfig", globalConfig.getEntityConfig()); params.put("javadocConfig", globalConfig.getJavadocConfig()); - strategyConfig.getTemplateEngine().generate(params, templatePath, entityJavaFile); + 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 2511e3bc..6720a538 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 @@ -34,7 +34,7 @@ import java.util.Map; */ public class MapperGenerator implements IGenerator { - private final String templatePath; + private String templatePath; public MapperGenerator() { this(TemplateConst.MAPPER); @@ -70,6 +70,15 @@ public class MapperGenerator implements IGenerator { params.put("mapperConfig", globalConfig.getMapperConfig()); params.put("javadocConfig", globalConfig.getJavadocConfig()); - strategyConfig.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 index 2713c721..d309485d 100644 --- 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 @@ -34,7 +34,7 @@ import java.util.Map; */ public class MapperXmlGenerator implements IGenerator { - private final String templatePath; + private String templatePath; public MapperXmlGenerator() { this(TemplateConst.MAPPER_XML); @@ -67,6 +67,15 @@ public class MapperXmlGenerator implements IGenerator { params.put("table", table); params.put("packageConfig", packageConfig); - strategyConfig.getTemplateEngine().generate(params, templatePath, mapperXmlFile); + 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/ServiceGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ServiceGenerator.java index 1a479112..d2242d00 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 @@ -34,7 +34,7 @@ import java.util.Map; */ public class ServiceGenerator implements IGenerator { - private final String templatePath; + private String templatePath; public ServiceGenerator() { this(TemplateConst.SERVICE); @@ -70,6 +70,15 @@ public class ServiceGenerator implements IGenerator { params.put("serviceConfig", globalConfig.getServiceConfig()); params.put("javadocConfig", globalConfig.getJavadocConfig()); - strategyConfig.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 13f921b4..ebe9b2e5 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 @@ -34,7 +34,7 @@ import java.util.Map; */ public class ServiceImplGenerator implements IGenerator { - private final String templatePath; + private String templatePath; public ServiceImplGenerator() { this(TemplateConst.SERVICE_IMPL); @@ -70,6 +70,15 @@ public class ServiceImplGenerator implements IGenerator { params.put("javadocConfig", globalConfig.getJavadocConfig()); params.put("serviceImplConfig", globalConfig.getServiceImplConfig()); - strategyConfig.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 ebb8dc70..f35e1402 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 @@ -34,7 +34,7 @@ import java.util.Map; */ public class TableDefGenerator implements IGenerator { - private final String templatePath; + private String templatePath; public TableDefGenerator() { this(TemplateConst.TABLE_DEF); @@ -70,6 +70,15 @@ public class TableDefGenerator implements IGenerator { params.put("javadocConfig", globalConfig.getJavadocConfig()); params.put("tableDefConfig", globalConfig.getTableDefConfig()); - strategyConfig.getTemplateEngine().generate(params, templatePath, tableDefJavaFile); + 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); + } + + } + } From ed1a3c0938940872be3d09d192a955638aceaa42 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 17 May 2023 20:20:46 +0800 Subject: [PATCH 10/28] =?UTF-8?q?update:=20=E6=9B=B4=E6=96=B0=E7=89=88?= =?UTF-8?q?=E6=9D=83=E4=BF=A1=E6=81=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/JavadocConfig.java | 16 ++++++++++++++++ .../codegen/config/MapperXmlConfig.java | 16 ++++++++++++++++ .../codegen/config/PackageConfig.java | 16 ++++++++++++++++ .../codegen/config/StrategyConfig.java | 16 ++++++++++++++++ .../codegen/constant/TemplateConst.java | 16 ++++++++++++++++ 5 files changed, 80 insertions(+) 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 index 85b81850..6784a14e 100644 --- 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 @@ -1,3 +1,19 @@ +/* + * 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; 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 index 07a43d89..68acc26c 100644 --- 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 @@ -1,3 +1,19 @@ +/* + * 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; /** 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 index 6ff36607..ebfd4e83 100644 --- 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 @@ -1,3 +1,19 @@ +/* + * 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; 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 index d538e0be..974dfdca 100644 --- 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 @@ -1,3 +1,19 @@ +/* + * 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.ITemplate; 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 index bbd01f07..83701616 100644 --- 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 @@ -1,3 +1,19 @@ +/* + * 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; /** From 0cc1ba76d8b8fc03090f3804c904ad1677cf0a3b Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 17 May 2023 20:49:31 +0800 Subject: [PATCH 11/28] =?UTF-8?q?feat:=20=E7=94=9F=E6=88=90=E7=9A=84?= =?UTF-8?q?=E6=AF=8F=E7=B1=BB=E6=96=87=E4=BB=B6=EF=BC=8C=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=98=AF=E5=90=A6=E8=A6=86=E7=9B=96=E6=BA=90?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/ControllerConfig.java | 42 ++++++++++++------- .../codegen/config/EntityConfig.java | 42 ++++++++++++------- .../codegen/config/MapperConfig.java | 42 ++++++++++++------- .../codegen/config/MapperXmlConfig.java | 14 +++++++ .../codegen/config/ServiceConfig.java | 42 ++++++++++++------- .../codegen/config/ServiceImplConfig.java | 42 ++++++++++++------- .../codegen/config/StrategyConfig.java | 34 --------------- .../codegen/config/TableDefConfig.java | 42 ++++++++++++------- .../generator/impl/ControllerGenerator.java | 38 ++++++++--------- .../generator/impl/EntityGenerator.java | 36 ++++++++-------- .../generator/impl/MapperGenerator.java | 36 ++++++++-------- .../generator/impl/MapperXmlGenerator.java | 34 +++++++-------- .../generator/impl/ServiceGenerator.java | 36 ++++++++-------- .../generator/impl/ServiceImplGenerator.java | 36 ++++++++-------- .../generator/impl/TableDefGenerator.java | 36 ++++++++-------- 15 files changed, 308 insertions(+), 244 deletions(-) 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 index 2dd47e76..3cd804f7 100644 --- 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 @@ -1,17 +1,17 @@ -/** - * 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; @@ -39,6 +39,11 @@ public class ControllerConfig { */ private Class supperClass; + /** + * 是否覆盖之前生成的文件。 + */ + private boolean overwriteEnable; + /** * 生成 REST 风格的 Controller。 */ @@ -79,6 +84,15 @@ public class ControllerConfig { return this; } + public boolean isOverwriteEnable() { + return overwriteEnable; + } + + public ControllerConfig setOverwriteEnable(boolean overwriteEnable) { + this.overwriteEnable = overwriteEnable; + return this; + } + public boolean isRestStyle() { return restStyle; } 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 index 02e3ce1f..fbf2d8bb 100644 --- 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 @@ -1,17 +1,17 @@ -/** - * 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; @@ -41,6 +41,11 @@ public class EntityConfig { */ private Class supperClass; + /** + * 是否覆盖之前生成的文件。 + */ + private boolean overwriteEnable; + /** * Entity 默认实现的接口。 */ @@ -78,6 +83,15 @@ public class EntityConfig { return this; } + public boolean isOverwriteEnable() { + return overwriteEnable; + } + + public EntityConfig setOverwriteEnable(boolean overwriteEnable) { + this.overwriteEnable = overwriteEnable; + return this; + } + public Class[] getImplInterfaces() { return implInterfaces; } 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 index 2aed21b8..86a3f9c5 100644 --- 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 @@ -1,17 +1,17 @@ -/** - * 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; @@ -41,6 +41,11 @@ public class MapperConfig { */ private Class supperClass = BaseMapper.class; + /** + * 是否覆盖之前生成的文件。 + */ + private boolean overwriteEnable; + public String buildSuperClassImport() { return supperClass.getName(); } @@ -76,4 +81,13 @@ public class MapperConfig { 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 index 68acc26c..4b27ee0d 100644 --- 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 @@ -34,6 +34,11 @@ public class MapperXmlConfig { */ private String fileSuffix = "Mapper"; + /** + * 是否覆盖之前生成的文件。 + */ + private boolean overwriteEnable; + public String getFilePrefix() { return filePrefix; } @@ -52,4 +57,13 @@ public class MapperXmlConfig { 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/ServiceConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ServiceConfig.java index c3084b0f..174db0c9 100644 --- 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 @@ -1,17 +1,17 @@ -/** - * 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; @@ -41,6 +41,11 @@ public class ServiceConfig { */ private Class supperClass = IService.class; + /** + * 是否覆盖之前生成的文件。 + */ + private boolean overwriteEnable; + public String buildSuperClassImport() { return supperClass.getName(); } @@ -76,4 +81,13 @@ public class ServiceConfig { 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 index 972d77c4..274c2f90 100644 --- 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 @@ -1,17 +1,17 @@ -/** - * 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; @@ -41,6 +41,11 @@ public class ServiceImplConfig { */ private Class supperClass = ServiceImpl.class; + /** + * 是否覆盖之前生成的文件。 + */ + private boolean overwriteEnable; + public String buildSuperClassImport() { return supperClass.getName(); } @@ -76,4 +81,13 @@ public class ServiceImplConfig { 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 index 974dfdca..4ee35977 100644 --- 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 @@ -16,9 +16,6 @@ package com.mybatisflex.codegen.config; -import com.mybatisflex.codegen.template.ITemplate; -import com.mybatisflex.codegen.template.impl.EnjoyTemplate; - import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -33,11 +30,6 @@ import java.util.Set; @SuppressWarnings("unused") public class StrategyConfig { - /** - * 使用哪个模板引擎来生成代码。 - */ - protected ITemplate templateEngine; - /** * 数据库表前缀,多个前缀用英文逗号(,) 隔开。 */ @@ -58,11 +50,6 @@ public class StrategyConfig { */ private boolean generateForView; - /** - * 是否覆盖之前生成的文件。 - */ - private boolean overwriteEnable; - /** * 单独为某张表添加独立的配置。 */ @@ -185,18 +172,6 @@ public class StrategyConfig { return false; } - public ITemplate getTemplateEngine() { - if (templateEngine == null) { - templateEngine = new EnjoyTemplate(); - } - return templateEngine; - } - - public StrategyConfig setTemplateEngine(ITemplate templateEngine) { - this.templateEngine = templateEngine; - return this; - } - public String getTablePrefix() { return tablePrefix; } @@ -233,15 +208,6 @@ public class StrategyConfig { return this; } - public boolean isOverwriteEnable() { - return overwriteEnable; - } - - public StrategyConfig setOverwriteEnable(boolean overwriteEnable) { - this.overwriteEnable = overwriteEnable; - return this; - } - public Map getTableConfigMap() { return tableConfigMap; } 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 index dd0abb9c..c5bd3154 100644 --- 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 @@ -1,17 +1,17 @@ -/** - * 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; @@ -34,6 +34,11 @@ public class TableDefConfig { */ private String classSuffix = "Def"; + /** + * 是否覆盖之前生成的文件。 + */ + private boolean overwriteEnable; + public String getClassPrefix() { return classPrefix; } @@ -52,4 +57,13 @@ public class TableDefConfig { 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/generator/impl/ControllerGenerator.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/ControllerGenerator.java index 885cc130..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,23 +1,23 @@ -/** - * 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.config.StrategyConfig; import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -52,14 +52,14 @@ public class ControllerGenerator implements IGenerator { } PackageConfig packageConfig = globalConfig.getPackageConfig(); - StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); - + ControllerConfig controllerConfig = globalConfig.getControllerConfig(); + String controllerPackagePath = packageConfig.getControllerPackage().replace(".", "/"); File controllerJavaFile = new File(packageConfig.getSourceDir(), controllerPackagePath + "/" + table.buildControllerClassName() + ".java"); - if (controllerJavaFile.exists() && strategyConfig.isOverwriteEnable()) { + if (controllerJavaFile.exists() && controllerConfig.isOverwriteEnable()) { return; } @@ -67,8 +67,8 @@ public class ControllerGenerator implements IGenerator { Map params = new HashMap<>(4); params.put("table", table); params.put("packageConfig", packageConfig); + params.put("controllerConfig", controllerConfig); params.put("javadocConfig", globalConfig.getJavadocConfig()); - params.put("controllerConfig", globalConfig.getControllerConfig()); globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, controllerJavaFile); } 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 e2a11003..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,23 +1,23 @@ -/** - * 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.config.StrategyConfig; import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -52,22 +52,22 @@ public class EntityGenerator implements IGenerator { } PackageConfig packageConfig = globalConfig.getPackageConfig(); - StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); + EntityConfig entityConfig = globalConfig.getEntityConfig(); String entityPackagePath = packageConfig.getEntityPackage().replace(".", "/"); File entityJavaFile = new File(packageConfig.getSourceDir(), entityPackagePath + "/" + table.buildEntityClassName() + ".java"); - if (entityJavaFile.exists() && !strategyConfig.isOverwriteEnable()) { + 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("entityConfig", globalConfig.getEntityConfig()); params.put("javadocConfig", globalConfig.getJavadocConfig()); globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, entityJavaFile); 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 6720a538..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,23 +1,23 @@ -/** - * 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.config.StrategyConfig; import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -52,22 +52,22 @@ public class MapperGenerator implements IGenerator { } PackageConfig packageConfig = globalConfig.getPackageConfig(); - StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); + MapperConfig mapperConfig = globalConfig.getMapperConfig(); String mapperPackagePath = packageConfig.getMapperPackage().replace(".", "/"); File mapperJavaFile = new File(packageConfig.getSourceDir(), mapperPackagePath + "/" + table.buildMapperClassName() + ".java"); - if (mapperJavaFile.exists() && !strategyConfig.isOverwriteEnable()) { + if (mapperJavaFile.exists() && !mapperConfig.isOverwriteEnable()) { return; } Map params = new HashMap<>(4); params.put("table", table); + params.put("mapperConfig", mapperConfig); params.put("packageConfig", packageConfig); - params.put("mapperConfig", globalConfig.getMapperConfig()); params.put("javadocConfig", globalConfig.getJavadocConfig()); globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, mapperJavaFile); 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 index d309485d..c5667d13 100644 --- 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 @@ -1,23 +1,23 @@ -/** - * 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.MapperXmlConfig; import com.mybatisflex.codegen.config.PackageConfig; -import com.mybatisflex.codegen.config.StrategyConfig; import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -52,13 +52,13 @@ public class MapperXmlGenerator implements IGenerator { } PackageConfig packageConfig = globalConfig.getPackageConfig(); - StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); + MapperXmlConfig mapperXmlConfig = globalConfig.getMapperXmlConfig(); File mapperXmlFile = new File(packageConfig.getMapperXmlPath() + "/" + table.buildMapperXmlFileName() + ".xml"); - if (mapperXmlFile.exists() && !strategyConfig.isOverwriteEnable()) { + if (mapperXmlFile.exists() && !mapperXmlConfig.isOverwriteEnable()) { return; } 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 d2242d00..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,23 +1,23 @@ -/** - * 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.StrategyConfig; +import com.mybatisflex.codegen.config.ServiceConfig; import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -52,22 +52,22 @@ public class ServiceGenerator implements IGenerator { } PackageConfig packageConfig = globalConfig.getPackageConfig(); - StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); + ServiceConfig serviceConfig = globalConfig.getServiceConfig(); String servicePackagePath = packageConfig.getServicePackage().replace(".", "/"); File serviceJavaFile = new File(packageConfig.getSourceDir(), servicePackagePath + "/" + table.buildServiceClassName() + ".java"); - if (serviceJavaFile.exists() && !strategyConfig.isOverwriteEnable()) { + if (serviceJavaFile.exists() && !serviceConfig.isOverwriteEnable()) { return; } Map params = new HashMap<>(4); params.put("table", table); + params.put("serviceConfig", serviceConfig); params.put("packageConfig", packageConfig); - params.put("serviceConfig", globalConfig.getServiceConfig()); params.put("javadocConfig", globalConfig.getJavadocConfig()); globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, serviceJavaFile); 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 ebe9b2e5..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,23 +1,23 @@ -/** - * 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.StrategyConfig; +import com.mybatisflex.codegen.config.ServiceImplConfig; import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -52,14 +52,14 @@ public class ServiceImplGenerator implements IGenerator { } PackageConfig packageConfig = globalConfig.getPackageConfig(); - StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); + ServiceImplConfig serviceImplConfig = globalConfig.getServiceImplConfig(); String serviceImplPackagePath = packageConfig.getServiceImplPackage().replace(".", "/"); File serviceImplJavaFile = new File(packageConfig.getSourceDir(), serviceImplPackagePath + "/" + table.buildServiceImplClassName() + ".java"); - if (serviceImplJavaFile.exists() && !strategyConfig.isOverwriteEnable()) { + if (serviceImplJavaFile.exists() && !serviceImplConfig.isOverwriteEnable()) { return; } @@ -67,8 +67,8 @@ public class ServiceImplGenerator implements IGenerator { Map params = new HashMap<>(4); params.put("table", table); params.put("packageConfig", packageConfig); + params.put("serviceImplConfig", serviceImplConfig); params.put("javadocConfig", globalConfig.getJavadocConfig()); - params.put("serviceImplConfig", globalConfig.getServiceImplConfig()); globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, serviceImplJavaFile); } 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 f35e1402..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,23 +1,23 @@ -/** - * 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.StrategyConfig; +import com.mybatisflex.codegen.config.TableDefConfig; import com.mybatisflex.codegen.constant.TemplateConst; import com.mybatisflex.codegen.entity.Table; import com.mybatisflex.codegen.generator.IGenerator; @@ -52,14 +52,14 @@ public class TableDefGenerator implements IGenerator { } PackageConfig packageConfig = globalConfig.getPackageConfig(); - StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); + TableDefConfig tableDefConfig = globalConfig.getTableDefConfig(); String tableDefPackagePath = packageConfig.getTableDefPackage().replace(".", "/"); File tableDefJavaFile = new File(packageConfig.getSourceDir(), tableDefPackagePath + "/" + table.buildTableDefClassName() + ".java"); - if (tableDefJavaFile.exists() && !strategyConfig.isOverwriteEnable()) { + if (tableDefJavaFile.exists() && !tableDefConfig.isOverwriteEnable()) { return; } @@ -67,8 +67,8 @@ public class TableDefGenerator implements IGenerator { Map params = new HashMap<>(4); params.put("table", table); params.put("packageConfig", packageConfig); + params.put("tableDefConfig", tableDefConfig); params.put("javadocConfig", globalConfig.getJavadocConfig()); - params.put("tableDefConfig", globalConfig.getTableDefConfig()); globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, tableDefJavaFile); } From 8fe91f3ec6808c283b5e07c7e108dfb982419df6 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 17 May 2023 22:26:57 +0800 Subject: [PATCH 12/28] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20package-info?= =?UTF-8?q?=20=E6=96=87=E4=BB=B6=E7=94=9F=E6=88=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/GlobalConfig.java | 41 ++++--- .../codegen/config/JavadocConfig.java | 18 +++ .../codegen/constant/GenTypeConst.java | 31 +++--- .../codegen/constant/TemplateConst.java | 3 +- .../codegen/generator/GeneratorFactory.java | 29 ++--- .../generator/impl/PackageInfoGenerator.java | 104 ++++++++++++++++++ .../templates/enjoy/package-info.tpl | 6 + 7 files changed, 189 insertions(+), 43 deletions(-) create mode 100644 mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/PackageInfoGenerator.java create mode 100644 mybatis-flex-codegen/src/main/resources/templates/enjoy/package-info.tpl 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 2b1de937..6e83f36b 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,17 +1,17 @@ -/** - * 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; @@ -57,6 +57,7 @@ public class GlobalConfig { private boolean controllerGenerateEnable; private boolean tableDefGenerateEnable; private boolean mapperXmlGenerateEnable; + private boolean packageInfoGenerateEnable; public GlobalConfig() { this.javadocConfig = new JavadocConfig(); @@ -165,6 +166,10 @@ public class GlobalConfig { return mapperXmlConfig; } + public void enablePackageInfo() { + packageInfoGenerateEnable = true; + } + public void disableEntity() { entityGenerateEnable = false; } @@ -193,6 +198,10 @@ public class GlobalConfig { mapperXmlGenerateEnable = false; } + public void disablePackageInfo() { + packageInfoGenerateEnable = false; + } + public boolean isEntityGenerateEnable() { return entityGenerateEnable; } @@ -221,6 +230,10 @@ public class GlobalConfig { return mapperXmlGenerateEnable; } + public boolean isPackageInfoGenerateEnable() { + return packageInfoGenerateEnable; + } + public void addCustomConfig(String key, Object value) { if (customConfig == null) { customConfig = new HashMap<>(); 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 index 6784a14e..f13fc8cf 100644 --- 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 @@ -45,6 +45,11 @@ public class JavadocConfig { */ private Function tableCommentFormat = Function.identity(); + /** + * 包注释格式化。 + */ + private Function packageCommentFormat = Function.identity(); + public String getAuthor() { return author; } @@ -81,4 +86,17 @@ public class JavadocConfig { return this; } + public String formatPackageComment(String packageName) { + return packageCommentFormat.apply(packageName); + } + + public Function getPackageCommentFormat() { + return packageCommentFormat; + } + + public JavadocConfig setPackageCommentFormat(Function packageCommentFormat) { + this.packageCommentFormat = packageCommentFormat; + 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 index 915b1550..e91969cf 100644 --- 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 @@ -1,17 +1,17 @@ -/** - * 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.constant; @@ -30,6 +30,9 @@ public class GenTypeConst { 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() { } 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 index 83701616..4051d184 100644 --- 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 @@ -24,6 +24,8 @@ package com.mybatisflex.codegen.constant; */ public final class TemplateConst { + public static final String PACKAGE_INFO = "/templates/enjoy/package-info.tpl"; + 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"; @@ -31,7 +33,6 @@ public final class TemplateConst { 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"; - private TemplateConst() { } 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 ce5b4e85..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,17 +1,17 @@ -/** - * 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; @@ -34,6 +34,7 @@ public class GeneratorFactory { 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) { 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..ba216c19 --- /dev/null +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/generator/impl/PackageInfoGenerator.java @@ -0,0 +1,104 @@ +/* + * 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.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; + +/** + * 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; + } + + PackageConfig packageConfig = globalConfig.getPackageConfig(); + + String sourceDir = packageConfig.getSourceDir(); + + Map map = new HashMap<>(6); + + if (globalConfig.isEntityGenerateEnable()) { + String entityPackage = packageConfig.getEntityPackage(); + map.put(entityPackage, getFilePath(sourceDir, entityPackage)); + } + if (globalConfig.isMapperGenerateEnable()) { + String mapperPackage = packageConfig.getMapperPackage(); + map.put(mapperPackage, getFilePath(sourceDir, mapperPackage)); + } + if (globalConfig.isServiceGenerateEnable()) { + String servicePackage = packageConfig.getServicePackage(); + map.put(servicePackage, getFilePath(sourceDir, servicePackage)); + } + if (globalConfig.isServiceImplGenerateEnable()) { + String serviceImplPackage = packageConfig.getServiceImplPackage(); + map.put(serviceImplPackage, getFilePath(sourceDir, serviceImplPackage)); + } + if (globalConfig.isControllerGenerateEnable()) { + String controllerPackage = packageConfig.getControllerPackage(); + map.put(controllerPackage, getFilePath(sourceDir, controllerPackage)); + } + if (globalConfig.isTableDefGenerateEnable()) { + String tableDefPackage = packageConfig.getTableDefPackage(); + map.put(tableDefPackage, getFilePath(sourceDir, tableDefPackage)); + } + + map.forEach((packageName, filePath) -> { + Map params = new HashMap<>(3); + params.put("packageName", packageName); + params.put("javadocConfig", globalConfig.getJavadocConfig()); + globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, filePath); + }); + } + + private File getFilePath(String sourceDir, String packageName) { + return new File(sourceDir, packageName.replace(".", "/") + "/package-info.java"); + } + + public String getTemplatePath() { + return templatePath; + } + + public void setTemplatePath(String templatePath) { + this.templatePath = templatePath; + } + +} 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..2d8bc9e6 --- /dev/null +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/package-info.tpl @@ -0,0 +1,6 @@ +/** + * #(javadocConfig.formatPackageComment(packageName)) + * + * @since #(javadocConfig.getSince()) + */ + package #(packageName); \ No newline at end of file From 15f960afefd9e48dc3712e143a5d692b581d9871 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 17 May 2023 22:39:14 +0800 Subject: [PATCH 13/28] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BB=A3=E7=A0=81=EF=BC=8C=E4=BD=9C=E4=B8=BA=E6=A0=B7?= =?UTF-8?q?=E4=BE=8B=E5=8F=82=E8=80=83=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/test/GeneratorTest.java | 65 ++++++++++++++----- 1 file changed, 47 insertions(+), 18 deletions(-) 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 24d51ba7..6393fc48 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,18 +1,19 @@ -/** - * 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; @@ -98,16 +99,37 @@ public class GeneratorTest { GlobalConfig globalConfig = new GlobalConfig(); //用户信息表,用于存放用户信息。 -> 用户信息 - Function format = (e) -> e.split(",")[0].replace("表", ""); + Function tableFormat = (e) -> e.split(",")[0].replace("表", ""); + //包注释生成 + Function packageFormat = (e) -> { + String[] s = e.split("\\."); + switch (s[s.length - 1]) { + case "entity": + return "实体类 软件包。"; + case "mapper": + return "映射层 软件包。"; + case "service": + return "服务层 软件包。"; + case "impl": + return "服务层实现 软件包。"; + case "controller": + return "控制层 软件包。"; + default: + return e; + } + }; + //设置注解生成配置 globalConfig.getJavadocConfig() .setAuthor("王帅") - .setTableCommentFormat(format); + .setTableCommentFormat(tableFormat) + .setPackageCommentFormat(packageFormat); //设置生成文件目录和根包 globalConfig.getPackageConfig() .setSourceDir(System.getProperty("user.dir") + "/src/test/java") + .setMapperXmlPath(System.getProperty("user.dir") + "/src/test/java/resources/mapper") .setBasePackage("com.test"); //设置表前缀和只生成哪些表 @@ -115,6 +137,9 @@ public class GeneratorTest { .setTablePrefix("sys_") .addGenerateTable("sys_user"); + globalConfig.getTemplateConfig() + .setEntity("D:\\Documents\\配置文件\\entity.tpl"); + //配置生成 entity globalConfig.enableEntity() .setWithLombok(true) @@ -127,9 +152,13 @@ public class GeneratorTest { //配置生成 serviceImpl globalConfig.enableServiceImpl(); //配置生成 controller - globalConfig.enableController(); + //globalConfig.enableController(); //配置生成 tableDef - globalConfig.enableTableDef(); + //globalConfig.enableTableDef(); + //配置生成 mapperXml + //globalConfig.enableMapperXml(); + //配置生成 package-info.java + globalConfig.enablePackageInfo(); //通过 datasource 和 globalConfig 创建代码生成器 Generator generator = new Generator(dataSource, globalConfig); From d674bd9400a3c5bcc7ff037880b269d5f8ffacb5 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 17 May 2023 23:15:10 +0800 Subject: [PATCH 14/28] =?UTF-8?q?feat:=20Controller=20=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/templates/enjoy/controller.tpl | 38 ++++++++++++++----- 1 file changed, 28 insertions(+), 10 deletions(-) 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 338c0811..b8d375a3 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/controller.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/controller.tpl @@ -1,17 +1,27 @@ +#set(tableComment = table.getComment()) +#set(entityClassName = table.buildEntityClassName()) +#set(serviceVarName = firstCharToLowerCase(table.buildServiceClassName())) package #(packageConfig.controllerPackage); import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.GetMapping; +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(controllerConfig.supperClass) -import #(controllerConfig.buildSuperClassImport()) +import #(controllerConfig.buildSuperClassImport()); #end +import java.io.Serializable; + /** - * #(table.getComment()) 控制层。 + * #(tableComment) 控制层。 * * @author #(javadocConfig.getAuthor()) * @since #(javadocConfig.getSince()) @@ -21,13 +31,21 @@ import #(controllerConfig.buildSuperClassImport()) #else @Controller #end -@RequestMapping("/#(table.getEntityJavaFileName())") -#if(controllerConfig.supperClass) -public class #(table.buildControllerClassName()) extends #(controllerConfig.buildSuperClassName()) { +@RequestMapping("/#(firstCharToLowerCase(entityClassName))") +public class #(table.buildControllerClassName()) #if(controllerConfig.supperClass)extends #(controllerConfig.buildSuperClassName()) #end { -} -#else -public class #(table.buildControllerClassName()) { + @Autowired + private #(table.buildServiceClassName()) #(serviceVarName); + + /** + * 根据#(tableComment)获取详细信息。 + * + * @param id #(tableComment)主键 + * @return #(tableComment)详情 + */ + @GetMapping("getInfo/{id}") + public #(entityClassName) getInfo(@PathVariable Serializable id) { + return #(serviceVarName).getById(id); + } -} -#end \ No newline at end of file +} \ No newline at end of file From 51acb6df3f72434fb957ec95587dcc171c2042a2 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 18 May 2023 12:32:36 +0800 Subject: [PATCH 15/28] =?UTF-8?q?update:=20=E5=9C=A8=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=9F=9F=E6=B7=BB=E5=8A=A0=20spring-web=20=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=EF=BC=8C=E6=96=B9=E4=BE=BF=E6=B5=8B=E8=AF=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mybatis-flex-codegen/pom.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mybatis-flex-codegen/pom.xml b/mybatis-flex-codegen/pom.xml index 3c306aae..58277b0e 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 + + From 6729860e0306d15f795b2905c4978e62ccbeb64c Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 18 May 2023 12:33:11 +0800 Subject: [PATCH 16/28] =?UTF-8?q?style:=20=E4=BB=A3=E7=A0=81=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E6=9B=B4=E6=96=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/GlobalConfig.java | 9 ++- .../codegen/config/StrategyConfig.java | 12 ++-- .../codegen/generator/IGenerator.java | 66 +++++++++---------- 3 files changed, 47 insertions(+), 40 deletions(-) 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 6e83f36b..57885847 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 @@ -234,7 +234,14 @@ public class GlobalConfig { return packageInfoGenerateEnable; } - public void addCustomConfig(String key, Object value) { + public Object getCustomConfig(String key) { + if (customConfig != null) { + return customConfig.get(key); + } + return null; + } + + public void setCustomConfig(String key, Object value) { if (customConfig == null) { customConfig = new HashMap<>(); } 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 index 4ee35977..b6dc157a 100644 --- 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 @@ -70,7 +70,7 @@ public class StrategyConfig { */ private Set unGenerateTables; - public void addTableConfig(TableConfig tableConfig) { + public void setTableConfig(TableConfig tableConfig) { if (tableConfigMap == null) { tableConfigMap = new HashMap<>(); } @@ -81,19 +81,19 @@ public class StrategyConfig { return tableConfigMap == null ? null : tableConfigMap.get(tableName); } - public void addColumnConfig(ColumnConfig columnConfig) { + public void setColumnConfig(ColumnConfig columnConfig) { if (columnConfigMap == null) { columnConfigMap = new HashMap<>(); } columnConfigMap.put(columnConfig.getColumnName(), columnConfig); } - public void addColumnConfig(String tableName, ColumnConfig columnConfig) { + public void setColumnConfig(String tableName, ColumnConfig columnConfig) { TableConfig tableConfig = getTableConfig(tableName); if (tableConfig == null) { tableConfig = new TableConfig(); tableConfig.setTableName(tableName); - addTableConfig(tableConfig); + setTableConfig(tableConfig); } tableConfig.addColumnConfig(columnConfig); @@ -129,7 +129,7 @@ public class StrategyConfig { return columnConfig; } - public void addGenerateTable(String... tables) { + public void setGenerateTable(String... tables) { if (generateTables == null) { generateTables = new HashSet<>(); } @@ -141,7 +141,7 @@ public class StrategyConfig { } } - public void addUnGenerateTable(String... tables) { + public void setUnGenerateTable(String... tables) { if (unGenerateTables == null) { unGenerateTables = new HashSet<>(); } 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 4d653e85..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,17 +1,17 @@ -/** - * 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; @@ -26,26 +26,26 @@ import com.mybatisflex.codegen.entity.Table; */ public interface IGenerator { - /** - * 获取模板文件位置。 - * - * @return 路径 - */ - String getTemplatePath(); + /** + * 获取模板文件位置。 + * + * @return 路径 + */ + String getTemplatePath(); - /** - * 设置模板文件位置。 - * - * @param templatePath - */ - void setTemplatePath(String templatePath); + /** + * 设置模板文件位置。 + * + * @param templatePath + */ + void setTemplatePath(String templatePath); - /** - * 根据模板生成文件。 - * - * @param table 表内容 - * @param globalConfig 全局配置 - */ - void generate(Table table, GlobalConfig globalConfig); + /** + * 根据模板生成文件。 + * + * @param table 表内容 + * @param globalConfig 全局配置 + */ + void generate(Table table, GlobalConfig globalConfig); } From 1b2598b4dbb7371cdc802815784ea027b63bcca8 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 18 May 2023 12:47:48 +0800 Subject: [PATCH 17/28] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/test/GeneratorTest.java | 10 +++++----- .../codegen/test/SqliteGeneratorTest.java | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) 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 6393fc48..839db8ae 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 @@ -49,7 +49,7 @@ public class GeneratorTest { //设置只生成哪些表 globalConfig.getStrategyConfig() - .addGenerateTable("account", "account_session"); + .setGenerateTable("account", "account_session"); //设置生成 entity globalConfig.enableEntity() @@ -70,7 +70,7 @@ public class GeneratorTest { TableConfig tableConfig = new TableConfig(); tableConfig.setTableName("account"); tableConfig.setUpdateListenerClass(MyUpdateListener.class); - globalConfig.getStrategyConfig().addTableConfig(tableConfig); + globalConfig.getStrategyConfig().setTableConfig(tableConfig); //可以单独配置某个列 @@ -78,7 +78,7 @@ public class GeneratorTest { columnConfig.setColumnName("tenant_id"); columnConfig.setLarge(true); columnConfig.setVersion(true); - globalConfig.getStrategyConfig().addColumnConfig("account", columnConfig); + globalConfig.getStrategyConfig().setColumnConfig("account", columnConfig); //通过 datasource 和 globalConfig 创建代码生成器 @@ -135,7 +135,7 @@ public class GeneratorTest { //设置表前缀和只生成哪些表 globalConfig.getStrategyConfig() .setTablePrefix("sys_") - .addGenerateTable("sys_user"); + .setGenerateTable("sys_user"); globalConfig.getTemplateConfig() .setEntity("D:\\Documents\\配置文件\\entity.tpl"); @@ -152,7 +152,7 @@ public class GeneratorTest { //配置生成 serviceImpl globalConfig.enableServiceImpl(); //配置生成 controller - //globalConfig.enableController(); + globalConfig.enableController(); //配置生成 tableDef //globalConfig.enableTableDef(); //配置生成 mapperXml 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 f8cdde78..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,3 +1,19 @@ +/* + * 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; @@ -31,7 +47,7 @@ public class SqliteGeneratorTest { //设置只生成哪些表 globalConfig.getStrategyConfig() - .addGenerateTable("person"); + .setGenerateTable("person"); globalConfig.enableEntity() .setWithLombok(true); From 4a0f01812a178a4040c4fc6879c867bd9814eaf3 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 18 May 2023 12:48:09 +0800 Subject: [PATCH 18/28] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=94=9F=E6=88=90=E5=99=A8=E6=96=87=E6=A1=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/others/codegen.md | 340 +++++++++++++++++++------------------- 1 file changed, 169 insertions(+), 171 deletions(-) diff --git a/docs/zh/others/codegen.md b/docs/zh/others/codegen.md index 790b0a6d..c894bdd9 100644 --- a/docs/zh/others/codegen.md +++ b/docs/zh/others/codegen.md @@ -85,7 +85,7 @@ public class Codegen { ## 使用介绍 -在 Mybatis-Flex 的代码生成器中,支持如下 6 种类型的的产物生成: +在 Mybatis-Flex 的代码生成器中,支持如下 7 种类型的的产物生成: - Entity 实体类 - Mapper 映射类 @@ -93,253 +93,250 @@ public class Codegen { - Service 服务类 - ServiceImpl 服务实现类 - Controller 控制类 +- MapperXml 文件 启用或关闭某种类型产物的生成,代码如下: ```java - // 开启 Entity 的生成 globalConfig.enableEntity(); // 关闭 Entity 的生成 globalConfig.disableEntity(); - ``` 所有方法均支持链式调用配置,代码如下: ```java - // 设置生成 Entity 并启用 Lombok、设置父类 globalConfig.enableEntity() .setWithLombok(true) .setSupperClass(BaseEntity.class); - ``` ## 全局配置 `GlobalConfig` -GlobalConfig 全局配置项包含多个子配置项: +| 获取配置 | 描述 | +|------------------------|------------------| +| 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"); +``` -public class GlobalConfig { +| 启用生成 | 描述 | +|---------------------|-------------------| +| enableEntity() | 启用 Entity 生成 | +| enableMapper() | 启用 Mapper 生成 | +| enableService() | 启用 Service 生成 | +| enableServiceImpl() | 启用 ServiceImpl 生成 | +| enableController() | 启用 Controller 生成 | +| enableTableDef() | 启用 TableDef 生成 | +| enableMapperXml() | 启用 MapperXml 生成 | - private final PackageConfig packageConfig; - private final StrategyConfig strategyConfig; +启用生成之后可以继续链式进行配置,例如: - private EntityConfig entityConfig; - private MapperConfig mapperConfig; - private ServiceConfig serviceConfig; - private ServiceImplConfig serviceImplConfig; - private ControllerConfig controllerConfig; - private TableDefConfig tableDefConfig; +```java +// 设置生成 Entity 并启用 Lombok、设置父类 +globalConfig.enableEntity() + .setWithLombok(true) + .setSupperClass(BaseEntity.class); +``` - private Map customConfig; - -} +## 注释配置 `JavadocConfig` +| 配置 | 描述 | 默认值 | +|-----------------------------------|-------|---------------------------------| +| setAuthor(String) | 作者 | System.getProperty("user.name") | +| setSince(String) | 自 | 日期(yyyy-MM-dd) | +| setTableCommentFormat(Function) | 表名格式化 | 原表名 | +| setPackageCommentFormat(Function) | 包名格式化 | 原包名 | + +```java +globalConfig.getJavadocConfig() + .setAuthor("Your Name") + .setSince("1.0.1"); ``` ## 包配置 `PackageConfig` -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 - -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; - -} +globalConfig.getPackageConfig() + .setSourceDir("D://files/java") + .setBasePackage("com.your.domain"); ``` ## 策略配置 `StrategyConfig` -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"); +``` -public class StrategyConfig { +## 模板配置 `TemplateConfig` - //数据库表前缀,多个前缀用英文逗号(,) 隔开。 - private String tablePrefix; +| 配置 | 描述 | 默认值 | +|------------------------|------------------|------------------------------------| +| 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" | - //逻辑删除的默认字段名称。 - private String logicDeleteColumn; - - //乐观锁的字段名称。 - private String versionColumn; - - //是否生成视图映射。 - private boolean generateForView; - - //是否覆盖之前生成的文件。 - private boolean overwriteEnable; - - //单独为某张表添加独立的配置。 - private Map tableConfigMap; - - //设置某个列的全局配置。 - private Map defaultColumnConfigMap; - - //生成那些表,白名单。 - private Set generateTables; - - //不生成那些表,黑名单。 - private Set unGenerateTables; - - //使用哪个模板引擎来生成代码。 - protected ITemplate templateEngine; - -} +```java +globalConfig.getTemplateConfig() + .setTemplate(new FreeMarkerTemplate()) + .setEntity("D:\your-template-file\my-entity.tpl"); ``` ## Entity 生成配置 `EntityConfig` -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 - -public class EntityConfig { - - //Entity 类的前缀。 - private String classPrefix = ""; - - //Entity 类的后缀。 - private String classSuffix = ""; - - //Entity 类的父类,可以自定义一些 BaseEntity 类。 - private Class supperClass; - - //Entity 默认实现的接口。 - private Class[] implInterfaces = {Serializable.class}; - - //Entity 是否使用 Lombok 注解。 - private boolean withLombok; - -} +globalConfig.getEntityConfig() + .setWithLombok(true) + .setClassPrefix("My") + .setClassSuffix("Entity") + .setSupperClass(BaseEntity.class); ``` ## Mapper 生成配置 `MapperConfig` -MapperConfig 支持的配置如下: +| 配置 | 描述 | 默认值 | +|-----------------------------|-------------|------------------| +| setClassPrefix(String) | Mapper 类的前缀 | "" | +| setClassSuffix(String) | Mapper 类的后缀 | "Mapper" | +| setSupperClass(Class) | Mapper 类的父类 | BaseMapper.class | +| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false | ```java - -public class MapperConfig { - - //Mapper 类的前缀。 - private String classPrefix = ""; - - //Mapper 类的后缀。 - private String classSuffix = "Mapper"; - - //自定义 Mapper 的父类。 - private Class supperClass = BaseMapper.class; - -} +globalConfig.getMapperConfig() + .setClassPrefix("My") + .setClassSuffix("Mapper") + .setSuperClass(BaseMapper.class); ``` ## Service 生成配置 `ServiceConfig` -ServiceConfig 支持的配置如下: +| 配置 | 描述 | 默认值 | +|-----------------------------|--------------|----------------| +| setClassPrefix(String) | Service 类的前缀 | "" | +| setClassSuffix(String) | Service 类的后缀 | "Service" | +| setSupperClass(Class) | Service 类的父类 | IService.class | +| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false | ```java - -public class ServiceConfig { - - //Service 类的前缀。 - private String classPrefix = ""; - - //Service 类的后缀。 - private String classSuffix = "Service"; - - //自定义 Service 的父类。 - private Class supperClass = IService.class; - -} +globalConfig.getServiceConfig() + .setClassPrefix("My") + .setClassSuffix("Service") + .setSuperClass(IService.class); ``` ## ServiceImpl 生成配置 `ServiceImplConfig` -ServiceImplConfig 支持的配置如下: +| 配置 | 描述 | 默认值 | +|-----------------------------|------------------|-------------------| +| setClassPrefix(String) | ServiceImpl 类的前缀 | "" | +| setClassSuffix(String) | ServiceImpl 类的后缀 | "ServiceImpl" | +| setSupperClass(Class) | ServiceImpl 类的父类 | ServiceImpl.class | +| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false | ```java - -public class ServiceImplConfig { - - //ServiceImpl 类的前缀。 - private String classPrefix = ""; - - //ServiceImpl 类的后缀。 - private String classSuffix = "ServiceImpl"; - - //自定义 ServiceImpl 的父类。 - private Class supperClass = ServiceImpl.class; - -} +globalConfig.getServiceImplConfig() + .setClassPrefix("My") + .setClassSuffix("ServiceImpl") + .setSuperClass(ServiceImpl.class); ``` ## Controller 生成配置 `ControllerConfig` -ControllerConfig 支持的配置如下: +| 配置 | 描述 | 默认值 | +|-----------------------------|---------------------|--------------| +| setClassPrefix(String) | Controller 类的前缀 | "" | +| setClassSuffix(String) | Controller 类的后缀 | "Controller" | +| setSupperClass(Class) | Controller 类的父类 | null | +| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false | +| setRestStyle(boolean) | REST 风格的 Controller | true | ```java - -public class ControllerConfig { - - //Controller 类的前缀。 - private String classPrefix = ""; - - //Controller 类的后缀。 - private String classSuffix = "Controller"; - - //自定义 Controller 的父类。 - private Class supperClass; - - //生成 REST 风格的 Controller。 - private boolean restStyle = true; - -} +globalConfig.getControllerConfig() + .setClassPrefix("My") + .setClassSuffix("Controller") + .setSuperClass(BaseController.class); ``` ## TableDef 生成配置 `TableDefConfig` -TableDefConfig 支持的配置如下: +| 配置 | 描述 | 默认值 | +|------------------------|---------------|-------| +| setClassPrefix(String) | TableDef 类的前缀 | "" | +| setClassSuffix(String) | TableDef 类的后缀 | "Def" | +| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false | ```java +globalConfig.getTableDefConfig() + .setClassPrefix("My") + .setClassSuffix("Def"); +``` -public class TableDefConfig { +## MapperXml 生成配置 `MapperXmlConfig` - //TableDef 类的前缀。 - private String classPrefix = ""; +| 配置 | 描述 | 默认值 | +|-----------------------------|-----------------|----------| +| setFilePrefix(String) | MapperXml 文件的前缀 | "" | +| setFileSuffix(String) | MapperXml 文件的后缀 | "Mapper" | +| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false | - //TableDef 类的后缀。 - private String classSuffix = "Def"; - -} +```java +globalConfig.getMapperXmlConfig() + .setFilePrefix("My") + .setFileSuffix("Mapper"); ``` ## 表配置 `TableConfig` @@ -476,24 +473,25 @@ public class EntityGenerator implements IGenerator { } PackageConfig packageConfig = globalConfig.getPackageConfig(); - StrategyConfig strategyConfig = globalConfig.getStrategyConfig(); + EntityConfig entityConfig = globalConfig.getEntityConfig(); String entityPackagePath = packageConfig.getEntityPackage().replace(".", "/"); File entityJavaFile = new File(packageConfig.getSourceDir(), entityPackagePath + "/" + table.buildEntityClassName() + ".java"); - if (entityJavaFile.exists() && !strategyConfig.isOverwriteEnable()) { + if (entityJavaFile.exists() && !entityConfig.isOverwriteEnable()) { return; } - Map params = new HashMap<>(3); + Map params = new HashMap<>(4); params.put("table", table); + params.put("entityConfig", entityConfig); params.put("packageConfig", packageConfig); - params.put("entityConfig", globalConfig.getEntityConfig()); + params.put("javadocConfig", globalConfig.getJavadocConfig()); - strategyConfig.getTemplateEngine().generate(params, templatePath, entityJavaFile); + globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, entityJavaFile); } } ``` From 091afd5e17c9b36a6068eba55e63b94a16dd19e6 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 19 May 2023 19:36:23 +0800 Subject: [PATCH 19/28] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20MapperXML=20?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=88=86=E9=A1=B5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/paginate/PaginateInterceptor.java | 217 ++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java new file mode 100644 index 00000000..0c025b4c --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java @@ -0,0 +1,217 @@ +/* + * 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.core.paginate; + +import com.mybatisflex.core.dialect.LimitOffsetProcessor; +import com.mybatisflex.core.query.QueryWrapper; +import org.apache.ibatis.cache.CacheKey; +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.mapping.ResultMap; +import org.apache.ibatis.plugin.Interceptor; +import org.apache.ibatis.plugin.Intercepts; +import org.apache.ibatis.plugin.Invocation; +import org.apache.ibatis.plugin.Signature; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; + +import java.sql.SQLException; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +/** + * 分页拦截器。 + * + * @author 王帅 + * @since 2023-05-18 + */ +@Intercepts({ + @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), + @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}) +}) +@SuppressWarnings({"rawtypes", "unchecked"}) +public class PaginateInterceptor implements Interceptor { + + /** + * 拦截 Executor 的两个 query 方法,实现分页操作。
+ * {@link Executor#query(MappedStatement, Object, RowBounds, ResultHandler)}
+ * {@link Executor#query(MappedStatement, Object, RowBounds, ResultHandler, CacheKey, BoundSql)} + */ + @Override + public Object intercept(Invocation invocation) throws Throwable { + // 获取 Executor 对象 query 方法的参数列表 + Object[] args = invocation.getArgs(); + // 获取两个 query 方法都有的参数 + MappedStatement ms = (MappedStatement) args[0]; + Object parameters = args[1]; + RowBounds rowBounds = (RowBounds) args[2]; + ResultHandler resultHandler = (ResultHandler) args[3]; + // 获取 Executor 对象 + Executor executor = (Executor) invocation.getTarget(); + + BoundSql boundSql; + + // 两个 query 方法参数不相同,获取 BoundSql 的方式不同 + if (args.length == 4) { + // 4 个参数时 + boundSql = ms.getBoundSql(parameters); + } else { + // 6 个参数时 + boundSql = (BoundSql) args[5]; + } + + // 获取拦截查询方法是否有 Page 参数 + Page page = getPage(parameters); + // 没有 Page 参数就认为不是分页操作 + if (page == null) { + return invocation.proceed(); + } + + // 设置分页数据 + page.setRecords(executeQuery(executor, ms, parameters, rowBounds, resultHandler, boundSql, page)); + // 设置数据总数量 + page.setTotalRow(executeCount(executor, ms, parameters, rowBounds, resultHandler, boundSql)); + + return Collections.singletonList(page); + } + + /** + * 查询被拦截方法的参数有没有 {@link Page} 对象。 + */ + private Page getPage(Object parameters) { + if (parameters != null) { + if (parameters instanceof Map) { + Map parameterMap = (Map) parameters; + for (Entry entry : parameterMap.entrySet()) { + if (entry.getValue() instanceof Page) { + return (Page) entry.getValue(); + } + } + } else if (parameters instanceof Page) { + return (Page) parameters; + } + } + return null; + } + + /** + * 根据 {@link Page} 对象中页码({@link Page#getPageNumber()}) + * 和每页显示的数量({@link Page#getPageSize()}),构建 SQL 语句, + * 查询指定页的内容。 + */ + private List executeQuery(Executor executor, MappedStatement ms, + Object parameters, RowBounds rowBounds, + ResultHandler resultHandler, BoundSql boundSql, + Page page) throws SQLException { + // 获取分页 SQL 语句 + String pageSqlStr = getPageSqlStr(boundSql.getSql(), page); + // 构建分页查询 BoundSql 对象 + BoundSql pageSql = new BoundSql(ms.getConfiguration(), pageSqlStr, boundSql.getParameterMappings(), parameters); + // 将原 SQL 中的占位符对应的参数,添加到新分页 SQL 中 + boundSql.getAdditionalParameters().forEach(pageSql::setAdditionalParameter); + // 获取 CacheKey 对象 + CacheKey cacheKey = executor.createCacheKey(ms, parameters, rowBounds, pageSql); + // 执行分页 SQL 查询数据 + return executor.query(ms, parameters, rowBounds, resultHandler, cacheKey, pageSql); + } + + /** + * 构建分页 SQL 语句。 + */ + private String getPageSqlStr(String originalSql, Page page) { + int size = page.getPageSize(); + int number = page.getPageNumber(); + int limitRows = (number - 1) * size; + return LimitOffsetProcessor.MYSQL.process(new StringBuilder(originalSql), QueryWrapper.create(), size, limitRows).toString(); + } + + /** + * 查询符合原 SQL 条件的数据数量。 + */ + private Long executeCount(Executor executor, MappedStatement ms, + Object parameters, RowBounds rowBounds, + ResultHandler resultHandler, BoundSql boundSql) throws SQLException { + // 获取 count 查询的 MappedStatement 对象 + MappedStatement countMs = getCountMappedStatement(ms); + + BoundSql countSql; + + if (countMs != null) { + countSql = countMs.getBoundSql(parameters); + } else { + // 获取不到则构建新的 MappedStatement 对象 + countMs = buildCountMappedStatement(ms); + // 获取 count 查询 SQL + String countSqlStr = getCountSqlStr(boundSql.getSql()); + // 构建 count 查询 BoundSql 对象 + countSql = new BoundSql(ms.getConfiguration(), countSqlStr, boundSql.getParameterMappings(), parameters); + // 将原 SQL 中的占位符对应的参数,添加到新 count 查询 SQL 中 + boundSql.getAdditionalParameters().forEach(countSql::setAdditionalParameter); + } + + // 获取 CacheKey 对象 + CacheKey countKey = executor.createCacheKey(countMs, parameters, rowBounds, countSql); + // 执行 count 查询 SQL 查询数量 + Object result = executor.query(countMs, parameters, rowBounds, resultHandler, countKey, countSql); + // 处理结果,返回总数量 + return ((Number) ((List) result).get(0)).longValue(); + } + + /** + * 尝试获取已经存在的 {@link MappedStatement} 对象。 + */ + private MappedStatement getCountMappedStatement(MappedStatement ms) { + String msId = ms.getId().concat("_COUNT"); + MappedStatement mappedStatement = null; + try { + mappedStatement = ms.getConfiguration().getMappedStatement(msId, false); + } catch (Exception ignored) { + // do nothing. + } + return mappedStatement; + } + + /** + * 构建 count 查询的 {@link MappedStatement} 对象。 + */ + private MappedStatement buildCountMappedStatement(MappedStatement ms) { + String countId = ms.getId() + "_Count"; + MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), countId, ms.getSqlSource(), ms.getSqlCommandType()); + return builder.resource(ms.getResource()) + .fetchSize(ms.getFetchSize()) + .statementType(ms.getStatementType()) + .keyGenerator(ms.getKeyGenerator()) + .timeout(ms.getTimeout()) + .parameterMap(ms.getParameterMap()) + .resultMaps(Collections.singletonList(new ResultMap.Builder(ms.getConfiguration(), ms.getId(), Long.class, Collections.emptyList()).build())) + .resultSetType(ms.getResultSetType()) + .cache(ms.getCache()) + .flushCacheRequired(ms.isFlushCacheRequired()) + .useCache(ms.isUseCache()) + .build(); + } + + /** + * 构建 count 查询 SQL 语句。 + */ + private String getCountSqlStr(String originalSql) { + return String.format("SELECT COUNT(*) FROM (%s) TOTAL", originalSql); + } + +} \ No newline at end of file From 2678247b03b1a8c79715c381844df4e7a7084f5c Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sat, 20 May 2023 11:39:32 +0800 Subject: [PATCH 20/28] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20count=20?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E8=AF=AD=E5=8F=A5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/paginate/PaginateInterceptor.java | 139 +++++++++++++++++- 1 file changed, 137 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java index 0c025b4c..a3736940 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java @@ -17,6 +17,13 @@ package com.mybatisflex.core.paginate; import com.mybatisflex.core.dialect.LimitOffsetProcessor; import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.core.util.CollectionUtil; +import net.sf.jsqlparser.expression.Alias; +import net.sf.jsqlparser.expression.Expression; +import net.sf.jsqlparser.parser.CCJSqlParserUtil; +import net.sf.jsqlparser.schema.Column; +import net.sf.jsqlparser.schema.Table; +import net.sf.jsqlparser.statement.select.*; import org.apache.ibatis.cache.CacheKey; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; @@ -34,6 +41,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; /** * 分页拦截器。 @@ -48,6 +56,10 @@ import java.util.Map.Entry; @SuppressWarnings({"rawtypes", "unchecked"}) public class PaginateInterceptor implements Interceptor { + private static final List COUNT_SELECT_ITEM = Collections.singletonList( + new SelectExpressionItem(new Column().withColumnName("COUNT(*)")).withAlias(new Alias("total")) + ); + /** * 拦截 Executor 的两个 query 方法,实现分页操作。
* {@link Executor#query(MappedStatement, Object, RowBounds, ResultHandler)}
@@ -191,7 +203,7 @@ public class PaginateInterceptor implements Interceptor { * 构建 count 查询的 {@link MappedStatement} 对象。 */ private MappedStatement buildCountMappedStatement(MappedStatement ms) { - String countId = ms.getId() + "_Count"; + String countId = ms.getId().concat("_COUNT"); MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), countId, ms.getSqlSource(), ms.getSqlCommandType()); return builder.resource(ms.getResource()) .fetchSize(ms.getFetchSize()) @@ -208,10 +220,133 @@ public class PaginateInterceptor implements Interceptor { } /** - * 构建 count 查询 SQL 语句。 + * 构建并优化 count 查询 SQL 语句。 */ private String getCountSqlStr(String originalSql) { + try { + Select select = (Select) CCJSqlParserUtil.parse(originalSql); + PlainSelect plainSelect = (PlainSelect) select.getSelectBody(); + GroupByElement groupBy = plainSelect.getGroupBy(); + + // 没有 group by 语句时,可以去掉 order by 语句 + if (groupBy == null && canCleanOrderBy(plainSelect.getOrderByElements())) { + plainSelect.setOrderByElements(null); + } + + // 如果 select 列中包含参数,则不继续处理 + for (SelectItem item : plainSelect.getSelectItems()) { + if (item.toString().contains("?")) { + return getSimpleCountSqlStr(select.toString()); + } + } + + Distinct distinct = plainSelect.getDistinct(); + + // 如果有 distinct、group by 语句,则不继续处理 + if (distinct != null || groupBy != null) { + return getSimpleCountSqlStr(originalSql); + } + + String whereStr = Optional.ofNullable(plainSelect.getWhere()) + .map(Expression::toString) + .orElse(""); + + // 判断是否可以去除 join 语句 + if (canCleanJoins(plainSelect.getJoins(), whereStr)) { + plainSelect.setJoins(null); + } + + // 优化 count 查询 SQL 语句 + plainSelect.setSelectItems(COUNT_SELECT_ITEM); + + return select.toString(); + } catch (Exception ignored) { + // 无法解析优化 SQL 时使用原 SQL 语句 + } + return getSimpleCountSqlStr(originalSql); + } + + /** + * 构建 count 查询 SQL 语句。 + */ + private String getSimpleCountSqlStr(String originalSql) { return String.format("SELECT COUNT(*) FROM (%s) TOTAL", originalSql); } + /** + * 能否去除 order by 语句,如果有 order by 语句,且 order by 语句中没有参数, + * 则可以去除 order by 语句。 + */ + public boolean canCleanOrderBy(List orderBy) { + // 没有 order by 子句 + if (CollectionUtil.isEmpty(orderBy)) { + return false; + } + // order by 子句中有参数 + for (OrderByElement orderByElement : orderBy) { + if (orderByElement.toString().contains("?")) { + return false; + } + } + // 可以去除 order by 子句 + return true; + } + + /** + * 能否去除 join 语句,如果全是 left join 语句,并且 on 语句没有用到参数, + * where 语句也没有用到 join 语句中的参数,则可以去除 join 语句。 + */ + public boolean canCleanJoins(List joins, String whereStr) { + // 没有 join 语句 + if (CollectionUtil.isEmpty(joins)) { + return false; + } + for (Join join : joins) { + // 不是 left join 语句 + if (!join.isLeft()) { + return false; + } + + FromItem rightItem = join.getRightItem(); + + String aliasStr = ""; + + if (rightItem instanceof Table) { + // left join 后面是表 + Table table = (Table) rightItem; + // 获取表的别名,用于判断是否在 where 语句中使用 + aliasStr = Optional.ofNullable(table.getAlias()) + .map(Alias::getName) + .orElse(table.getName()); + } else if (rightItem instanceof SubSelect) { + // left join 后面是子查询 + SubSelect subSelect = (SubSelect) rightItem; + // 子查询里包含参数 + if (subSelect.toString().contains("?")) { + return false; + } + // 获取子查询表的别名,用于判断是否在 where 语句中使用 + aliasStr = subSelect.getAlias().getName(); + } + + // 忽略大小写 + aliasStr = aliasStr.toLowerCase(); + whereStr = whereStr.toLowerCase(); + + // where 语句中包含 join 表的字段 + if (whereStr.contains(aliasStr)) { + return false; + } + + // on 语句中有参数 + for (Expression expression : join.getOnExpressions()) { + if (expression.toString().contains("?")) { + return false; + } + } + } + // 可以去除 join 语句 + return true; + } + } \ No newline at end of file From 4897b7ff2feb86b7738d55a13f8bec3ec6429384 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sat, 20 May 2023 11:50:04 +0800 Subject: [PATCH 21/28] =?UTF-8?q?Revert=20"feat:=20=E4=BC=98=E5=8C=96=20co?= =?UTF-8?q?unt=20=E6=9F=A5=E8=AF=A2=E8=AF=AD=E5=8F=A5=E3=80=82"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 2678247b03b1a8c79715c381844df4e7a7084f5c. --- .../core/paginate/PaginateInterceptor.java | 139 +----------------- 1 file changed, 2 insertions(+), 137 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java index a3736940..0c025b4c 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java @@ -17,13 +17,6 @@ package com.mybatisflex.core.paginate; import com.mybatisflex.core.dialect.LimitOffsetProcessor; import com.mybatisflex.core.query.QueryWrapper; -import com.mybatisflex.core.util.CollectionUtil; -import net.sf.jsqlparser.expression.Alias; -import net.sf.jsqlparser.expression.Expression; -import net.sf.jsqlparser.parser.CCJSqlParserUtil; -import net.sf.jsqlparser.schema.Column; -import net.sf.jsqlparser.schema.Table; -import net.sf.jsqlparser.statement.select.*; import org.apache.ibatis.cache.CacheKey; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; @@ -41,7 +34,6 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Optional; /** * 分页拦截器。 @@ -56,10 +48,6 @@ import java.util.Optional; @SuppressWarnings({"rawtypes", "unchecked"}) public class PaginateInterceptor implements Interceptor { - private static final List COUNT_SELECT_ITEM = Collections.singletonList( - new SelectExpressionItem(new Column().withColumnName("COUNT(*)")).withAlias(new Alias("total")) - ); - /** * 拦截 Executor 的两个 query 方法,实现分页操作。
* {@link Executor#query(MappedStatement, Object, RowBounds, ResultHandler)}
@@ -203,7 +191,7 @@ public class PaginateInterceptor implements Interceptor { * 构建 count 查询的 {@link MappedStatement} 对象。 */ private MappedStatement buildCountMappedStatement(MappedStatement ms) { - String countId = ms.getId().concat("_COUNT"); + String countId = ms.getId() + "_Count"; MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), countId, ms.getSqlSource(), ms.getSqlCommandType()); return builder.resource(ms.getResource()) .fetchSize(ms.getFetchSize()) @@ -219,134 +207,11 @@ public class PaginateInterceptor implements Interceptor { .build(); } - /** - * 构建并优化 count 查询 SQL 语句。 - */ - private String getCountSqlStr(String originalSql) { - try { - Select select = (Select) CCJSqlParserUtil.parse(originalSql); - PlainSelect plainSelect = (PlainSelect) select.getSelectBody(); - GroupByElement groupBy = plainSelect.getGroupBy(); - - // 没有 group by 语句时,可以去掉 order by 语句 - if (groupBy == null && canCleanOrderBy(plainSelect.getOrderByElements())) { - plainSelect.setOrderByElements(null); - } - - // 如果 select 列中包含参数,则不继续处理 - for (SelectItem item : plainSelect.getSelectItems()) { - if (item.toString().contains("?")) { - return getSimpleCountSqlStr(select.toString()); - } - } - - Distinct distinct = plainSelect.getDistinct(); - - // 如果有 distinct、group by 语句,则不继续处理 - if (distinct != null || groupBy != null) { - return getSimpleCountSqlStr(originalSql); - } - - String whereStr = Optional.ofNullable(plainSelect.getWhere()) - .map(Expression::toString) - .orElse(""); - - // 判断是否可以去除 join 语句 - if (canCleanJoins(plainSelect.getJoins(), whereStr)) { - plainSelect.setJoins(null); - } - - // 优化 count 查询 SQL 语句 - plainSelect.setSelectItems(COUNT_SELECT_ITEM); - - return select.toString(); - } catch (Exception ignored) { - // 无法解析优化 SQL 时使用原 SQL 语句 - } - return getSimpleCountSqlStr(originalSql); - } - /** * 构建 count 查询 SQL 语句。 */ - private String getSimpleCountSqlStr(String originalSql) { + private String getCountSqlStr(String originalSql) { return String.format("SELECT COUNT(*) FROM (%s) TOTAL", originalSql); } - /** - * 能否去除 order by 语句,如果有 order by 语句,且 order by 语句中没有参数, - * 则可以去除 order by 语句。 - */ - public boolean canCleanOrderBy(List orderBy) { - // 没有 order by 子句 - if (CollectionUtil.isEmpty(orderBy)) { - return false; - } - // order by 子句中有参数 - for (OrderByElement orderByElement : orderBy) { - if (orderByElement.toString().contains("?")) { - return false; - } - } - // 可以去除 order by 子句 - return true; - } - - /** - * 能否去除 join 语句,如果全是 left join 语句,并且 on 语句没有用到参数, - * where 语句也没有用到 join 语句中的参数,则可以去除 join 语句。 - */ - public boolean canCleanJoins(List joins, String whereStr) { - // 没有 join 语句 - if (CollectionUtil.isEmpty(joins)) { - return false; - } - for (Join join : joins) { - // 不是 left join 语句 - if (!join.isLeft()) { - return false; - } - - FromItem rightItem = join.getRightItem(); - - String aliasStr = ""; - - if (rightItem instanceof Table) { - // left join 后面是表 - Table table = (Table) rightItem; - // 获取表的别名,用于判断是否在 where 语句中使用 - aliasStr = Optional.ofNullable(table.getAlias()) - .map(Alias::getName) - .orElse(table.getName()); - } else if (rightItem instanceof SubSelect) { - // left join 后面是子查询 - SubSelect subSelect = (SubSelect) rightItem; - // 子查询里包含参数 - if (subSelect.toString().contains("?")) { - return false; - } - // 获取子查询表的别名,用于判断是否在 where 语句中使用 - aliasStr = subSelect.getAlias().getName(); - } - - // 忽略大小写 - aliasStr = aliasStr.toLowerCase(); - whereStr = whereStr.toLowerCase(); - - // where 语句中包含 join 表的字段 - if (whereStr.contains(aliasStr)) { - return false; - } - - // on 语句中有参数 - for (Expression expression : join.getOnExpressions()) { - if (expression.toString().contains("?")) { - return false; - } - } - } - // 可以去除 join 语句 - return true; - } - } \ No newline at end of file From ef49eea97a30494d493a1577e981dac703f63040 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sat, 20 May 2023 11:50:40 +0800 Subject: [PATCH 22/28] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20count=20?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E8=AF=AD=E5=8F=A5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/paginate/PaginateInterceptor.java | 139 +++++++++++++++++- 1 file changed, 137 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java index 0c025b4c..a3736940 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java @@ -17,6 +17,13 @@ package com.mybatisflex.core.paginate; import com.mybatisflex.core.dialect.LimitOffsetProcessor; import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.core.util.CollectionUtil; +import net.sf.jsqlparser.expression.Alias; +import net.sf.jsqlparser.expression.Expression; +import net.sf.jsqlparser.parser.CCJSqlParserUtil; +import net.sf.jsqlparser.schema.Column; +import net.sf.jsqlparser.schema.Table; +import net.sf.jsqlparser.statement.select.*; import org.apache.ibatis.cache.CacheKey; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; @@ -34,6 +41,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; /** * 分页拦截器。 @@ -48,6 +56,10 @@ import java.util.Map.Entry; @SuppressWarnings({"rawtypes", "unchecked"}) public class PaginateInterceptor implements Interceptor { + private static final List COUNT_SELECT_ITEM = Collections.singletonList( + new SelectExpressionItem(new Column().withColumnName("COUNT(*)")).withAlias(new Alias("total")) + ); + /** * 拦截 Executor 的两个 query 方法,实现分页操作。
* {@link Executor#query(MappedStatement, Object, RowBounds, ResultHandler)}
@@ -191,7 +203,7 @@ public class PaginateInterceptor implements Interceptor { * 构建 count 查询的 {@link MappedStatement} 对象。 */ private MappedStatement buildCountMappedStatement(MappedStatement ms) { - String countId = ms.getId() + "_Count"; + String countId = ms.getId().concat("_COUNT"); MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), countId, ms.getSqlSource(), ms.getSqlCommandType()); return builder.resource(ms.getResource()) .fetchSize(ms.getFetchSize()) @@ -208,10 +220,133 @@ public class PaginateInterceptor implements Interceptor { } /** - * 构建 count 查询 SQL 语句。 + * 构建并优化 count 查询 SQL 语句。 */ private String getCountSqlStr(String originalSql) { + try { + Select select = (Select) CCJSqlParserUtil.parse(originalSql); + PlainSelect plainSelect = (PlainSelect) select.getSelectBody(); + GroupByElement groupBy = plainSelect.getGroupBy(); + + // 没有 group by 语句时,可以去掉 order by 语句 + if (groupBy == null && canCleanOrderBy(plainSelect.getOrderByElements())) { + plainSelect.setOrderByElements(null); + } + + // 如果 select 列中包含参数,则不继续处理 + for (SelectItem item : plainSelect.getSelectItems()) { + if (item.toString().contains("?")) { + return getSimpleCountSqlStr(select.toString()); + } + } + + Distinct distinct = plainSelect.getDistinct(); + + // 如果有 distinct、group by 语句,则不继续处理 + if (distinct != null || groupBy != null) { + return getSimpleCountSqlStr(originalSql); + } + + String whereStr = Optional.ofNullable(plainSelect.getWhere()) + .map(Expression::toString) + .orElse(""); + + // 判断是否可以去除 join 语句 + if (canCleanJoins(plainSelect.getJoins(), whereStr)) { + plainSelect.setJoins(null); + } + + // 优化 count 查询 SQL 语句 + plainSelect.setSelectItems(COUNT_SELECT_ITEM); + + return select.toString(); + } catch (Exception ignored) { + // 无法解析优化 SQL 时使用原 SQL 语句 + } + return getSimpleCountSqlStr(originalSql); + } + + /** + * 构建 count 查询 SQL 语句。 + */ + private String getSimpleCountSqlStr(String originalSql) { return String.format("SELECT COUNT(*) FROM (%s) TOTAL", originalSql); } + /** + * 能否去除 order by 语句,如果有 order by 语句,且 order by 语句中没有参数, + * 则可以去除 order by 语句。 + */ + public boolean canCleanOrderBy(List orderBy) { + // 没有 order by 子句 + if (CollectionUtil.isEmpty(orderBy)) { + return false; + } + // order by 子句中有参数 + for (OrderByElement orderByElement : orderBy) { + if (orderByElement.toString().contains("?")) { + return false; + } + } + // 可以去除 order by 子句 + return true; + } + + /** + * 能否去除 join 语句,如果全是 left join 语句,并且 on 语句没有用到参数, + * where 语句也没有用到 join 语句中的参数,则可以去除 join 语句。 + */ + public boolean canCleanJoins(List joins, String whereStr) { + // 没有 join 语句 + if (CollectionUtil.isEmpty(joins)) { + return false; + } + for (Join join : joins) { + // 不是 left join 语句 + if (!join.isLeft()) { + return false; + } + + FromItem rightItem = join.getRightItem(); + + String aliasStr = ""; + + if (rightItem instanceof Table) { + // left join 后面是表 + Table table = (Table) rightItem; + // 获取表的别名,用于判断是否在 where 语句中使用 + aliasStr = Optional.ofNullable(table.getAlias()) + .map(Alias::getName) + .orElse(table.getName()); + } else if (rightItem instanceof SubSelect) { + // left join 后面是子查询 + SubSelect subSelect = (SubSelect) rightItem; + // 子查询里包含参数 + if (subSelect.toString().contains("?")) { + return false; + } + // 获取子查询表的别名,用于判断是否在 where 语句中使用 + aliasStr = subSelect.getAlias().getName(); + } + + // 忽略大小写 + aliasStr = aliasStr.toLowerCase(); + whereStr = whereStr.toLowerCase(); + + // where 语句中包含 join 表的字段 + if (whereStr.contains(aliasStr)) { + return false; + } + + // on 语句中有参数 + for (Expression expression : join.getOnExpressions()) { + if (expression.toString().contains("?")) { + return false; + } + } + } + // 可以去除 join 语句 + return true; + } + } \ No newline at end of file From f2eb16c6f59547ed292d79d48b9eec716d0c7967 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sat, 20 May 2023 11:58:12 +0800 Subject: [PATCH 23/28] =?UTF-8?q?Revert=20"feat:=20=E4=BC=98=E5=8C=96=20co?= =?UTF-8?q?unt=20=E6=9F=A5=E8=AF=A2=E8=AF=AD=E5=8F=A5=E3=80=82"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 2678247b03b1a8c79715c381844df4e7a7084f5c. --- .../core/paginate/PaginateInterceptor.java | 139 +----------------- 1 file changed, 2 insertions(+), 137 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java index a3736940..0c025b4c 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java @@ -17,13 +17,6 @@ package com.mybatisflex.core.paginate; import com.mybatisflex.core.dialect.LimitOffsetProcessor; import com.mybatisflex.core.query.QueryWrapper; -import com.mybatisflex.core.util.CollectionUtil; -import net.sf.jsqlparser.expression.Alias; -import net.sf.jsqlparser.expression.Expression; -import net.sf.jsqlparser.parser.CCJSqlParserUtil; -import net.sf.jsqlparser.schema.Column; -import net.sf.jsqlparser.schema.Table; -import net.sf.jsqlparser.statement.select.*; import org.apache.ibatis.cache.CacheKey; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; @@ -41,7 +34,6 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Optional; /** * 分页拦截器。 @@ -56,10 +48,6 @@ import java.util.Optional; @SuppressWarnings({"rawtypes", "unchecked"}) public class PaginateInterceptor implements Interceptor { - private static final List COUNT_SELECT_ITEM = Collections.singletonList( - new SelectExpressionItem(new Column().withColumnName("COUNT(*)")).withAlias(new Alias("total")) - ); - /** * 拦截 Executor 的两个 query 方法,实现分页操作。
* {@link Executor#query(MappedStatement, Object, RowBounds, ResultHandler)}
@@ -203,7 +191,7 @@ public class PaginateInterceptor implements Interceptor { * 构建 count 查询的 {@link MappedStatement} 对象。 */ private MappedStatement buildCountMappedStatement(MappedStatement ms) { - String countId = ms.getId().concat("_COUNT"); + String countId = ms.getId() + "_Count"; MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), countId, ms.getSqlSource(), ms.getSqlCommandType()); return builder.resource(ms.getResource()) .fetchSize(ms.getFetchSize()) @@ -219,134 +207,11 @@ public class PaginateInterceptor implements Interceptor { .build(); } - /** - * 构建并优化 count 查询 SQL 语句。 - */ - private String getCountSqlStr(String originalSql) { - try { - Select select = (Select) CCJSqlParserUtil.parse(originalSql); - PlainSelect plainSelect = (PlainSelect) select.getSelectBody(); - GroupByElement groupBy = plainSelect.getGroupBy(); - - // 没有 group by 语句时,可以去掉 order by 语句 - if (groupBy == null && canCleanOrderBy(plainSelect.getOrderByElements())) { - plainSelect.setOrderByElements(null); - } - - // 如果 select 列中包含参数,则不继续处理 - for (SelectItem item : plainSelect.getSelectItems()) { - if (item.toString().contains("?")) { - return getSimpleCountSqlStr(select.toString()); - } - } - - Distinct distinct = plainSelect.getDistinct(); - - // 如果有 distinct、group by 语句,则不继续处理 - if (distinct != null || groupBy != null) { - return getSimpleCountSqlStr(originalSql); - } - - String whereStr = Optional.ofNullable(plainSelect.getWhere()) - .map(Expression::toString) - .orElse(""); - - // 判断是否可以去除 join 语句 - if (canCleanJoins(plainSelect.getJoins(), whereStr)) { - plainSelect.setJoins(null); - } - - // 优化 count 查询 SQL 语句 - plainSelect.setSelectItems(COUNT_SELECT_ITEM); - - return select.toString(); - } catch (Exception ignored) { - // 无法解析优化 SQL 时使用原 SQL 语句 - } - return getSimpleCountSqlStr(originalSql); - } - /** * 构建 count 查询 SQL 语句。 */ - private String getSimpleCountSqlStr(String originalSql) { + private String getCountSqlStr(String originalSql) { return String.format("SELECT COUNT(*) FROM (%s) TOTAL", originalSql); } - /** - * 能否去除 order by 语句,如果有 order by 语句,且 order by 语句中没有参数, - * 则可以去除 order by 语句。 - */ - public boolean canCleanOrderBy(List orderBy) { - // 没有 order by 子句 - if (CollectionUtil.isEmpty(orderBy)) { - return false; - } - // order by 子句中有参数 - for (OrderByElement orderByElement : orderBy) { - if (orderByElement.toString().contains("?")) { - return false; - } - } - // 可以去除 order by 子句 - return true; - } - - /** - * 能否去除 join 语句,如果全是 left join 语句,并且 on 语句没有用到参数, - * where 语句也没有用到 join 语句中的参数,则可以去除 join 语句。 - */ - public boolean canCleanJoins(List joins, String whereStr) { - // 没有 join 语句 - if (CollectionUtil.isEmpty(joins)) { - return false; - } - for (Join join : joins) { - // 不是 left join 语句 - if (!join.isLeft()) { - return false; - } - - FromItem rightItem = join.getRightItem(); - - String aliasStr = ""; - - if (rightItem instanceof Table) { - // left join 后面是表 - Table table = (Table) rightItem; - // 获取表的别名,用于判断是否在 where 语句中使用 - aliasStr = Optional.ofNullable(table.getAlias()) - .map(Alias::getName) - .orElse(table.getName()); - } else if (rightItem instanceof SubSelect) { - // left join 后面是子查询 - SubSelect subSelect = (SubSelect) rightItem; - // 子查询里包含参数 - if (subSelect.toString().contains("?")) { - return false; - } - // 获取子查询表的别名,用于判断是否在 where 语句中使用 - aliasStr = subSelect.getAlias().getName(); - } - - // 忽略大小写 - aliasStr = aliasStr.toLowerCase(); - whereStr = whereStr.toLowerCase(); - - // where 语句中包含 join 表的字段 - if (whereStr.contains(aliasStr)) { - return false; - } - - // on 语句中有参数 - for (Expression expression : join.getOnExpressions()) { - if (expression.toString().contains("?")) { - return false; - } - } - } - // 可以去除 join 语句 - return true; - } - } \ No newline at end of file From dafa06a6e53478cc01d12ae49091021b440d597b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=B8=85?= <1474983351@qq.com> Date: Sat, 20 May 2023 04:00:23 +0000 Subject: [PATCH 24/28] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20my?= =?UTF-8?q?batis-flex-core/src/main/java/com/mybatisflex/core/paginate/Pag?= =?UTF-8?q?inateInterceptor.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/paginate/PaginateInterceptor.java | 217 ------------------ 1 file changed, 217 deletions(-) delete mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java deleted file mode 100644 index 0c025b4c..00000000 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/paginate/PaginateInterceptor.java +++ /dev/null @@ -1,217 +0,0 @@ -/* - * 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.core.paginate; - -import com.mybatisflex.core.dialect.LimitOffsetProcessor; -import com.mybatisflex.core.query.QueryWrapper; -import org.apache.ibatis.cache.CacheKey; -import org.apache.ibatis.executor.Executor; -import org.apache.ibatis.mapping.BoundSql; -import org.apache.ibatis.mapping.MappedStatement; -import org.apache.ibatis.mapping.ResultMap; -import org.apache.ibatis.plugin.Interceptor; -import org.apache.ibatis.plugin.Intercepts; -import org.apache.ibatis.plugin.Invocation; -import org.apache.ibatis.plugin.Signature; -import org.apache.ibatis.session.ResultHandler; -import org.apache.ibatis.session.RowBounds; - -import java.sql.SQLException; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -/** - * 分页拦截器。 - * - * @author 王帅 - * @since 2023-05-18 - */ -@Intercepts({ - @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), - @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}) -}) -@SuppressWarnings({"rawtypes", "unchecked"}) -public class PaginateInterceptor implements Interceptor { - - /** - * 拦截 Executor 的两个 query 方法,实现分页操作。
- * {@link Executor#query(MappedStatement, Object, RowBounds, ResultHandler)}
- * {@link Executor#query(MappedStatement, Object, RowBounds, ResultHandler, CacheKey, BoundSql)} - */ - @Override - public Object intercept(Invocation invocation) throws Throwable { - // 获取 Executor 对象 query 方法的参数列表 - Object[] args = invocation.getArgs(); - // 获取两个 query 方法都有的参数 - MappedStatement ms = (MappedStatement) args[0]; - Object parameters = args[1]; - RowBounds rowBounds = (RowBounds) args[2]; - ResultHandler resultHandler = (ResultHandler) args[3]; - // 获取 Executor 对象 - Executor executor = (Executor) invocation.getTarget(); - - BoundSql boundSql; - - // 两个 query 方法参数不相同,获取 BoundSql 的方式不同 - if (args.length == 4) { - // 4 个参数时 - boundSql = ms.getBoundSql(parameters); - } else { - // 6 个参数时 - boundSql = (BoundSql) args[5]; - } - - // 获取拦截查询方法是否有 Page 参数 - Page page = getPage(parameters); - // 没有 Page 参数就认为不是分页操作 - if (page == null) { - return invocation.proceed(); - } - - // 设置分页数据 - page.setRecords(executeQuery(executor, ms, parameters, rowBounds, resultHandler, boundSql, page)); - // 设置数据总数量 - page.setTotalRow(executeCount(executor, ms, parameters, rowBounds, resultHandler, boundSql)); - - return Collections.singletonList(page); - } - - /** - * 查询被拦截方法的参数有没有 {@link Page} 对象。 - */ - private Page getPage(Object parameters) { - if (parameters != null) { - if (parameters instanceof Map) { - Map parameterMap = (Map) parameters; - for (Entry entry : parameterMap.entrySet()) { - if (entry.getValue() instanceof Page) { - return (Page) entry.getValue(); - } - } - } else if (parameters instanceof Page) { - return (Page) parameters; - } - } - return null; - } - - /** - * 根据 {@link Page} 对象中页码({@link Page#getPageNumber()}) - * 和每页显示的数量({@link Page#getPageSize()}),构建 SQL 语句, - * 查询指定页的内容。 - */ - private List executeQuery(Executor executor, MappedStatement ms, - Object parameters, RowBounds rowBounds, - ResultHandler resultHandler, BoundSql boundSql, - Page page) throws SQLException { - // 获取分页 SQL 语句 - String pageSqlStr = getPageSqlStr(boundSql.getSql(), page); - // 构建分页查询 BoundSql 对象 - BoundSql pageSql = new BoundSql(ms.getConfiguration(), pageSqlStr, boundSql.getParameterMappings(), parameters); - // 将原 SQL 中的占位符对应的参数,添加到新分页 SQL 中 - boundSql.getAdditionalParameters().forEach(pageSql::setAdditionalParameter); - // 获取 CacheKey 对象 - CacheKey cacheKey = executor.createCacheKey(ms, parameters, rowBounds, pageSql); - // 执行分页 SQL 查询数据 - return executor.query(ms, parameters, rowBounds, resultHandler, cacheKey, pageSql); - } - - /** - * 构建分页 SQL 语句。 - */ - private String getPageSqlStr(String originalSql, Page page) { - int size = page.getPageSize(); - int number = page.getPageNumber(); - int limitRows = (number - 1) * size; - return LimitOffsetProcessor.MYSQL.process(new StringBuilder(originalSql), QueryWrapper.create(), size, limitRows).toString(); - } - - /** - * 查询符合原 SQL 条件的数据数量。 - */ - private Long executeCount(Executor executor, MappedStatement ms, - Object parameters, RowBounds rowBounds, - ResultHandler resultHandler, BoundSql boundSql) throws SQLException { - // 获取 count 查询的 MappedStatement 对象 - MappedStatement countMs = getCountMappedStatement(ms); - - BoundSql countSql; - - if (countMs != null) { - countSql = countMs.getBoundSql(parameters); - } else { - // 获取不到则构建新的 MappedStatement 对象 - countMs = buildCountMappedStatement(ms); - // 获取 count 查询 SQL - String countSqlStr = getCountSqlStr(boundSql.getSql()); - // 构建 count 查询 BoundSql 对象 - countSql = new BoundSql(ms.getConfiguration(), countSqlStr, boundSql.getParameterMappings(), parameters); - // 将原 SQL 中的占位符对应的参数,添加到新 count 查询 SQL 中 - boundSql.getAdditionalParameters().forEach(countSql::setAdditionalParameter); - } - - // 获取 CacheKey 对象 - CacheKey countKey = executor.createCacheKey(countMs, parameters, rowBounds, countSql); - // 执行 count 查询 SQL 查询数量 - Object result = executor.query(countMs, parameters, rowBounds, resultHandler, countKey, countSql); - // 处理结果,返回总数量 - return ((Number) ((List) result).get(0)).longValue(); - } - - /** - * 尝试获取已经存在的 {@link MappedStatement} 对象。 - */ - private MappedStatement getCountMappedStatement(MappedStatement ms) { - String msId = ms.getId().concat("_COUNT"); - MappedStatement mappedStatement = null; - try { - mappedStatement = ms.getConfiguration().getMappedStatement(msId, false); - } catch (Exception ignored) { - // do nothing. - } - return mappedStatement; - } - - /** - * 构建 count 查询的 {@link MappedStatement} 对象。 - */ - private MappedStatement buildCountMappedStatement(MappedStatement ms) { - String countId = ms.getId() + "_Count"; - MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), countId, ms.getSqlSource(), ms.getSqlCommandType()); - return builder.resource(ms.getResource()) - .fetchSize(ms.getFetchSize()) - .statementType(ms.getStatementType()) - .keyGenerator(ms.getKeyGenerator()) - .timeout(ms.getTimeout()) - .parameterMap(ms.getParameterMap()) - .resultMaps(Collections.singletonList(new ResultMap.Builder(ms.getConfiguration(), ms.getId(), Long.class, Collections.emptyList()).build())) - .resultSetType(ms.getResultSetType()) - .cache(ms.getCache()) - .flushCacheRequired(ms.isFlushCacheRequired()) - .useCache(ms.isUseCache()) - .build(); - } - - /** - * 构建 count 查询 SQL 语句。 - */ - private String getCountSqlStr(String originalSql) { - return String.format("SELECT COUNT(*) FROM (%s) TOTAL", originalSql); - } - -} \ No newline at end of file From 866bd42fbe7e5ff9c6661dde5b578e5cf75f4ef0 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 25 May 2023 21:00:12 +0800 Subject: [PATCH 25/28] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E7=94=9F?= =?UTF-8?q?=E6=88=90=E6=A8=A1=E6=9D=BF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/templates/enjoy/controller.tpl | 69 +++++++++++++++++-- .../templates/enjoy/package-info.tpl | 3 +- 2 files changed, 67 insertions(+), 5 deletions(-) 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 b8d375a3..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,11 +1,17 @@ #set(tableComment = table.getComment()) #set(entityClassName = table.buildEntityClassName()) +#set(entityVarName = firstCharToLowerCase(entityClassName)) #set(serviceVarName = firstCharToLowerCase(table.buildServiceClassName())) package #(packageConfig.controllerPackage); -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.PathVariable; +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; import org.springframework.beans.factory.annotation.Autowired; import #(packageConfig.entityPackage).#(entityClassName); import #(packageConfig.servicePackage).#(table.buildServiceClassName()); @@ -19,6 +25,7 @@ import #(controllerConfig.buildSuperClassImport()); #end import java.io.Serializable; +import java.util.List; /** * #(tableComment) 控制层。 @@ -36,9 +43,52 @@ public class #(table.buildControllerClassName()) #if(controllerConfig.supperClas @Autowired private #(table.buildServiceClassName()) #(serviceVarName); - + /** - * 根据#(tableComment)获取详细信息。 + * 添加#(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)详情 @@ -48,4 +98,15 @@ public class #(table.buildControllerClassName()) #if(controllerConfig.supperClas 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/package-info.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/package-info.tpl index 2d8bc9e6..a86e5048 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/package-info.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/package-info.tpl @@ -1,6 +1,7 @@ /** - * #(javadocConfig.formatPackageComment(packageName)) + * #(packageComment) * + * @author #(javadocConfig.getAuthor()) * @since #(javadocConfig.getSince()) */ package #(packageName); \ No newline at end of file From b1c818a9448dc914d78bae1e823d9857ff1281ee Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 25 May 2023 21:00:59 +0800 Subject: [PATCH 26/28] =?UTF-8?q?feat:=20=E4=B8=A4=E7=A7=8D=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=B9=E5=BC=8F=E9=83=BD=E5=8F=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/ControllerConfig.java | 30 + .../codegen/config/EntityConfig.java | 38 +- .../codegen/config/GlobalConfig.java | 1083 ++++++++++++++++- .../codegen/config/JavadocConfig.java | 146 ++- .../codegen/config/MapperConfig.java | 24 + .../codegen/config/MapperXmlConfig.java | 19 + .../codegen/config/PackageConfig.java | 54 + .../codegen/config/ServiceConfig.java | 24 + .../codegen/config/ServiceImplConfig.java | 24 + .../codegen/config/StrategyConfig.java | 117 +- .../codegen/config/TableDefConfig.java | 18 + .../codegen/constant/TemplateConst.java | 4 +- .../generator/impl/PackageInfoGenerator.java | 55 +- 13 files changed, 1546 insertions(+), 90 deletions(-) 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 index 3cd804f7..7a29d2f3 100644 --- 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 @@ -57,46 +57,76 @@ public class ControllerConfig { 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; 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 index fbf2d8bb..0a3b4edf 100644 --- 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 @@ -56,55 +56,91 @@ public class EntityConfig { */ 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) { + /** + * 设置实现接口。 + */ + 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; 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 57885847..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 @@ -15,12 +15,19 @@ */ package com.mybatisflex.codegen.config; +import com.mybatisflex.codegen.template.ITemplate; + import java.util.HashMap; 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 */ @@ -66,6 +73,8 @@ public class GlobalConfig { this.templateConfig = new TemplateConfig(); } + // === 分类配置 === + public JavadocConfig getJavadocConfig() { return javadocConfig; } @@ -131,6 +140,8 @@ public class GlobalConfig { return mapperXmlConfig; } + // === 启用配置 === + public EntityConfig enableEntity() { entityGenerateEnable = true; return getEntityConfig(); @@ -163,13 +174,15 @@ public class GlobalConfig { public MapperXmlConfig enableMapperXml() { mapperXmlGenerateEnable = true; - return mapperXmlConfig; + return getMapperXmlConfig(); } public void enablePackageInfo() { packageInfoGenerateEnable = true; } + // === 禁用配置 === + public void disableEntity() { entityGenerateEnable = false; } @@ -202,37 +215,7 @@ public class GlobalConfig { packageInfoGenerateEnable = false; } - public boolean isEntityGenerateEnable() { - return entityGenerateEnable; - } - - public boolean isMapperGenerateEnable() { - return mapperGenerateEnable; - } - - public boolean isServiceGenerateEnable() { - return serviceGenerateEnable; - } - - public boolean isServiceImplGenerateEnable() { - return serviceImplGenerateEnable; - } - - public boolean isControllerGenerateEnable() { - return controllerGenerateEnable; - } - - public boolean isTableDefGenerateEnable() { - return tableDefGenerateEnable; - } - - public boolean isMapperXmlGenerateEnable() { - return mapperXmlGenerateEnable; - } - - public boolean isPackageInfoGenerateEnable() { - return packageInfoGenerateEnable; - } + // === 自定义配置 === public Object getCustomConfig(String key) { if (customConfig != null) { @@ -248,4 +231,1040 @@ public class GlobalConfig { customConfig.put(key, value); } + // === 分项配置 === + + /** + * @see JavadocConfig#getAuthor() + */ + public String getAuthor() { + return getJavadocConfig().getAuthor(); + } + + /** + * @see JavadocConfig#setAuthor(String) + */ + public void setAuthor(String author) { + getJavadocConfig().setAuthor(author); + } + + /** + * @see JavadocConfig#getSince() + */ + public String getSince() { + return getJavadocConfig().getSince(); + } + + /** + * @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() { + return getPackageConfig().getSourceDir(); + } + + /** + * @see PackageConfig#setSourceDir(String) + */ + public void setSourceDir(String sourceDir) { + getPackageConfig().setSourceDir(sourceDir); + } + + /** + * @see PackageConfig#getBasePackage() + */ + public String getBasePackage() { + return getPackageConfig().getBasePackage(); + } + + /** + * @see PackageConfig#setBasePackage(String) + */ + public void setBasePackage(String basePackage) { + getPackageConfig().setBasePackage(basePackage); + } + + /** + * @see PackageConfig#getEntityPackage() + */ + public String getEntityPackage() { + return getPackageConfig().getEntityPackage(); + } + + /** + * @see PackageConfig#setEntityPackage(String) + */ + public void setEntityPackage(String entityPackage) { + getPackageConfig().setEntityPackage(entityPackage); + } + + /** + * @see PackageConfig#getMapperPackage() + */ + public String getMapperPackage() { + return getPackageConfig().getMapperPackage(); + } + + /** + * @see PackageConfig#setMapperPackage(String) + */ + public void setMapperPackage(String mapperPackage) { + getPackageConfig().setMapperPackage(mapperPackage); + } + + /** + * @see PackageConfig#getServicePackage() + */ + public String getServicePackage() { + return getPackageConfig().getServicePackage(); + } + + /** + * @see PackageConfig#setServicePackage(String) + */ + public void setServicePackage(String servicePackage) { + getPackageConfig().setServicePackage(servicePackage); + } + + /** + * @see PackageConfig#getServiceImplPackage() + */ + public String getServiceImplPackage() { + return getPackageConfig().getServiceImplPackage(); + } + + /** + * @see PackageConfig#setServiceImplPackage(String) + */ + public void setServiceImplPackage(String serviceImplPackage) { + getPackageConfig().setServiceImplPackage(serviceImplPackage); + } + + /** + * @see PackageConfig#getControllerPackage() + */ + public String getControllerPackage() { + return getPackageConfig().getControllerPackage(); + } + + /** + * @see PackageConfig#setControllerPackage(String) + */ + public void setControllerPackage(String controllerPackage) { + getPackageConfig().setControllerPackage(controllerPackage); + } + + /** + * @see PackageConfig#getTableDefPackage() + */ + public String getTableDefPackage() { + return getPackageConfig().getTableDefPackage(); + } + + /** + * @see PackageConfig#setTableDefPackage(String) + */ + public void setTableDefPackage(String tableDefPackage) { + getPackageConfig().setTableDefPackage(tableDefPackage); + } + + /** + * @see PackageConfig#getMapperXmlPath() + */ + public String getMapperXmlPath() { + return getPackageConfig().getMapperXmlPath(); + } + + /** + * @see PackageConfig#setMapperXmlPath(String) + */ + public void setMapperXmlPath(String mapperXmlPath) { + getPackageConfig().setMapperXmlPath(mapperXmlPath); + } + + /** + * @see StrategyConfig#getTablePrefix() + */ + public String getTablePrefix() { + return getStrategyConfig().getTablePrefix(); + } + + /** + * @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 getMapperConfig().isOverwriteEnable(); + } + + /** + * @see MapperConfig#setOverwriteEnable(boolean) + */ + public void setMapperOverwriteEnable(boolean mapperOverwriteEnable) { + getMapperConfig().setOverwriteEnable(mapperOverwriteEnable); + } + + /** + * @see MapperConfig#getClassPrefix() + */ + public String getMapperClassPrefix() { + return getMapperConfig().getClassPrefix(); + } + + /** + * @see MapperConfig#setClassPrefix(String) + */ + public void setMapperClassPrefix(String mapperClassPrefix) { + getMapperConfig().setClassPrefix(mapperClassPrefix); + } + + /** + * @see MapperConfig#getClassSuffix() + */ + public String getMapperClassSuffix() { + return getMapperConfig().getClassSuffix(); + } + + /** + * @see MapperConfig#setClassSuffix(String) + */ + public void setMapperClassSuffix(String mapperClassSuffix) { + getMapperConfig().setClassSuffix(mapperClassSuffix); + } + + /** + * @see MapperConfig#getSupperClass() + */ + public Class getMapperSupperClass() { + return getMapperConfig().getSupperClass(); + } + + /** + * @see MapperConfig#setSupperClass(Class) + */ + public void setMapperSupperClass(Class 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 getServiceConfig().isOverwriteEnable(); + } + + /** + * @see ServiceConfig#setOverwriteEnable(boolean) + */ + public void setServiceOverwriteEnable(boolean serviceOverwriteEnable) { + getServiceConfig().setOverwriteEnable(serviceOverwriteEnable); + } + + /** + * @see ServiceConfig#getClassPrefix() + */ + public String getServiceClassPrefix() { + return getServiceConfig().getClassPrefix(); + } + + /** + * @see ServiceConfig#setClassPrefix(String) + */ + public void setServiceClassPrefix(String serviceClassPrefix) { + getServiceConfig().setClassPrefix(serviceClassPrefix); + } + + /** + * @see ServiceConfig#getClassSuffix() + */ + public String getServiceClassSuffix() { + return getServiceConfig().getClassSuffix(); + } + + /** + * @see ServiceConfig#setClassSuffix(String) + */ + public void setServiceClassSuffix(String serviceClassSuffix) { + getServiceConfig().setClassSuffix(serviceClassSuffix); + } + + /** + * @see ServiceConfig#getSupperClass() + */ + public Class getServiceSupperClass() { + return getServiceConfig().getSupperClass(); + } + + /** + * @see ServiceConfig#setSupperClass(Class) + */ + public void setServiceSupperClass(Class 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 getServiceImplConfig().isOverwriteEnable(); + } + + /** + * @see ServiceImplConfig#setOverwriteEnable(boolean) + */ + public void setServiceImplOverwriteEnable(boolean serviceImplOverwriteEnable) { + getServiceImplConfig().setOverwriteEnable(serviceImplOverwriteEnable); + } + + /** + * @see ServiceImplConfig#getClassPrefix() + */ + public String getServiceImplClassPrefix() { + return getServiceImplConfig().getClassPrefix(); + } + + /** + * @see ServiceImplConfig#setClassPrefix(String) + */ + public void setServiceImplClassPrefix(String serviceImplClassPrefix) { + getServiceImplConfig().setClassPrefix(serviceImplClassPrefix); + } + + /** + * @see ServiceImplConfig#getClassSuffix() + */ + public String getServiceImplClassSuffix() { + return getServiceImplConfig().getClassSuffix(); + } + + /** + * @see ServiceImplConfig#setClassSuffix(String) + */ + public void setServiceImplClassSuffix(String serviceImplClassSuffix) { + getServiceImplConfig().setClassSuffix(serviceImplClassSuffix); + } + + /** + * @see ServiceImplConfig#getSupperClass() + */ + public Class getServiceImplSupperClass() { + return getServiceImplConfig().getSupperClass(); + } + + /** + * @see ServiceImplConfig#setSupperClass(Class) + */ + public void setServiceImplSupperClass(Class 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; + } + + /** + * @see ControllerConfig#getClassPrefix() + */ + public String getControllerClassPrefix() { + return getControllerConfig().getClassPrefix(); + } + + /** + * @see ControllerConfig#setClassPrefix(String) + */ + public void setControllerClassPrefix(String controllerClassPrefix) { + getControllerConfig().setClassPrefix(controllerClassPrefix); + } + + /** + * @see ControllerConfig#getClassSuffix() + */ + public String getControllerClassSuffix() { + return getControllerConfig().getClassSuffix(); + } + + /** + * @see ControllerConfig#setClassSuffix(String) + */ + public void setControllerClassSuffix(String controllerClassSuffix) { + getControllerConfig().setClassSuffix(controllerClassSuffix); + } + + /** + * @see ControllerConfig#getSupperClass() + */ + public Class getControllerSupperClass() { + return getControllerConfig().getSupperClass(); + } + + /** + * @see ControllerConfig#setSupperClass(Class) + */ + public void setControllerSupperClass(Class controllerSupperClass) { + getControllerConfig().setSupperClass(controllerSupperClass); + } + + /** + * @see ControllerConfig#isRestStyle() + */ + public boolean isControllerRestStyle() { + return getControllerConfig().isRestStyle(); + } + + /** + * @see ControllerConfig#setRestStyle(boolean) + */ + public void setControllerRestStyle(boolean restStyle) { + getControllerConfig().setRestStyle(restStyle); + } + + public boolean isTableDefGenerateEnable() { + return tableDefGenerateEnable; + } + + /** + * @see #enableTableDef() + * @see #disableTableDef() + */ + public void setTableDefGenerateEnable(boolean tableDefGenerateEnable) { + this.tableDefGenerateEnable = tableDefGenerateEnable; + } + + /** + * @see TableDefConfig#isOverwriteEnable() + */ + public boolean isTableDefOverwriteEnable() { + return getTableDefConfig().isOverwriteEnable(); + } + + /** + * @see TableDefConfig#setOverwriteEnable(boolean) + */ + public void setTableDefOverwriteEnable(boolean tableDefOverwriteEnable) { + getTableDefConfig().setOverwriteEnable(tableDefOverwriteEnable); + } + + /** + * @see TableDefConfig#getClassPrefix() + */ + public String getTableDefClassPrefix() { + return getTableDefConfig().getClassPrefix(); + } + + /** + * @see TableDefConfig#setClassPrefix(String) + */ + public void setTableDefClassPrefix(String tableDefClassPrefix) { + getTableDefConfig().setClassPrefix(tableDefClassPrefix); + } + + /** + * @see TableDefConfig#getClassSuffix() + */ + public String getTableDefClassSuffix() { + return getTableDefConfig().getClassSuffix(); + } + + /** + * @see TableDefConfig#setClassSuffix(String) + */ + public void setTableDefClassSuffix(String tableDefClassSuffix) { + getTableDefConfig().setClassSuffix(tableDefClassSuffix); + } + + public boolean isMapperXmlGenerateEnable() { + return mapperXmlGenerateEnable; + } + + /** + * @see #enableMapperXml() + * @see #disableMapperXml() + */ + public void setMapperXmlGenerateEnable(boolean mapperXmlGenerateEnable) { + this.mapperXmlGenerateEnable = mapperXmlGenerateEnable; + } + + /** + * @see MapperXmlConfig#isOverwriteEnable() + */ + public boolean isMapperXmlOverwriteEnable() { + return getMapperXmlConfig().isOverwriteEnable(); + } + + /** + * @see MapperXmlConfig#setOverwriteEnable(boolean) + */ + public void setMapperXmlOverwriteEnable(boolean mapperXmlOverwriteEnable) { + getMapperXmlConfig().setOverwriteEnable(mapperXmlOverwriteEnable); + } + + /** + * @see MapperXmlConfig#getFilePrefix() + */ + public String getMapperXmlFilePrefix() { + return getMapperXmlConfig().getFilePrefix(); + } + + /** + * @see MapperXmlConfig#setFilePrefix(String) + */ + public void setMapperXmlFilePrefix(String mapperXmlFilePrefix) { + getMapperXmlConfig().setFilePrefix(mapperXmlFilePrefix); + } + + /** + * @see MapperXmlConfig#getFileSuffix() + */ + public String getMapperXmlFileSuffix() { + return getMapperXmlConfig().getFileSuffix(); + } + + /** + * @see MapperXmlConfig#setFileSuffix(String) + */ + public void setMapperXmlFileSuffix(String mapperXmlFileSuffix) { + getMapperXmlConfig().setFileSuffix(mapperXmlFileSuffix); + } + + public boolean isPackageInfoGenerateEnable() { + return packageInfoGenerateEnable; + } + + /** + * @see #enablePackageInfo() + * @see #disablePackageInfo() + */ + public void setPackageInfoGenerateEnable(boolean packageInfoGenerateEnable) { + this.packageInfoGenerateEnable = packageInfoGenerateEnable; + } + } 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 index f13fc8cf..2ba8beb4 100644 --- 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 @@ -20,6 +20,7 @@ import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.function.Function; import java.util.function.Supplier; +import java.util.function.UnaryOperator; /** * 注释配置类。 @@ -43,31 +44,71 @@ public class JavadocConfig { /** * 表名格式化。 */ - private Function tableCommentFormat = Function.identity(); + private UnaryOperator tableCommentFormat = UnaryOperator.identity(); /** - * 包注释格式化。 + * Entity 包注释。 */ - private Function packageCommentFormat = Function.identity(); + 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; @@ -77,25 +118,110 @@ public class JavadocConfig { return tableCommentFormat.apply(comment); } + /** + * 获取表注释格式化。 + */ public Function getTableCommentFormat() { return tableCommentFormat; } - public JavadocConfig setTableCommentFormat(Function tableCommentFormat) { + /** + * 设置表注释格式化方案,用来生成实体类注释。 + */ + public JavadocConfig setTableCommentFormat(UnaryOperator tableCommentFormat) { this.tableCommentFormat = tableCommentFormat; return this; } - public String formatPackageComment(String packageName) { - return packageCommentFormat.apply(packageName); + /** + * 获取实体类层包注释。 + */ + public String getEntityPackage() { + return entityPackage; } - public Function getPackageCommentFormat() { - return packageCommentFormat; + /** + * 设置实体类层包注释。 + */ + public JavadocConfig setEntityPackage(String entityPackage) { + this.entityPackage = entityPackage; + return this; } - public JavadocConfig setPackageCommentFormat(Function packageCommentFormat) { - this.packageCommentFormat = packageCommentFormat; + /** + * 获取映射层包注释。 + */ + 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; } 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 index 86a3f9c5..62fc4905 100644 --- 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 @@ -54,37 +54,61 @@ public class MapperConfig { 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; 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 index 4b27ee0d..dfa5fa47 100644 --- 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 @@ -22,6 +22,7 @@ package com.mybatisflex.codegen.config; * @author 王帅 * @since 2023-05-17 */ +@SuppressWarnings("unused") public class MapperXmlConfig { /** @@ -39,28 +40,46 @@ public class MapperXmlConfig { */ 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; 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 index ebfd4e83..24cbc012 100644 --- 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 @@ -72,6 +72,9 @@ public class PackageConfig { */ private String mapperXmlPath; + /** + * 获取生成目录。 + */ public String getSourceDir() { if (StringUtil.isBlank(sourceDir)) { return System.getProperty("user.dir") + "/src/main/java"; @@ -79,20 +82,32 @@ public class PackageConfig { 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"); @@ -100,11 +115,17 @@ public class PackageConfig { return entityPackage; } + /** + * 设置实体类层包路径。 + */ public PackageConfig setEntityPackage(String entityPackage) { this.entityPackage = entityPackage; return this; } + /** + * 获取映射层包路径。 + */ public String getMapperPackage() { if (StringUtil.isBlank(mapperPackage)) { return basePackage.concat(".mapper"); @@ -112,11 +133,17 @@ public class PackageConfig { return mapperPackage; } + /** + * 设置映射层包路径。 + */ public PackageConfig setMapperPackage(String mapperPackage) { this.mapperPackage = mapperPackage; return this; } + /** + * 获取服务层包路径。 + */ public String getServicePackage() { if (StringUtil.isBlank(servicePackage)) { return basePackage.concat(".service"); @@ -124,11 +151,17 @@ public class PackageConfig { return servicePackage; } + /** + * 设置服务层包路径。 + */ public PackageConfig setServicePackage(String servicePackage) { this.servicePackage = servicePackage; return this; } + /** + * 获取服务层实现包路径。 + */ public String getServiceImplPackage() { if (StringUtil.isBlank(serviceImplPackage)) { return basePackage.concat(".service.impl"); @@ -136,11 +169,17 @@ public class PackageConfig { return serviceImplPackage; } + /** + * 设置服务层实现包路径。 + */ public PackageConfig setServiceImplPackage(String serviceImplPackage) { this.serviceImplPackage = serviceImplPackage; return this; } + /** + * 获取控制层包路径。 + */ public String getControllerPackage() { if (StringUtil.isBlank(controllerPackage)) { return basePackage.concat(".controller"); @@ -148,11 +187,17 @@ public class PackageConfig { return controllerPackage; } + /** + * 设置控制层包路径。 + */ public PackageConfig setControllerPackage(String controllerPackage) { this.controllerPackage = controllerPackage; return this; } + /** + * 获取表定义层包路径。 + */ public String getTableDefPackage() { if (StringUtil.isBlank(tableDefPackage)) { return getEntityPackage().concat(".tables"); @@ -160,11 +205,17 @@ public class PackageConfig { 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"); @@ -172,6 +223,9 @@ public class PackageConfig { return mapperXmlPath; } + /** + * 设置 Mapper XML 文件路径。 + */ public PackageConfig setMapperXmlPath(String mapperXmlPath) { this.mapperXmlPath = mapperXmlPath; return this; 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 index 174db0c9..0cf08862 100644 --- 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 @@ -54,37 +54,61 @@ public class ServiceConfig { 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; 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 index 274c2f90..0c0d5523 100644 --- 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 @@ -54,37 +54,61 @@ public class ServiceImplConfig { 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; 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 index b6dc157a..917053ac 100644 --- 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 @@ -61,15 +61,25 @@ public class StrategyConfig { 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<>(); @@ -77,28 +87,9 @@ public class StrategyConfig { tableConfigMap.put(tableConfig.getTableName(), tableConfig); } - public TableConfig getTableConfig(String tableName) { - return tableConfigMap == null ? null : tableConfigMap.get(tableName); - } - - 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 ColumnConfig getColumnConfig(String tableName, String columnName) { ColumnConfig columnConfig = null; @@ -129,6 +120,33 @@ public class StrategyConfig { 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<>(); @@ -141,6 +159,9 @@ public class StrategyConfig { } } + /** + * 设置不生成哪些表。 + */ public void setUnGenerateTable(String... tables) { if (unGenerateTables == null) { unGenerateTables = new HashSet<>(); @@ -172,73 +193,121 @@ public class StrategyConfig { 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; 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 index c5bd3154..f909ac11 100644 --- 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 @@ -39,28 +39,46 @@ public class TableDefConfig { */ 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; 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 index 4051d184..21a14621 100644 --- 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 @@ -24,8 +24,6 @@ package com.mybatisflex.codegen.constant; */ public final class TemplateConst { - public static final String PACKAGE_INFO = "/templates/enjoy/package-info.tpl"; - 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"; @@ -33,6 +31,8 @@ public final class TemplateConst { 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() { } 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 index ba216c19..002ffb7d 100644 --- 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 @@ -16,13 +16,16 @@ 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; /** @@ -50,49 +53,41 @@ public class PackageInfoGenerator implements IGenerator { return; } + JavadocConfig javadocConfig = globalConfig.getJavadocConfig(); PackageConfig packageConfig = globalConfig.getPackageConfig(); String sourceDir = packageConfig.getSourceDir(); - Map map = new HashMap<>(6); + List dataList = new ArrayList<>(); if (globalConfig.isEntityGenerateEnable()) { - String entityPackage = packageConfig.getEntityPackage(); - map.put(entityPackage, getFilePath(sourceDir, entityPackage)); + dataList.add(new Data(sourceDir, packageConfig.getEntityPackage(), javadocConfig.getEntityPackage())); } if (globalConfig.isMapperGenerateEnable()) { - String mapperPackage = packageConfig.getMapperPackage(); - map.put(mapperPackage, getFilePath(sourceDir, mapperPackage)); + dataList.add(new Data(sourceDir, packageConfig.getMapperPackage(), javadocConfig.getMapperPackage())); } if (globalConfig.isServiceGenerateEnable()) { - String servicePackage = packageConfig.getServicePackage(); - map.put(servicePackage, getFilePath(sourceDir, servicePackage)); + dataList.add(new Data(sourceDir, packageConfig.getServicePackage(), javadocConfig.getServicePackage())); } if (globalConfig.isServiceImplGenerateEnable()) { - String serviceImplPackage = packageConfig.getServiceImplPackage(); - map.put(serviceImplPackage, getFilePath(sourceDir, serviceImplPackage)); + dataList.add(new Data(sourceDir, packageConfig.getServiceImplPackage(), javadocConfig.getServiceImplPackage())); } if (globalConfig.isControllerGenerateEnable()) { - String controllerPackage = packageConfig.getControllerPackage(); - map.put(controllerPackage, getFilePath(sourceDir, controllerPackage)); + dataList.add(new Data(sourceDir, packageConfig.getControllerPackage(), javadocConfig.getControllerPackage())); } if (globalConfig.isTableDefGenerateEnable()) { - String tableDefPackage = packageConfig.getTableDefPackage(); - map.put(tableDefPackage, getFilePath(sourceDir, tableDefPackage)); + dataList.add(new Data(sourceDir, packageConfig.getTableDefPackage(), javadocConfig.getTableDefPackage())); } - map.forEach((packageName, filePath) -> { + dataList.forEach(data -> { Map params = new HashMap<>(3); - params.put("packageName", packageName); - params.put("javadocConfig", globalConfig.getJavadocConfig()); - globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, filePath); + params.put("packageName", data.packageName); + params.put("packageComment", data.packageComment); + params.put("javadocConfig", javadocConfig); + globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, data.filePath); }); } - private File getFilePath(String sourceDir, String packageName) { - return new File(sourceDir, packageName.replace(".", "/") + "/package-info.java"); - } - public String getTemplatePath() { return templatePath; } @@ -101,4 +96,22 @@ public class PackageInfoGenerator implements IGenerator { 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"); + } + + } + } From 0be1088b074f77e801aace3817f85cb766a40434 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 25 May 2023 21:01:15 +0800 Subject: [PATCH 27/28] =?UTF-8?q?test:=20=E4=B8=A4=E7=A7=8D=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=B9=E5=BC=8F=E7=9A=84=E6=B5=8B=E8=AF=95=E6=A0=B7?= =?UTF-8?q?=E4=BE=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/test/GeneratorTest.java | 152 ++++++++---------- 1 file changed, 64 insertions(+), 88 deletions(-) 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 839db8ae..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 @@ -17,79 +17,17 @@ 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.Function; +import java.util.function.UnaryOperator; public class GeneratorTest { - // @Test - public void testGenerator() { - //配置数据源 - 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.setUsername("root"); - dataSource.setPassword("123456"); - - // JdbcTypeMapping.registerMapping(BigInteger.class, Long.class); - // JdbcTypeMapping.registerMapping(Integer.class, Long.class); - - GlobalConfig globalConfig = new GlobalConfig(); - - //设置生成文件目录和根包 - globalConfig.getPackageConfig() - .setSourceDir(System.getProperty("user.dir") + "/src/test/java") - .setBasePackage("com.test"); - - //设置只生成哪些表 - globalConfig.getStrategyConfig() - .setGenerateTable("account", "account_session"); - - //设置生成 entity - globalConfig.enableEntity() - .setWithLombok(true) - .setClassPrefix("My") - .setClassSuffix("Entity") - .setSupperClass(BaseEntity.class); - - //设置生成 tableDef - globalConfig.enableTableDef(); - - //设置生成 mapper - globalConfig.enableMapper() - .setClassPrefix("Flex") - .setClassSuffix("Dao") - .setSupperClass(MyBaseMapper.class); - - TableConfig tableConfig = new TableConfig(); - tableConfig.setTableName("account"); - tableConfig.setUpdateListenerClass(MyUpdateListener.class); - globalConfig.getStrategyConfig().setTableConfig(tableConfig); - - - //可以单独配置某个列 - ColumnConfig columnConfig = new ColumnConfig(); - columnConfig.setColumnName("tenant_id"); - columnConfig.setLarge(true); - columnConfig.setVersion(true); - globalConfig.getStrategyConfig().setColumnConfig("account", columnConfig); - - - //通过 datasource 和 globalConfig 创建代码生成器 - Generator generator = new Generator(dataSource, globalConfig); - - //开始生成代码 - generator.generate(); - } - - @Test - public void testCodeGen() { + //@Test + public void testCodeGen1() { //配置数据源 HikariDataSource dataSource = new HikariDataSource(); dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8"); @@ -99,32 +37,68 @@ public class GeneratorTest { GlobalConfig globalConfig = new GlobalConfig(); //用户信息表,用于存放用户信息。 -> 用户信息 - Function tableFormat = (e) -> e.split(",")[0].replace("表", ""); - //包注释生成 - Function packageFormat = (e) -> { - String[] s = e.split("\\."); - switch (s[s.length - 1]) { - case "entity": - return "实体类 软件包。"; - case "mapper": - return "映射层 软件包。"; - case "service": - return "服务层 软件包。"; - case "impl": - return "服务层实现 软件包。"; - case "controller": - return "控制层 软件包。"; - default: - return e; - } - }; + 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); + + //配置生成 mapper + globalConfig.setMapperGenerateEnable(true); + //配置生成 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); + + //开始生成代码 + generator.generate(); + } + + @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(); + + //用户信息表,用于存放用户信息。 -> 用户信息 + UnaryOperator tableFormat = (e) -> e.split(",")[0].replace("表", ""); //设置注解生成配置 globalConfig.getJavadocConfig() .setAuthor("王帅") - .setTableCommentFormat(tableFormat) - .setPackageCommentFormat(packageFormat); + .setTableCommentFormat(tableFormat); //设置生成文件目录和根包 globalConfig.getPackageConfig() @@ -137,11 +111,13 @@ public class GeneratorTest { .setTablePrefix("sys_") .setGenerateTable("sys_user"); + //设置模板路径 globalConfig.getTemplateConfig() .setEntity("D:\\Documents\\配置文件\\entity.tpl"); //配置生成 entity globalConfig.enableEntity() + .setOverwriteEnable(true) .setWithLombok(true) .setSupperClass(BaseEntity.class); @@ -154,9 +130,9 @@ public class GeneratorTest { //配置生成 controller globalConfig.enableController(); //配置生成 tableDef - //globalConfig.enableTableDef(); + globalConfig.enableTableDef(); //配置生成 mapperXml - //globalConfig.enableMapperXml(); + globalConfig.enableMapperXml(); //配置生成 package-info.java globalConfig.enablePackageInfo(); From 4ca0214084948c6927fda44b3f3cb990d12eb3d6 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Thu, 25 May 2023 21:19:13 +0800 Subject: [PATCH 28/28] =?UTF-8?q?doc:=20=E9=85=8D=E7=BD=AE=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E6=96=87=E6=A1=A3=E8=AF=B4=E6=98=8E=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/others/codegen.md | 104 ++++++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 28 deletions(-) diff --git a/docs/zh/others/codegen.md b/docs/zh/others/codegen.md index c894bdd9..07f6f6f7 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,15 +35,54 @@ ```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 = createGlobalConfigUseStyle1(); + //GlobalConfig globalConfig = createGlobalConfigUseStyle2(); + + //通过 datasource 和 globalConfig 创建代码生成器 + Generator generator = new Generator(dataSource, globalConfig); + + //生成代码 + 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(); @@ -52,7 +93,7 @@ public class Codegen { //设置表前缀和只生成哪些表 globalConfig.getStrategyConfig() .setTablePrefix("tb_") - .addGenerateTable("account", "account_session"); + .setGenerateTable("account", "account_session"); //设置生成 entity 并启用 Lombok globalConfig.enableEntity() @@ -67,13 +108,9 @@ public class Codegen { columnConfig.setLarge(true); columnConfig.setVersion(true); globalConfig.getStrategyConfig() - .addColumnConfig("account", columnConfig); + .setColumnConfig("account", columnConfig); - //通过 datasource 和 globalConfig 创建代码生成器 - Generator generator = new Generator(dataSource, globalConfig); - - //生成代码 - generator.generate(); + return globalConfig; } } ``` @@ -85,7 +122,7 @@ public class Codegen { ## 使用介绍 -在 Mybatis-Flex 的代码生成器中,支持如下 7 种类型的的产物生成: +在 Mybatis-Flex 的代码生成器中,支持如下 8 种类型的的产物生成: - Entity 实体类 - Mapper 映射类 @@ -94,6 +131,7 @@ public class Codegen { - ServiceImpl 服务实现类 - Controller 控制类 - MapperXml 文件 +- package-info.java 文件 启用或关闭某种类型产物的生成,代码如下: @@ -115,6 +153,8 @@ globalConfig.enableEntity() ## 全局配置 `GlobalConfig` +> 可以像先前一样直接使用 `setXxx()` 进行配置,也可以使用 `getXxxConfig().setXxx()` 进行分类配置。 + | 获取配置 | 描述 | |------------------------|------------------| | getJavadocConfig() | 注释配置 | @@ -156,12 +196,17 @@ globalConfig.enableEntity() ## 注释配置 `JavadocConfig` -| 配置 | 描述 | 默认值 | -|-----------------------------------|-------|---------------------------------| -| setAuthor(String) | 作者 | System.getProperty("user.name") | -| setSince(String) | 自 | 日期(yyyy-MM-dd) | -| setTableCommentFormat(Function) | 表名格式化 | 原表名 | -| setPackageCommentFormat(Function) | 包名格式化 | 原包名 | +| 配置 | 描述 | 默认值 | +|---------------------------------|-----------------|---------------------------------| +| 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() @@ -205,7 +250,7 @@ globalConfig.getPackageConfig() ```java globalConfig.getStrategyConfig() .setTablePrefix("sys_") - .setGenerateTables("sys_user", "sys_dept"); + .setGenerateTables("sys_user","sys_dept"); ``` ## 模板配置 `TemplateConfig` @@ -313,11 +358,11 @@ globalConfig.getControllerConfig() ## TableDef 生成配置 `TableDefConfig` -| 配置 | 描述 | 默认值 | -|------------------------|---------------|-------| -| setClassPrefix(String) | TableDef 类的前缀 | "" | -| setClassSuffix(String) | TableDef 类的后缀 | "Def" | -| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false | +| 配置 | 描述 | 默认值 | +|-----------------------------|---------------|-------| +| setClassPrefix(String) | TableDef 类的前缀 | "" | +| setClassSuffix(String) | TableDef 类的后缀 | "Def" | +| setOverwriteEnable(boolean) | 是否覆盖之前生成的文件 | false | ```java globalConfig.getTableDefConfig() @@ -392,7 +437,7 @@ public class ColumnConfig implements Serializable { private KeyType keyType; private String keyValue; private Boolean keyBefore; - + // 是否是租户列 private Boolean tenantId; } @@ -404,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 { @@ -507,6 +554,7 @@ public class HtmlGenerator implements IGenerator { } } ``` + 最后,通过 `GeneratorFactory` 来注册 `HtmlGenerator` 即可: ```java