spring-data-jpa by giuseppe-trisciuoglio/developer-kit
npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill spring-data-jpa要使用 Spring Data JPA 实现持久层,需要创建仓库接口,这些接口提供自动的 CRUD 操作、实体关系、查询方法以及分页、审计和性能优化等高级功能。
在以下情况下使用此技能:
@Query 注解编写查询实现仓库接口的步骤:
扩展适当的仓库接口:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
// 在此定义自定义方法
}
使用派生查询处理简单条件:
Optional<User> findByEmail(String email);
List<User> findByStatusOrderByCreatedDateDesc(String status);
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
@Query 实现自定义查询:@Query("SELECT u FROM User u WHERE u.status = :status")
List<User> findActiveUsers(@Param("status") String status);
使用适当的注解定义实体:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 100)
private String email;
}
使用适当的级联类型配置关系:
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Order> orders = new ArrayList<>();
设置数据库审计:
@CreatedDate
@Column(nullable = false, updatable = false)
private LocalDateTime createdDate;
@Query@Modifying@Transactional(readOnly = true) 标记只读操作@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
// 派生查询
List<Product> findByCategory(String category);
// 自定义查询
@Query("SELECT p FROM Product p WHERE p.price > :minPrice")
List<Product> findExpensiveProducts(@Param("minPrice") BigDecimal minPrice);
}
@Service
public class ProductService {
private final ProductRepository repository;
public Page<Product> getProducts(int page, int size) {
Pageable pageable = PageRequest.of(page, size, Sort.by("name").ascending());
return repository.findAll(pageable);
}
}
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@CreatedDate
@Column(nullable = false, updatable = false)
private LocalDateTime createdDate;
@LastModifiedDate
private LocalDateTime lastModifiedDate;
@CreatedBy
@Column(nullable = false, updatable = false)
private String createdBy;
}
final 修饰符的不可变字段@Value@Id 和 @GeneratedValue 注解@Table 和 @Column 注解@Query 以避免冗长的方法名@ParamOptional<T>@Transactional@EntityGraph 以避免 N+1 查询问题@Transactional(readOnly = true) 标记只读操作有关全面的示例、详细模式和高级配置,请参阅:
@EntityGraph 或 JOIN FETCH 来避免 N+1 查询问题。CascadeType.REMOVE 需谨慎,因为它可能导致性能问题。EAGER 获取类型;它可能导致过多的数据库查询。@Transactional(readOnly = true) 以启用优化。每周安装数
414
代码仓库
GitHub 星标数
173
首次出现
2026年2月3日
安全审计
安装于
gemini-cli324
opencode323
codex317
cursor306
github-copilot302
claude-code300
To implement persistence layers with Spring Data JPA, create repository interfaces that provide automatic CRUD operations, entity relationships, query methods, and advanced features like pagination, auditing, and performance optimization.
Use this Skill when:
@Query annotationsTo implement a repository interface:
Extend the appropriate repository interface:
@Repository public interface UserRepository extends JpaRepository<User, Long> { // Custom methods defined here }
Use derived queries for simple conditions:
Optional<User> findByEmail(String email); List<User> findByStatusOrderByCreatedDateDesc(String status);
Implement custom queries with@Query:
@Query("SELECT u FROM User u WHERE u.status = :status") List<User> findActiveUsers(@Param("status") String status);
Define entities with proper annotations:
@Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;
@Column(nullable = false, length = 100) private String email; }
Configure relationships using appropriate cascade types:
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) private List<Order> orders = new ArrayList<>();
Set up database auditing:
@CreatedDate @Column(nullable = false, updatable = false) private LocalDateTime createdDate;
@Query for complex queries@Modifying for update/delete operations@Transactional(readOnly = true)@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
// Derived query
List<Product> findByCategory(String category);
// Custom query
@Query("SELECT p FROM Product p WHERE p.price > :minPrice")
List<Product> findExpensiveProducts(@Param("minPrice") BigDecimal minPrice);
}
@Service
public class ProductService {
private final ProductRepository repository;
public Page<Product> getProducts(int page, int size) {
Pageable pageable = PageRequest.of(page, size, Sort.by("name").ascending());
return repository.findAll(pageable);
}
}
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@CreatedDate
@Column(nullable = false, updatable = false)
private LocalDateTime createdDate;
@LastModifiedDate
private LocalDateTime lastModifiedDate;
@CreatedBy
@Column(nullable = false, updatable = false)
private String createdBy;
}
final modifiers@Value for DTOs@Id and @GeneratedValue annotations@Table and @Column annotations@Query for complex queries to avoid long method names@Param for query parametersOptional<T> for single results@Transactional on modifying operations@EntityGraph to avoid N+1 query problems@Transactional(readOnly = true)For comprehensive examples, detailed patterns, and advanced configurations, see:
@EntityGraph or JOIN FETCH in queries.CascadeType.REMOVE on large collections as it can cause performance issues.EAGER fetch type for collections; it can cause excessive database queries.@Transactional(readOnly = true) for read operations to enable optimizations.Weekly Installs
414
Repository
GitHub Stars
173
First Seen
Feb 3, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
gemini-cli324
opencode323
codex317
cursor306
github-copilot302
claude-code300
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
105,000 周安装
Azure 可观测性服务指南:Monitor、App Insights、Log Analytics 监控与 KQL 查询
102,200 周安装
Azure 验证工具 - Microsoft GitHub Copilot for Azure 部署前检查指南
103,000 周安装
Azure 资源查找工具:跨订阅查询、发现孤立资源、审计标签配置
103,000 周安装
Azure RBAC 权限管理工具 - Microsoft GitHub Copilot for Azure 最小角色分配指南
103,000 周安装
Azure Data Explorer (Kusto) 查询分析指南:KQL 语法、性能优化与大数据分析
103,000 周安装
Microsoft Foundry 技能指南:部署、调用、监控智能体全流程详解
103,100 周安装