312-frameworks-spring-data-jdbc
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSpring Data JDBC with Records
Spring Data JDBC with Records
Apply Spring Data JDBC guidelines with Java records.
What is covered in this Skill?
- Records for entity classes (immutable, constructor-friendly)
- @Table for naming when record name differs from the table name
- @Embedded to inline value-object columns into the parent row without a separate table
- Repository pattern
- Immutable updates with static factories for new rows and with* helpers for updates
- save() INSERT vs UPDATE semantics driven by @Id nullability
- Aggregate boundaries: one repository per aggregate root, Set for one-to-many inside the root, foreign keys between aggregates
- Custom queries with @Query and named parameters (no user-input concatenation)
- Transaction management (@Transactional on services; readOnly where appropriate)
- Single query loading (N+1 avoidance)
Scope: Apply recommendations based on the reference rules and good/bad code examples.
结合Java records应用Spring Data JDBC指南。
本技能涵盖哪些内容?
- 用于实体类的records(不可变、适配构造函数)
- 当record名称与表名不同时,使用@Table指定表名
- 使用@Embedded将值对象列内联到父行中,无需单独创建表
- 仓库模式
- 借助静态工厂创建新行,以及使用with*助手类实现不可变更新
- 由@Id是否为null决定save()方法的INSERT与UPDATE语义
- 聚合边界:每个聚合根对应一个仓库,聚合根内部使用Set实现一对多关系,聚合之间通过外键关联
- 使用@Query和命名参数编写自定义查询(禁止拼接用户输入)
- 事务管理(在服务层使用@Transactional;在合适的场景设置readOnly属性)
- 单查询加载(避免N+1问题)
适用范围: 基于参考规则和代码示例的优劣对比,应用相关建议。
Constraints
约束条件
Before applying any Spring Data JDBC changes, ensure the project compiles. If compilation fails, stop immediately. After applying improvements, run full verification.
- MANDATORY: Run or
./mvnw compilebefore applying any changemvn compile - SAFETY: If compilation fails, stop immediately
- VERIFY: Run or
./mvnw clean verifyafter applying improvementsmvn clean verify - BEFORE APPLYING: Read the reference for detailed rules and good/bad patterns
在进行任何Spring Data JDBC修改之前,确保项目可编译。如果编译失败,立即停止操作。在完成优化后,执行完整的验证流程。
- 强制要求:在进行任何修改前,执行或
./mvnw compilemvn compile - 安全规则:如果编译失败,立即停止
- 验证步骤:完成优化后,执行或
./mvnw clean verifymvn clean verify - 操作前准备:阅读参考文档,了解详细规则和优劣模式
When to use this skill
何时使用本技能
- Review Java code for Spring Data JDBC
- Apply best practices for Spring Data JDBC in Java code
- 审查基于Spring Data JDBC的Java代码
- 在Java代码中应用Spring Data JDBC最佳实践
Reference
参考资料
For detailed guidance, examples, and constraints, see references/312-frameworks-spring-data-jdbc.md.
如需详细指南、示例和约束说明,请查看references/312-frameworks-spring-data-jdbc.md。