簡體   English   中英

在 Spring Boot 中,CrudRepository 和 JpaRepository 在擴展 Java 存儲庫接口方面有什么區別

[英]In Spring Boot what is the difference between CrudRepository and JpaRepository in extending a Java repository interface

在 spring 中構建存儲庫時,在我的存儲庫界面中,我使用以下方法對其進行了擴展

extends CrudRepository<EntityName, EntityType> 其中 EntityName 是我的實體的名稱 Class 並且 EntityType 設置為默認類型 Long 請參閱下面的示例代碼

@Repository
public interface RoomRepository extends CrudRepository<Room, Long> {
}

但是我注意到了 JpaRepository 的使用,請參見下面的示例

public interface RoomRepository extends JpaRepository<Room, UUID>{

    public Boolean existsRoom(String roomNumber);
    
}

CrudRepository 與 JpaRepository

JpaRepository 擴展了 PagingAndSortingRepository,后者又擴展了 CrudRepository。

  1. CrudRepository 主要提供CRUD 功能。
  2. PagingAndSortingRepository 提供了對記錄進行分頁和排序的方法。
  3. JpaRepository 提供了一些 JPA 相關的方法,比如刷新持久化上下文,批量刪除記錄。

因為上面提到的inheritance,所以JpaRepository會具備CrudRepository和PagingAndSortingRepository的所有功能。

所以如果你不需要repository有JpaRepository和PagingAndSortingRepository提供的功能,就用CrudRepository。

參考: https://www.javatpoint.com/spring-boot-crud-operations#:~:text=CrudRepository%20does%20not%20provide%20any,works%20as%20a%20marker%20interface

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM