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