From ea0db128a2f7e358b17b87769ee93d86c0e6298b Mon Sep 17 00:00:00 2001 From: ray <1490493387@qq.com> Date: Sun, 1 Sep 2024 10:29:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=9B=AE=E5=BD=95=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E4=B9=8B=E5=90=8E=E5=8C=85=E5=90=8D=E5=8F=96=E5=80=BC?= =?UTF-8?q?=E8=B0=83=E6=95=B4=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=8C=85=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/property/GeneratorProperties.java | 14 +++++++++++ .../service/impl/GeneratorServiceImpl.java | 24 ++++++++++--------- src/main/resources/application.yml | 1 + 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/youlai/boot/config/property/GeneratorProperties.java b/src/main/java/com/youlai/boot/config/property/GeneratorProperties.java index 0f3261cd..17fee904 100644 --- a/src/main/java/com/youlai/boot/config/property/GeneratorProperties.java +++ b/src/main/java/com/youlai/boot/config/property/GeneratorProperties.java @@ -57,8 +57,14 @@ public class GeneratorProperties { @Data public static class TemplateConfig { + /** + * 模板路径 (e.g. /templates/generator/controller.java.vm) + */ private String templatePath; + /** + * 子包名 (e.g. controller/service/mapper/model) + */ private String subpackageName; /** @@ -74,8 +80,16 @@ public class GeneratorProperties { @Data public static class DefaultConfig { + /** + * 作者 (e.g. Ray) + */ private String author; + /** + * 默认模块名(e.g. system) + */ + private String moduleName; + } diff --git a/src/main/java/com/youlai/boot/platform/generator/service/impl/GeneratorServiceImpl.java b/src/main/java/com/youlai/boot/platform/generator/service/impl/GeneratorServiceImpl.java index c062b7cd..3bc12317 100644 --- a/src/main/java/com/youlai/boot/platform/generator/service/impl/GeneratorServiceImpl.java +++ b/src/main/java/com/youlai/boot/platform/generator/service/impl/GeneratorServiceImpl.java @@ -42,7 +42,6 @@ import java.nio.charset.StandardCharsets; import java.util.*; import java.util.zip.ZipOutputStream; - /** * 数据库服务实现类 * @@ -113,6 +112,7 @@ public class GeneratorServiceImpl implements GeneratorService { genConfig.setEntityName(entityName); genConfig.setPackageName(YouLaiApplication.class.getPackageName()); + genConfig.setModuleName(generatorProperties.getDefaultConfig().getModuleName()); // 默认模块名 genConfig.setAuthor(generatorProperties.getDefaultConfig().getAuthor()); } @@ -225,7 +225,6 @@ public class GeneratorServiceImpl implements GeneratorService { * 删除代码生成配置 * * @param tableName 表名 - * @return */ @Override public void deleteGenConfig(String tableName) { @@ -257,14 +256,18 @@ public class GeneratorServiceImpl implements GeneratorService { GenConfig genConfig = genConfigService.getOne(new LambdaQueryWrapper() .eq(GenConfig::getTableName, tableName) ); - Assert.isTrue(genConfig != null, "未找到表生成配置"); + if (genConfig == null) { + throw new BusinessException("未找到表生成配置"); + } List fieldConfigs = genFieldConfigService.list(new LambdaQueryWrapper() .eq(GenFieldConfig::getConfigId, genConfig.getId()) .orderByAsc(GenFieldConfig::getFieldSort) ); - Assert.isTrue(CollectionUtil.isNotEmpty(fieldConfigs), "未找到字段生成配置"); + if (CollectionUtil.isEmpty(fieldConfigs)) { + throw new BusinessException("未找到字段生成配置"); + } // 遍历模板配置 Map templateConfigs = generatorProperties.getTemplateConfigs(); @@ -286,22 +289,21 @@ public class GeneratorServiceImpl implements GeneratorService { previewVO.setFileName(fileName); /* 2. 生成文件路径 */ - // com.youlai + // 包名:com.youlai.boot String packageName = genConfig.getPackageName(); - // system + // 模块名:system String moduleName = genConfig.getModuleName(); - // controller + // 子包名:controller String subpackageName = templateConfig.getSubpackageName(); - // 文件路径 src/main/java/com/youlai/system/controller + // 组合成文件路径:src/main/java/com/youlai/boot/system/controller String filePath = getFilePath(templateName, moduleName, packageName, subpackageName, entityName); previewVO.setPath(filePath); /* 3. 生成文件内容 */ - + // 将模板文件中的变量替换为具体的值 生成代码内容 String content = getCodeContent(templateConfig, genConfig, fieldConfigs); previewVO.setContent(content); - list.add(previewVO); } return list; @@ -396,7 +398,7 @@ public class GeneratorServiceImpl implements GeneratorService { bindMap.put("tableName", genConfig.getTableName()); bindMap.put("author", genConfig.getAuthor()); bindMap.put("lowerFirstEntityName", StrUtil.lowerFirst(entityName)); // UserTest → userTest - bindMap.put("kebabCaseEntityName", StrUtil.toSymbolCase(entityName,'-')); // UserTest → user-test + bindMap.put("kebabCaseEntityName", StrUtil.toSymbolCase(entityName, '-')); // UserTest → user-test bindMap.put("businessName", genConfig.getBusinessName()); bindMap.put("fieldConfigs", fieldConfigs); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 913e9673..096386d3 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -15,6 +15,7 @@ generator: # 默认配置 defaultConfig: author: youlaitech + moduleName: system # 排除数据表 excludeTables: - gen_config