From ea631ee42b7a6aa7249ed760aa67e14eaa0dffa1 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Wed, 10 Feb 2021 14:52:28 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=99=A8=E6=A8=A1=E6=9D=BF=E7=9A=84=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=202.=20=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/impl/ToolCodegenEngine.java | 60 ++++++++++++++----- .../{ => java}/controller/controller.vm | 0 .../{ => java}/controller/vo/_column.vm | 0 .../{ => java}/controller/vo/baseVO.vm | 2 +- .../{ => java}/controller/vo/createReqVO.vm | 2 +- .../{ => java}/controller/vo/pageReqVO.vm | 0 .../{ => java}/controller/vo/respVO.vm | 0 .../{ => java}/controller/vo/updateReqVO.vm | 2 +- .../codegen/{ => java}/convert/convert.vm | 0 .../resources/codegen/{ => java}/dal/do.vm | 0 .../codegen/{ => java}/dal/mapper.vm | 0 .../codegen/{ => java}/enums/errorcode.vm | 0 .../codegen/{ => java}/service/service.vm | 0 .../codegen/{ => java}/service/serviceImpl.vm | 2 +- 14 files changed, 50 insertions(+), 18 deletions(-) rename src/main/resources/codegen/{ => java}/controller/controller.vm (100%) rename src/main/resources/codegen/{ => java}/controller/vo/_column.vm (100%) rename src/main/resources/codegen/{ => java}/controller/vo/baseVO.vm (91%) rename src/main/resources/codegen/{ => java}/controller/vo/createReqVO.vm (94%) rename src/main/resources/codegen/{ => java}/controller/vo/pageReqVO.vm (100%) rename src/main/resources/codegen/{ => java}/controller/vo/respVO.vm (100%) rename src/main/resources/codegen/{ => java}/controller/vo/updateReqVO.vm (94%) rename src/main/resources/codegen/{ => java}/convert/convert.vm (100%) rename src/main/resources/codegen/{ => java}/dal/do.vm (100%) rename src/main/resources/codegen/{ => java}/dal/mapper.vm (100%) rename src/main/resources/codegen/{ => java}/enums/errorcode.vm (100%) rename src/main/resources/codegen/{ => java}/service/service.vm (100%) rename src/main/resources/codegen/{ => java}/service/serviceImpl.vm (96%) diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java index 8dc577148..7eef432a7 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java @@ -1,5 +1,7 @@ package cn.iocoder.dashboard.modules.tool.service.codegen.impl; +import cn.hutool.core.map.MapUtil; +import cn.hutool.core.util.StrUtil; import cn.hutool.extra.template.TemplateConfig; import cn.hutool.extra.template.TemplateEngine; import cn.hutool.extra.template.TemplateUtil; @@ -14,6 +16,7 @@ import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColum import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; import cn.iocoder.dashboard.util.collection.CollectionUtils; import cn.iocoder.dashboard.util.date.DateUtils; +import com.google.common.collect.Maps; import org.springframework.stereotype.Component; import javax.annotation.Resource; @@ -34,6 +37,29 @@ import static cn.hutool.core.text.CharSequenceUtil.*; @Component public class ToolCodegenEngine { + /** + * 模板配置 + * key:模板在 resources 的地址 + * value:生成的路径 + */ + private static final Map TEMPLATES = MapUtil.builder() + // Java + .put("codegen/java/controller/controller.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/${table.className}Controller.java") + .put("codegen/java/controller/vo/baseVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}BaseVO.java") + .put("codegen/java/controller/vo/createReqVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}CreateReqVO.java") + .put("codegen/java/controller/vo/pageReqVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}PageReqVO.java") + .put("codegen/java/controller/vo/respVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}RespVO.java") + .put("codegen/java/controller/vo/updateReqVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}UpdateReqVO.java") + .put("codegen/java/convert/convert.vm", "java/${basePackage}/${table.moduleName}/convert/${table.businessName}/${table.className}Convert.java") + .put("codegen/java/dal/do.vm", "java/${basePackage}/${table.moduleName}/dal/dataobject/${table.businessName}/${table.className}DO.java") + .put("codegen/java/dal/mapper.vm", "java/${basePackage}/${table.moduleName}/dal/mysql/${table.businessName}/${table.className}Mapper.java") + .put("codegen/java/enums/errorcode.vm", "java/${basePackage}.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.java") + .put("codegen/java/service/service.vm", "java/${basePackage}/${table.moduleName}/service/${table.businessName}/${table.className}Service.java") + .put("codegen/java/service/serviceImpl.vm", "java/${basePackage}/${table.moduleName}/service/${table.businessName}/impl/${table.className}ServiceImpl.java") + // Vue + // SQL + .build(); + @Resource private ToolCodegenBuilder codegenBuilder; @@ -72,7 +98,7 @@ public class ToolCodegenEngine { globalBindingMap.put("BaseMapperClassName", BaseMapperX.class.getName()); } - public void execute(ToolCodegenTableDO table, List columns) { + public Map execute(ToolCodegenTableDO table, List columns) { // 创建 bindingMap Map bindingMap = new HashMap<>(globalBindingMap); bindingMap.put("table", table); @@ -81,6 +107,7 @@ public class ToolCodegenEngine { // moduleName 相关 String simpleModuleName = codegenBuilder.getSimpleModuleName(table.getModuleName()); bindingMap.put("simpleModuleName", simpleModuleName); // 将 system 转成 sys + bindingMap.put("simpleModuleName_upperFirst", upperFirst(simpleModuleName)); // 将 sys 转成 Sys // className 相关 String simpleClassName = subAfter(table.getClassName(), upperFirst(simpleModuleName) , false); // 将 TestDictType 转换成 DictType. 因为在 create 等方法后,不需要带上 Test 前缀 @@ -88,20 +115,25 @@ public class ToolCodegenEngine { bindingMap.put("simpleClassName_underlineCase", toUnderlineCase(simpleClassName)); // 将 DictType 转换成 dict_type bindingMap.put("classNameVar", lowerFirst(simpleClassName)); // 将 DictType 转换成 dictType,用于变量 bindingMap.put("simpleClassName_strikeCase", toSymbolCase(simpleClassName, '-')); // 将 DictType 转换成 dict-type + // 执行生成 -// String result = templateEngine.getTemplate("codegen/dal/do.vm").render(bindingMap); -// String result = templateEngine.getTemplate("codegen/dal/mapper.vm").render(bindingMap); -// String result = templateEngine.getTemplate("codegen/controller/vo/pageReqVO.vm").render(bindingMap); -// String result = templateEngine.getTemplate("codegen/controller/vo/baseVO.vm").render(bindingMap); -// String result = templateEngine.getTemplate("codegen/controller/vo/createReqVO.vm").render(bindingMap); -// String result = templateEngine.getTemplate("codegen/controller/vo/updateReqVO.vm").render(bindingMap); - String result = templateEngine.getTemplate("codegen/controller/vo/respVO.vm").render(bindingMap); -// String result = templateEngine.getTemplate("codegen/convert/convert.vm").render(bindingMap); -// String result = templateEngine.getTemplate("codegen/enums/errorcode.vm").render(bindingMap); -// String result = templateEngine.getTemplate("codegen/service/service.vm").render(bindingMap); -// String result = templateEngine.getTemplate("codegen/service/serviceImpl.vm").render(bindingMap); -// String result = templateEngine.getTemplate("codegen/controller/controller.vm").render(bindingMap); - System.out.println(result); + final Map result = Maps.newHashMapWithExpectedSize(TEMPLATES.size()); + TEMPLATES.forEach((vmPath, filePath) -> { + filePath = formatFilePath(filePath, bindingMap); + String content = templateEngine.getTemplate(vmPath).render(bindingMap); + result.put(filePath, content); + }); + return result; + } + + private String formatFilePath(String filePath, Map bindingMap) { + filePath = StrUtil.replace(filePath, "${basePackage}", ((String) bindingMap.get("basePackage")).replaceAll("\\.", "/")); + ToolCodegenTableDO table = (ToolCodegenTableDO) bindingMap.get("table"); + filePath = StrUtil.replace(filePath, "${simpleModuleName_upperFirst}", (String) bindingMap.get("simpleModuleName_upperFirst")); + filePath = StrUtil.replace(filePath, "${table.moduleName}", table.getModuleName()); + filePath = StrUtil.replace(filePath, "${table.businessName}", table.getBusinessName()); + filePath = StrUtil.replace(filePath, "${table.className}", table.getClassName()); + return filePath; } } diff --git a/src/main/resources/codegen/controller/controller.vm b/src/main/resources/codegen/java/controller/controller.vm similarity index 100% rename from src/main/resources/codegen/controller/controller.vm rename to src/main/resources/codegen/java/controller/controller.vm diff --git a/src/main/resources/codegen/controller/vo/_column.vm b/src/main/resources/codegen/java/controller/vo/_column.vm similarity index 100% rename from src/main/resources/codegen/controller/vo/_column.vm rename to src/main/resources/codegen/java/controller/vo/_column.vm diff --git a/src/main/resources/codegen/controller/vo/baseVO.vm b/src/main/resources/codegen/java/controller/vo/baseVO.vm similarity index 91% rename from src/main/resources/codegen/controller/vo/baseVO.vm rename to src/main/resources/codegen/java/controller/vo/baseVO.vm index 324e60e8f..4eba0097d 100644 --- a/src/main/resources/codegen/controller/vo/baseVO.vm +++ b/src/main/resources/codegen/java/controller/vo/baseVO.vm @@ -14,7 +14,7 @@ public class ${table.className}BaseVO { #foreach ($column in $columns) #if (${column.createOperation} && ${column.updateOperation} && ${column.listOperationResult})##通用操作 - #parse("codegen/controller/vo/_column.vm") + #parse("codegen/java/controller/vo/_column.vm") #end #end diff --git a/src/main/resources/codegen/controller/vo/createReqVO.vm b/src/main/resources/codegen/java/controller/vo/createReqVO.vm similarity index 94% rename from src/main/resources/codegen/controller/vo/createReqVO.vm rename to src/main/resources/codegen/java/controller/vo/createReqVO.vm index 82c191107..3ef3618cd 100644 --- a/src/main/resources/codegen/controller/vo/createReqVO.vm +++ b/src/main/resources/codegen/java/controller/vo/createReqVO.vm @@ -23,7 +23,7 @@ public class ${table.className}CreateReqVO extends ${table.className}BaseVO { #foreach ($column in $columns) #if (${column.createOperation} && (!${column.updateOperation} || !${column.listOperationResult}))##不是通用字段 - #parse("codegen/controller/vo/_column.vm") + #parse("codegen/java/controller/vo/_column.vm") #end #end diff --git a/src/main/resources/codegen/controller/vo/pageReqVO.vm b/src/main/resources/codegen/java/controller/vo/pageReqVO.vm similarity index 100% rename from src/main/resources/codegen/controller/vo/pageReqVO.vm rename to src/main/resources/codegen/java/controller/vo/pageReqVO.vm diff --git a/src/main/resources/codegen/controller/vo/respVO.vm b/src/main/resources/codegen/java/controller/vo/respVO.vm similarity index 100% rename from src/main/resources/codegen/controller/vo/respVO.vm rename to src/main/resources/codegen/java/controller/vo/respVO.vm diff --git a/src/main/resources/codegen/controller/vo/updateReqVO.vm b/src/main/resources/codegen/java/controller/vo/updateReqVO.vm similarity index 94% rename from src/main/resources/codegen/controller/vo/updateReqVO.vm rename to src/main/resources/codegen/java/controller/vo/updateReqVO.vm index e10abcc10..173b8dbcf 100644 --- a/src/main/resources/codegen/controller/vo/updateReqVO.vm +++ b/src/main/resources/codegen/java/controller/vo/updateReqVO.vm @@ -23,7 +23,7 @@ public class ${table.className}UpdateReqVO extends ${table.className}BaseVO { #foreach ($column in $columns) #if (${column.updateOperation} && (!${column.createOperation} || !${column.listOperationResult}))##不是通用字段 - #parse("codegen/controller/vo/_column.vm") + #parse("codegen/java/controller/vo/_column.vm") #end #end diff --git a/src/main/resources/codegen/convert/convert.vm b/src/main/resources/codegen/java/convert/convert.vm similarity index 100% rename from src/main/resources/codegen/convert/convert.vm rename to src/main/resources/codegen/java/convert/convert.vm diff --git a/src/main/resources/codegen/dal/do.vm b/src/main/resources/codegen/java/dal/do.vm similarity index 100% rename from src/main/resources/codegen/dal/do.vm rename to src/main/resources/codegen/java/dal/do.vm diff --git a/src/main/resources/codegen/dal/mapper.vm b/src/main/resources/codegen/java/dal/mapper.vm similarity index 100% rename from src/main/resources/codegen/dal/mapper.vm rename to src/main/resources/codegen/java/dal/mapper.vm diff --git a/src/main/resources/codegen/enums/errorcode.vm b/src/main/resources/codegen/java/enums/errorcode.vm similarity index 100% rename from src/main/resources/codegen/enums/errorcode.vm rename to src/main/resources/codegen/java/enums/errorcode.vm diff --git a/src/main/resources/codegen/service/service.vm b/src/main/resources/codegen/java/service/service.vm similarity index 100% rename from src/main/resources/codegen/service/service.vm rename to src/main/resources/codegen/java/service/service.vm diff --git a/src/main/resources/codegen/service/serviceImpl.vm b/src/main/resources/codegen/java/service/serviceImpl.vm similarity index 96% rename from src/main/resources/codegen/service/serviceImpl.vm rename to src/main/resources/codegen/java/service/serviceImpl.vm index 488c88e22..1803f9e9f 100644 --- a/src/main/resources/codegen/service/serviceImpl.vm +++ b/src/main/resources/codegen/java/service/serviceImpl.vm @@ -16,7 +16,7 @@ import ${basePackage}.${table.moduleName}.service.${table.businessName}.${table. import ${ServiceExceptionUtilClassName}; -import static ${basePackage}.${table.moduleName}.enums.${simpleModuleName.substring(0,1).toUpperCase()}${simpleModuleName.substring(1)}ErrorCodeConstants.*; +import static ${basePackage}.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.*; /** * ${table.classComment} Service 实现类