Spring Data JPA中CrudRepository和JpaRepository接口的区别是什么?

当我在网上看到这些例子时,我发现它们是可以互换使用的。

它们之间的区别是什么?

为什么要用其中一种而不是另一种呢?


当前回答

所有的答案都为问题提供了足够的细节。不过,让我再补充一点。

为什么我们要使用这些接口:

它们允许Spring找到存储库接口并为它们创建代理对象。 它为您提供了允许您执行一些常见操作的方法(您也可以定义您的自定义方法)。我喜欢这个特性,因为创建一个方法(定义查询和准备好的语句,然后用连接对象执行查询)来做一个简单的操作真的很糟糕!

哪个接口做什么:

CrudRepository:提供CRUD功能 PagingAndSortingRepository:提供对记录进行分页和排序的方法 JpaRepository:提供JPA相关的方法,例如刷新持久化上下文和批量删除记录

何时使用哪个接口:

根据http://jtuts.com/2014/08/26/difference-between-crudrepository-and-jparepository-in-spring-data-jpa/

一般来说,最好的想法是使用CrudRepository或PagingAndSortingRepository,这取决于你是否需要排序和分页。

如果可能的话,应该避免使用JpaRepository,因为它将您的存储库绑定到JPA持久性技术上,在大多数情况下,您可能甚至不会使用它提供的额外方法。

其他回答

spring-data-jpa 3.x的最新更新

从spring-data-jpa 3开始。X与弹簧靴一起使用。6.x和弹簧芯,

结构和层次结构已经修改,看起来更加清晰。

ListCrudRepository扩展了CrudRepository

ListPagingAndSortingRepository扩展了PagingAndSortingRepository

JpaRepository扩展了ListCrudRepository, listpaging和sortingrepository。

所以基本上新引入的ListPagingAndSortingRepository接口,ListCrudRepository现在代表了旧接口的功能,但返回类型为List<T>,而剩余的PagingAndSortingRepository, CrudRepository处理返回类型为Iterable<T>

在新的(3.x)版本中,结构如下:

在过去(3.0之前),许多返回List<T>的声明方法都直接在JpaRepository中声明,但现在这些方法有单独的接口,它们被提取到ListPagingAndSortingRepository, ListCrudRepository中。

(3.x)之前的结构是

我希望从上面的模式中可以清楚地看到,在3中提到的JpaRepository和CrudRepository是如何被修改的。x版本。

In case you plan to migrate spring-data-jpa from 2.x into 3.x (would be necessary if you migrate from spring-boot 2.x to spring-boot 3.x) as illustrated in the above schemas you should expect to have breaking code in cases where you have used the PagingAndSortingRepository in your code, as in the past it was extending from CrudRepository and so your custom repository which extended directly the PagingAndSortingRepository already had access to the methods of CrudRepository. If this is the issue you should fix this by adapting your custom repository to also extend either the ListCrudRepository or the CrudRepository.

JpaRepository扩展PagingAndSortingRepository,而PagingAndSortingRepository又扩展CrudRepository。

它们的主要功能是:

CrudRepository主要提供CRUD函数。 PagingAndSortingRepository提供了对记录进行分页和排序的方法。 JpaRepository提供了一些与jpa相关的方法,例如在批处理中刷新持久性上下文和删除记录。

由于上面提到的继承,JpaRepository将拥有CrudRepository和PagingAndSortingRepository的所有功能。因此,如果你不需要存储库拥有JpaRepository和PagingAndSortingRepository提供的功能,请使用CrudRepository。

我正在学习Spring Data JPA。它可能会帮助你:

简介:

PagingAndSortingRepository扩展了CrudRepository JpaRepository扩展PagingAndSortingRepository

CrudRepository接口为CRUD操作提供了方法,因此它允许您创建、读取、更新和删除记录,而无需定义自己的方法。

PagingAndSortingRepository提供了使用分页和排序检索实体的附加方法。

最后,JpaRepository添加了一些特定于JPA的更多功能。

所有的答案都为问题提供了足够的细节。不过,让我再补充一点。

为什么我们要使用这些接口:

它们允许Spring找到存储库接口并为它们创建代理对象。 它为您提供了允许您执行一些常见操作的方法(您也可以定义您的自定义方法)。我喜欢这个特性,因为创建一个方法(定义查询和准备好的语句,然后用连接对象执行查询)来做一个简单的操作真的很糟糕!

哪个接口做什么:

CrudRepository:提供CRUD功能 PagingAndSortingRepository:提供对记录进行分页和排序的方法 JpaRepository:提供JPA相关的方法,例如刷新持久化上下文和批量删除记录

何时使用哪个接口:

根据http://jtuts.com/2014/08/26/difference-between-crudrepository-and-jparepository-in-spring-data-jpa/

一般来说,最好的想法是使用CrudRepository或PagingAndSortingRepository,这取决于你是否需要排序和分页。

如果可能的话,应该避免使用JpaRepository,因为它将您的存储库绑定到JPA持久性技术上,在大多数情况下,您可能甚至不会使用它提供的额外方法。