Feature Refactoring backend #170
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Spring Boot代码生成器项目重构说明文档
1. 重构概述
本项目旨在对Spring Boot代码生成器进行现代化重构,使其具有更清晰的架构、更好的可维护性和更强的扩展性。重构遵循现代Spring Boot应用的最佳实践,采用了分层架构设计和多种设计模式。
2. 重构目标
3. 重构后项目结构
4. 各层详细说明
4.1 控制层 (Controller)
控制层负责处理HTTP请求,协调业务逻辑并返回结果:
PageController:
CodeGenController:
TemplateController:
4.2 服务层 (Service)
服务层采用接口与实现分离的设计,便于测试和扩展:
接口层:
实现层:
4.3 实体层 (Entity)
实体层按照用途分类,避免不同类型对象混用:
DTO (Data Transfer Object):
VO (View Object):
Enums:
4.4 工具层 (Util)
工具层包含各种通用工具类和自定义异常:
工具类:
异常类:
5. 关键设计模式应用
5.1 策略模式
在SQL解析功能中应用策略模式,将不同的解析方式封装成独立的策略类:
SqlParserServiceImpl中实现了多种SQL解析方法:
processTableIntoClassInfo: 默认SQL解析generateSelectSqlBySQLPraser: SELECT SQL解析generateCreateSqlBySQLPraser: CREATE SQL解析processTableToClassInfoByRegex: 正则表达式解析processInsertSqlToClassInfo: INSERT SQL解析JsonParserServiceImpl中实现了JSON解析:
processJsonToClassInfo: JSON解析通过策略模式,可以:
5.2 接口与实现分离
所有服务层都采用接口与实现分离的设计,便于:
6. 重要技术实现细节
6.1 统一响应格式
所有控制器方法均返回 ResultVo 统一响应对象,保持与前端的兼容性:
6.2 前后端兼容性处理
为了保持与现有前端JavaScript代码的兼容性,在处理响应数据时特别注意了数据结构:
模板获取接口返回数据结构:
{ "code": 200, "msg": "success", "templates": [...] }代码生成接口返回数据结构:
{ "code": 200, "msg": "success", "outputJson": { "tableName": "...", "controller": "...", "service": "...", // 其他模板生成的代码 } }6.3 组件扫描配置
由于服务实现类位于不同的包层级中,已在 Application 类中配置了组件扫描路径:
确保所有服务实现类都能被正确扫描和注入。
7. 重构优势总结
8. 后续优化建议