312-frameworks-spring-data-jdbc

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Spring Data JDBC with Records

使用 Records 的 Spring Data JDBC

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 指南。
此 Skill 涵盖哪些内容?
  • 将 Records 用于实体类(不可变、便于使用构造函数)
  • 当 record 名称与表名不同时,使用 @Table 指定名称
  • 使用 @Embedded 将值对象列内联到父记录中,无需单独的表
  • Repository pattern
  • 使用静态工厂方法创建新行,并使用 with* 辅助方法进行不可变更新
  • 由 @Id 的可空性决定 save() 执行 INSERT 还是 UPDATE
  • 聚合边界:每个聚合根使用一个 repository,在聚合根内部使用 Set 表示一对多关系,聚合之间使用外键
  • 使用 @Query 和命名参数自定义查询(禁止拼接用户输入)
  • 事务管理(在 service 层使用 @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
    ./mvnw compile
    or
    mvn compile
    before applying any change
  • SAFETY: If compilation fails, stop immediately
  • VERIFY: Run
    ./mvnw clean verify
    or
    mvn clean verify
    after applying improvements
  • BEFORE APPLYING: Read the reference for detailed rules and good/bad patterns
  • EDGE CASE: If request scope is ambiguous, stop and ask a clarifying question before applying changes
  • EDGE CASE: If required inputs, files, or tooling are missing, report what is missing and ask whether to proceed with setup guidance
应用任何 Spring Data JDBC 更改之前,确保项目能够编译。如果编译失败,立即停止。应用改进后,运行完整验证。
  • 强制要求:应用任何更改之前,运行
    ./mvnw compile
    或
    mvn compile
  • 安全要求:如果编译失败,立即停止
  • 验证:应用改进后,运行
    ./mvnw clean verify
    或
    mvn clean verify
  • 应用之前:阅读参考资料,了解详细规则以及正确和错误的模式
  • 边缘情况:如果请求范围不明确,停止操作,并在应用更改前提出澄清问题
  • 边缘情况:如果缺少必要的输入、文件或工具,请说明缺少的内容,并询问是否继续提供设置指导

When to use this skill

何时使用此 Skill

  • Review Java code for Spring Data JDBC
  • Apply best practices for Spring Data JDBC in Java code
  • Model Spring Data JDBC aggregates with Java records
  • Review Spring Data JDBC repositories and aggregate boundaries
  • Improve optimistic locking or domain events in Spring Data JDBC
  • 审查 Spring Data JDBC 的 Java 代码
  • 在 Java 代码中应用 Spring Data JDBC 最佳实践
  • 使用 Java records 建模 Spring Data JDBC 聚合
  • 审查 Spring Data JDBC repository 和聚合边界
  • 改进 Spring Data JDBC 中的乐观锁或领域事件

Workflow

工作流

  1. Read reference and assess project context
Read
references/312-frameworks-spring-data-jdbc.md
and inspect the current project setup before proposing changes.
  1. Gather scope and decide target improvements
Identify requested outcomes, constraints, and the minimum safe set of changes to apply.
  1. Apply framework-aligned changes
Implement or refactor configuration/code following the reference patterns and project conventions.
  1. Run verification and report results
Execute appropriate build/tests and summarize what changed, what was verified, and any follow-up actions.
  1. 阅读参考资料并评估项目上下文
阅读
references/312-frameworks-spring-data-jdbc.md
,并在提出更改建议之前检查当前项目设置。
  1. 确定范围并决定目标改进项
确定请求的预期结果、约束条件,以及需要应用的最少且安全的更改。
  1. 应用符合框架规范的更改
按照参考模式及项目约定实施或重构配置和代码。
  1. 运行验证并报告结果
执行适当的构建和测试,并总结发生了哪些更改、验证了什么,以及需要采取哪些后续操作。

Reference

参考资料

For detailed guidance, examples, and constraints, see references/312-frameworks-spring-data-jdbc.md.
有关详细指南、示例和约束,请参阅 references/312-frameworks-spring-data-jdbc.md。