Spring Data JPA中的CrudRepository和JpaRepository接口有什么区别? [英] What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?
问题描述
Spring Data JPA中的 CrudRepository
和 JpaRepository
接口有什么区别?
What is the difference between CrudRepository
and JpaRepository
interfaces in Spring Data JPA?
当我在网上看到这些例子时,我看到它们在那里可以互换使用。它们之间有什么区别?你为什么要用另一个?
When I see the examples on the web, I see them there used kind of interchangeably. What is the difference between them? Why would you want to use one over the other?
推荐答案
JpaRepository 扩展 PagingAndSortingRepository 反过来扩展 CrudRepository 。
他们的主要功能是:
- CrudRepository 主要提供CRUD功能。
- PagingAndSortingRepository 提供了分页和排序记录的方法。
- JpaRepository 提供了一些与JPA相关的方法,例如刷新持久化上下文和删除批量记录。
- CrudRepository mainly provides CRUD functions.
- PagingAndSortingRepository provides methods to do pagination and sorting records.
- JpaRepository provides some JPA-related methods such as flushing the persistence context and deleting records in a batch.
由于上面提到的继承, JpaRepository
将具有 CrudRepository $的所有功能c $ c>和
PagingAndSortingRepository
。因此,如果您不需要存储库具有 JpaRepository
和 PagingAndSortingRepository
提供的功能,请使用 CrudRepository
。
Because of the inheritance mentioned above, JpaRepository
will have all the functions of CrudRepository
and PagingAndSortingRepository
. So if you don't need the repository to have the functions provided by JpaRepository
and PagingAndSortingRepository
, use CrudRepository
.
这篇关于Spring Data JPA中的CrudRepository和JpaRepository接口有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!