用途:按照 Spring Data JPA 最佳实践创建 Repository,适用于 Spring Boot 项目的数据访问层设计。
主要能力与规范:
- 为项目建立推荐的包结构;
- 仅为聚合根(aggregate root)创建 Repository;
- 自定义查询使用 @Query 与 JPQL;
- 优先采用语义清晰的方法名,避免冗长的 finder 方法名;
- 读操作使用构造表达式(constructor expressions)或 Projections;
- 使用 default 方法封装便捷操作。
示例:EventRepository 定义于 events/domain/EventRepository.java,继承 JpaRepository<EventEntity, EventId>,包含 JPQL 查询 findUpcomingEvents 与 findByCode,以及基于 default 的 getByCode,找不到时抛出 ResourceNotFoundException。
JPA 审计:在 BaseEntity 上使用 @MappedSuperclass、@EntityListeners(AuditingEntityListener.class),并为 createdAt、updatedAt 分别添加 @CreatedDate、@LastModifiedDate;再在配置类上添加 @EnableJpaAuditing 启用审计。资料未提供依赖版本、完整项目结构及安装命令。