Commit ea631ee42b7a6aa7249ed760aa67e14eaa0dffa1
1 parent
305090b7
1. 调整代码生成器模板的目录
2. 调整代码的方法
Showing
14 changed files
with
50 additions
and
18 deletions
src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java
| 1 | 1 | package cn.iocoder.dashboard.modules.tool.service.codegen.impl; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.map.MapUtil; | |
| 4 | +import cn.hutool.core.util.StrUtil; | |
| 3 | 5 | import cn.hutool.extra.template.TemplateConfig; |
| 4 | 6 | import cn.hutool.extra.template.TemplateEngine; |
| 5 | 7 | import cn.hutool.extra.template.TemplateUtil; |
| ... | ... | @@ -14,6 +16,7 @@ import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColum |
| 14 | 16 | import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; |
| 15 | 17 | import cn.iocoder.dashboard.util.collection.CollectionUtils; |
| 16 | 18 | import cn.iocoder.dashboard.util.date.DateUtils; |
| 19 | +import com.google.common.collect.Maps; | |
| 17 | 20 | import org.springframework.stereotype.Component; |
| 18 | 21 | |
| 19 | 22 | import javax.annotation.Resource; |
| ... | ... | @@ -34,6 +37,29 @@ import static cn.hutool.core.text.CharSequenceUtil.*; |
| 34 | 37 | @Component |
| 35 | 38 | public class ToolCodegenEngine { |
| 36 | 39 | |
| 40 | + /** | |
| 41 | + * 模板配置 | |
| 42 | + * key:模板在 resources 的地址 | |
| 43 | + * value:生成的路径 | |
| 44 | + */ | |
| 45 | + private static final Map<String, String> TEMPLATES = MapUtil.<String, String>builder() | |
| 46 | + // Java | |
| 47 | + .put("codegen/java/controller/controller.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/${table.className}Controller.java") | |
| 48 | + .put("codegen/java/controller/vo/baseVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}BaseVO.java") | |
| 49 | + .put("codegen/java/controller/vo/createReqVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}CreateReqVO.java") | |
| 50 | + .put("codegen/java/controller/vo/pageReqVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}PageReqVO.java") | |
| 51 | + .put("codegen/java/controller/vo/respVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}RespVO.java") | |
| 52 | + .put("codegen/java/controller/vo/updateReqVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}UpdateReqVO.java") | |
| 53 | + .put("codegen/java/convert/convert.vm", "java/${basePackage}/${table.moduleName}/convert/${table.businessName}/${table.className}Convert.java") | |
| 54 | + .put("codegen/java/dal/do.vm", "java/${basePackage}/${table.moduleName}/dal/dataobject/${table.businessName}/${table.className}DO.java") | |
| 55 | + .put("codegen/java/dal/mapper.vm", "java/${basePackage}/${table.moduleName}/dal/mysql/${table.businessName}/${table.className}Mapper.java") | |
| 56 | + .put("codegen/java/enums/errorcode.vm", "java/${basePackage}.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.java") | |
| 57 | + .put("codegen/java/service/service.vm", "java/${basePackage}/${table.moduleName}/service/${table.businessName}/${table.className}Service.java") | |
| 58 | + .put("codegen/java/service/serviceImpl.vm", "java/${basePackage}/${table.moduleName}/service/${table.businessName}/impl/${table.className}ServiceImpl.java") | |
| 59 | + // Vue | |
| 60 | + // SQL | |
| 61 | + .build(); | |
| 62 | + | |
| 37 | 63 | @Resource |
| 38 | 64 | private ToolCodegenBuilder codegenBuilder; |
| 39 | 65 | |
| ... | ... | @@ -72,7 +98,7 @@ public class ToolCodegenEngine { |
| 72 | 98 | globalBindingMap.put("BaseMapperClassName", BaseMapperX.class.getName()); |
| 73 | 99 | } |
| 74 | 100 | |
| 75 | - public void execute(ToolCodegenTableDO table, List<ToolCodegenColumnDO> columns) { | |
| 101 | + public Map<String, String> execute(ToolCodegenTableDO table, List<ToolCodegenColumnDO> columns) { | |
| 76 | 102 | // 创建 bindingMap |
| 77 | 103 | Map<String, Object> bindingMap = new HashMap<>(globalBindingMap); |
| 78 | 104 | bindingMap.put("table", table); |
| ... | ... | @@ -81,6 +107,7 @@ public class ToolCodegenEngine { |
| 81 | 107 | // moduleName 相关 |
| 82 | 108 | String simpleModuleName = codegenBuilder.getSimpleModuleName(table.getModuleName()); |
| 83 | 109 | bindingMap.put("simpleModuleName", simpleModuleName); // 将 system 转成 sys |
| 110 | + bindingMap.put("simpleModuleName_upperFirst", upperFirst(simpleModuleName)); // 将 sys 转成 Sys | |
| 84 | 111 | // className 相关 |
| 85 | 112 | String simpleClassName = subAfter(table.getClassName(), upperFirst(simpleModuleName) |
| 86 | 113 | , false); // 将 TestDictType 转换成 DictType. 因为在 create 等方法后,不需要带上 Test 前缀 |
| ... | ... | @@ -88,20 +115,25 @@ public class ToolCodegenEngine { |
| 88 | 115 | bindingMap.put("simpleClassName_underlineCase", toUnderlineCase(simpleClassName)); // 将 DictType 转换成 dict_type |
| 89 | 116 | bindingMap.put("classNameVar", lowerFirst(simpleClassName)); // 将 DictType 转换成 dictType,用于变量 |
| 90 | 117 | bindingMap.put("simpleClassName_strikeCase", toSymbolCase(simpleClassName, '-')); // 将 DictType 转换成 dict-type |
| 118 | + | |
| 91 | 119 | // 执行生成 |
| 92 | -// String result = templateEngine.getTemplate("codegen/dal/do.vm").render(bindingMap); | |
| 93 | -// String result = templateEngine.getTemplate("codegen/dal/mapper.vm").render(bindingMap); | |
| 94 | -// String result = templateEngine.getTemplate("codegen/controller/vo/pageReqVO.vm").render(bindingMap); | |
| 95 | -// String result = templateEngine.getTemplate("codegen/controller/vo/baseVO.vm").render(bindingMap); | |
| 96 | -// String result = templateEngine.getTemplate("codegen/controller/vo/createReqVO.vm").render(bindingMap); | |
| 97 | -// String result = templateEngine.getTemplate("codegen/controller/vo/updateReqVO.vm").render(bindingMap); | |
| 98 | - String result = templateEngine.getTemplate("codegen/controller/vo/respVO.vm").render(bindingMap); | |
| 99 | -// String result = templateEngine.getTemplate("codegen/convert/convert.vm").render(bindingMap); | |
| 100 | -// String result = templateEngine.getTemplate("codegen/enums/errorcode.vm").render(bindingMap); | |
| 101 | -// String result = templateEngine.getTemplate("codegen/service/service.vm").render(bindingMap); | |
| 102 | -// String result = templateEngine.getTemplate("codegen/service/serviceImpl.vm").render(bindingMap); | |
| 103 | -// String result = templateEngine.getTemplate("codegen/controller/controller.vm").render(bindingMap); | |
| 104 | - System.out.println(result); | |
| 120 | + final Map<String, String> result = Maps.newHashMapWithExpectedSize(TEMPLATES.size()); | |
| 121 | + TEMPLATES.forEach((vmPath, filePath) -> { | |
| 122 | + filePath = formatFilePath(filePath, bindingMap); | |
| 123 | + String content = templateEngine.getTemplate(vmPath).render(bindingMap); | |
| 124 | + result.put(filePath, content); | |
| 125 | + }); | |
| 126 | + return result; | |
| 127 | + } | |
| 128 | + | |
| 129 | + private String formatFilePath(String filePath, Map<String, Object> bindingMap) { | |
| 130 | + filePath = StrUtil.replace(filePath, "${basePackage}", ((String) bindingMap.get("basePackage")).replaceAll("\\.", "/")); | |
| 131 | + ToolCodegenTableDO table = (ToolCodegenTableDO) bindingMap.get("table"); | |
| 132 | + filePath = StrUtil.replace(filePath, "${simpleModuleName_upperFirst}", (String) bindingMap.get("simpleModuleName_upperFirst")); | |
| 133 | + filePath = StrUtil.replace(filePath, "${table.moduleName}", table.getModuleName()); | |
| 134 | + filePath = StrUtil.replace(filePath, "${table.businessName}", table.getBusinessName()); | |
| 135 | + filePath = StrUtil.replace(filePath, "${table.className}", table.getClassName()); | |
| 136 | + return filePath; | |
| 105 | 137 | } |
| 106 | 138 | |
| 107 | 139 | } | ... | ... |
src/main/resources/codegen/controller/controller.vm renamed to src/main/resources/codegen/java/controller/controller.vm
src/main/resources/codegen/controller/vo/_column.vm renamed to src/main/resources/codegen/java/controller/vo/_column.vm
src/main/resources/codegen/controller/vo/baseVO.vm renamed to src/main/resources/codegen/java/controller/vo/baseVO.vm
| ... | ... | @@ -14,7 +14,7 @@ public class ${table.className}BaseVO { |
| 14 | 14 | |
| 15 | 15 | #foreach ($column in $columns) |
| 16 | 16 | #if (${column.createOperation} && ${column.updateOperation} && ${column.listOperationResult})##通用操作 |
| 17 | - #parse("codegen/controller/vo/_column.vm") | |
| 17 | + #parse("codegen/java/controller/vo/_column.vm") | |
| 18 | 18 | |
| 19 | 19 | #end |
| 20 | 20 | #end | ... | ... |
src/main/resources/codegen/controller/vo/createReqVO.vm renamed to src/main/resources/codegen/java/controller/vo/createReqVO.vm
| ... | ... | @@ -23,7 +23,7 @@ public class ${table.className}CreateReqVO extends ${table.className}BaseVO { |
| 23 | 23 | |
| 24 | 24 | #foreach ($column in $columns) |
| 25 | 25 | #if (${column.createOperation} && (!${column.updateOperation} || !${column.listOperationResult}))##不是通用字段 |
| 26 | - #parse("codegen/controller/vo/_column.vm") | |
| 26 | + #parse("codegen/java/controller/vo/_column.vm") | |
| 27 | 27 | |
| 28 | 28 | #end |
| 29 | 29 | #end | ... | ... |
src/main/resources/codegen/controller/vo/pageReqVO.vm renamed to src/main/resources/codegen/java/controller/vo/pageReqVO.vm
src/main/resources/codegen/controller/vo/respVO.vm renamed to src/main/resources/codegen/java/controller/vo/respVO.vm
src/main/resources/codegen/controller/vo/updateReqVO.vm renamed to src/main/resources/codegen/java/controller/vo/updateReqVO.vm
| ... | ... | @@ -23,7 +23,7 @@ public class ${table.className}UpdateReqVO extends ${table.className}BaseVO { |
| 23 | 23 | |
| 24 | 24 | #foreach ($column in $columns) |
| 25 | 25 | #if (${column.updateOperation} && (!${column.createOperation} || !${column.listOperationResult}))##不是通用字段 |
| 26 | - #parse("codegen/controller/vo/_column.vm") | |
| 26 | + #parse("codegen/java/controller/vo/_column.vm") | |
| 27 | 27 | |
| 28 | 28 | #end |
| 29 | 29 | #end | ... | ... |
src/main/resources/codegen/convert/convert.vm renamed to src/main/resources/codegen/java/convert/convert.vm
src/main/resources/codegen/dal/do.vm renamed to src/main/resources/codegen/java/dal/do.vm
src/main/resources/codegen/dal/mapper.vm renamed to src/main/resources/codegen/java/dal/mapper.vm
src/main/resources/codegen/enums/errorcode.vm renamed to src/main/resources/codegen/java/enums/errorcode.vm
src/main/resources/codegen/service/service.vm renamed to src/main/resources/codegen/java/service/service.vm
src/main/resources/codegen/service/serviceImpl.vm renamed to src/main/resources/codegen/java/service/serviceImpl.vm
| ... | ... | @@ -16,7 +16,7 @@ import ${basePackage}.${table.moduleName}.service.${table.businessName}.${table. |
| 16 | 16 | |
| 17 | 17 | import ${ServiceExceptionUtilClassName}; |
| 18 | 18 | |
| 19 | -import static ${basePackage}.${table.moduleName}.enums.${simpleModuleName.substring(0,1).toUpperCase()}${simpleModuleName.substring(1)}ErrorCodeConstants.*; | |
| 19 | +import static ${basePackage}.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.*; | |
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * ${table.classComment} Service 实现类 | ... | ... |