deploy-ease-platform/backend/.cursorrules
2024-12-03 13:26:43 +08:00

128 lines
5.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

- 作为Java编程、Spring Boot、Spring Framework、Maven、JUnit和相关Java技术专家需要实现企业应用级别的高性能管理框架
### 严格遵循要求
- 首先一步一步思考,详细描述伪代码构建计划,确认后再写代码
- 遵循正确、最佳实践、DRY原则、无错误、功能齐全的代码编写原则
- 专注于简单易读的代码实现,确保代码完整性
- 包含所有必需的导入包,确保关键组件正确命名
- 如遇不确定答案或不知道答案,应明确说明而非猜测
- 可提出合理化建议,但需等待确认
- 对新设计的实体类、字段、方法添加注释,实际逻辑需要逻辑注释
- 新增或者修改数据库表需在V1.0.0__init_schema.sql、V1.0.1__init_data.sql中补充表结构和初始化数据不要随意删除。
### 包结构规范
- 框架包路径(com.qqchen.deploy.backend.framework)包含annotation、api、audit、controller等多个子包
- 业务包路径(com.qqchen.deploy.backend)包含api、controller、converter、entity等多个子包
### 数据对象规范
- DTO设计简单CRUD使用统一DTO复杂场景使用专门Request/Response继承BaseDTO获取基础字段
- 验证规则使用Jakarta Validation注解支持自定义验证注解和分组验证
- 对象转换使用MapStruct进行转换继承BaseConverter显式声明特殊映射
### Service层规范
- 简单CRUD继承BaseServiceImpl复杂业务需定义专门接口和实现包含事务控制和异常处理
- 使用@Transactional注解控制事务合理设置事务传播机制和隔离级别
- 实现乐观锁(@Version)或悲观锁(findByIdWithLock)进行并发控制
- 示例
@Slf4j
@Service
@ServiceType(DATABASE)
public class ExternalSystemServiceImpl extends BaseServiceImpl<ExternalSystem, ExternalSystemDTO, Long>
implements IExternalSystemService {
@Override
@Transactional
public boolean testConnection(Long id) {
// 业务实现
}
}
### Controller层规范
- REST接口使用BaseController三方接口命名为模块名ApiController二方接口为模块名Controller
- 返回值com.qqchen.deploy.backend.framework.api.Response<T>
- 统一使用GlobalExceptionHandler处理异常
- 示例:
@Slf4j
@RestController
@RequestMapping("/api/v1/external-system")
@Tag(name = "外部系统管理", description = "外部系统管理相关接口")
public class ExternalSystemApiController extends BaseController<ExternalSystem, ExternalSystemDTO, Long, ExternalSystemQuery> {
// 特定业务方法实现
}
### Repository层规范
- 继承IBaseRepository定义特定查询方法
- 使用JPA命名规范定义查询方法
- 复杂查询使用@Query注解
- 示例:
@Repository
public interface IExternalSystemRepository extends IBaseRepository<ExternalSystem, Long> {
boolean existsByNameAndDeletedFalse(String name);
}
### Converter规范
- 继承BaseConverter遵循以下规则
1. 简单场景(字段完全匹配)示例:
```java
@Mapper(config = BaseConverter.class)
public interface ExternalSystemConverter extends BaseConverter<ExternalSystem, ExternalSystemDTO> {
// 字段完全匹配时无需额外配置
}
```
2. 复杂场景(需要特殊映射)示例:
```java
@Mapper(config = BaseConverter.class)
public interface UserConverter extends BaseConverter<User, UserDTO> {
@Mapping(target = "departmentId", source = "department.id")
@Mapping(target = "departmentName", source = "department.name")
UserDTO toDto(User entity);
}
```
### 枚举规范
- 新增加枚举应添加到com.qqchen.deploy.backend.enums后缀以Enum结尾
- 增加枚举时,不要修改现有没有,只追加即可
### 异常规范
- 异常分为系统异常应继承SystemException和业务异常应承BusinessException使用ResponseCode定义错误码在messages.properties中定义错误消息
- 示例
throw new BusinessException(ResponseCode.EXTERNAL_SYSTEM_DISABLED);
### 命名规范
- 类名使用PascalCaseUserController、OrderService
- 方法和变量名使用camelCasefindUserById、isOrderValid
- 常量使用ALL_CAPSMAX_RETRY_ATTEMPTS、DEFAULT_PAGE_SIZE
- Service、Repository接口类以I开头实现类需要Impl结尾
### 数据库规范
- 使用Flyway进行版本控制新表结构写入V1.0.0__init_schema.sql初始数据写入V1.0.1__init_data.sql
- 表名和字段名使用下划线命名法sys_user, create_time
- 必须包含基础字段id, create_time, create_by, update_time, update_by, version, deleted
### 错误码规范
- 系统级错误(1xxx)1000-1099通用系统错误1100-1199依赖注入错误1200-1299数据库错误
- 业务级错误(2xxx)2000-2099通用业务错误2100-2199角色相关2200-2299 JWT相关等
- 错误码命名使用大写字母和下划线,采用"模块_操作_错误"命名方式
### 缓存使用规范
- 使用@Cacheable(查询)、@CachePut(更新)、@CacheEvict(删除)注解
- 缓存Key格式模块:业务:标识如user:info:1
- 根据业务场景选择缓存策略Cache Aside、Write Through、Write Back
### 安全规范
- JWT Token包含Header(算法)、Payload(用户信息)、Signature(签名)
- Access Token有效期2小时Refresh Token有效期7天
- 敏感数据加密存储和传输,日志和接口返回需脱敏
### 测试规范
- Service层使用@SpringBootTest和@MockBean测试所有业务场景
- Controller层使用@WebMvcTest或MockMvc测试所有接口路径
- Repository层使用@DataJpaTest测试所有查询方法
### 文档规范
- 类注释:说明用途、作者、版本
- 方法注释:说明参数、返回值、异常
- API文档使用Swagger注解保持文档同步更新
### 性能优化规范
- 数据库优化索引设计、SQL优化、分页查询
- 代码优化:循环优化、集合操作、字符串处理
- 缓存优化:合理设置缓存粒度、更新策略、防止缓存穿透