定义 Repository 接口
要定义存储库接口,首先需要定义一个特定于领域类的存储库接口。该接口必须扩展 Repository,并指定领域类和 ID 类型。如果您想为该领域类型公开 CRUD 方法,您可以扩展 CrudRepository 或其变体之一,而不是 Repository。
微调存储库定义
有几种方法可以开始使用您的存储库接口。
典型的方法是扩展 CrudRepository,它提供了 CRUD 功能的方法。CRUD 代表创建(Create)、读取(Read)、更新(Update)、删除(Delete)。在 3.0 版本中,我们还引入了 ListCrudRepository,它与 CrudRepository 非常相似,但对于返回多个实体的方法,它返回一个 List 而不是 Iterable,这可能更易于使用。
如果您正在使用响应式存储,您可以选择 ReactiveCrudRepository 或 RxJava3CrudRepository,具体取决于您使用的响应式框架。
如果您正在使用 Kotlin,您可以选择利用 Kotlin 协程的 CoroutineCrudRepository。
此外,如果您需要允许指定 Sort 抽象(或在第一个情况下为 Pageable 抽象)的方法,您可以扩展 PagingAndSortingRepository、ReactiveSortingRepository、RxJava3SortingRepository 或 CoroutineSortingRepository。请注意,各种排序存储库不再像 Spring Data 3.0 之前的版本那样扩展其各自的 CRUD 存储库。因此,如果您需要两者的功能,则需要同时扩展这两个接口。
如果您不想扩展 Spring Data 接口,也可以使用 @RepositoryDefinition 注解您的存储库接口。扩展其中一个 CRUD 存储库接口会公开一组完整的方法来操作您的实体。如果您希望选择性地公开方法,请将您想要公开的方法从 CRUD 存储库复制到您的领域存储库中。这样做时,您可以更改方法的返回类型。Spring Data 将尽可能尊重返回类型。例如,对于返回多个实体的方法,您可以选择 Iterable<T>、List<T>、Collection<T> 或 VAVR 列表。
如果您的应用程序中的许多存储库应该具有相同的方法集,您可以定义自己的基接口以供继承。此类接口必须使用 @NoRepositoryBean 进行注解。这可以防止 Spring Data 尝试直接创建其实例并因无法确定该存储库的实体(因为它仍包含一个泛型类型变量)而失败。
以下示例展示了如何选择性地公开 CRUD 方法(在本例中为 findById 和 save)
@NoRepositoryBean
interface MyBaseRepository<T, ID> extends Repository<T, ID> {
Optional<T> findById(ID id);
<S extends T> S save(S entity);
}
interface UserRepository extends MyBaseRepository<User, Long> {
User findByEmailAddress(EmailAddress emailAddress);
}
在前面的示例中,您为所有领域存储库定义了一个通用基接口,并公开了 findById(…) 和 save(…)。这些方法被路由到 Spring Data 提供的您选择的存储的基础存储库实现中(例如,如果您使用 JPA,实现是 SimpleJpaRepository),因为它们与 CrudRepository 中的方法签名匹配。因此,UserRepository 现在可以保存用户,通过 ID 查找单个用户,并触发查询以通过电子邮件地址查找 Users。
中间存储库接口使用 @NoRepositoryBean 注解。请确保将该注解添加到所有 Spring Data 不应在运行时创建实例的存储库接口中。 |
将存储库与多个 Spring Data 模块一起使用
在您的应用程序中使用唯一的 Spring Data 模块可以使事情变得简单,因为定义范围内的所有存储库接口都绑定到 Spring Data 模块。有时,应用程序需要使用多个 Spring Data 模块。在这种情况下,存储库定义必须区分持久化技术。当它在类路径上检测到多个存储库工厂时,Spring Data 会进入严格的存储库配置模式。严格配置使用存储库或领域类上的详细信息来决定存储库定义的 Spring Data 模块绑定
-
如果存储库定义扩展了特定模块的存储库,则它是特定 Spring Data 模块的有效候选者。
-
如果领域类使用特定模块的类型注解进行注解,则它是特定 Spring Data 模块的有效候选者。Spring Data 模块接受第三方注解(例如 JPA 的
@Entity)或提供自己的注解(例如 Spring Data MongoDB 和 Spring Data Elasticsearch 的@Document)。
以下示例展示了使用特定模块接口的存储库(在本例中为 JPA)
interface MyRepository extends JpaRepository<User, Long> { }
@NoRepositoryBean
interface MyBaseRepository<T, ID> extends JpaRepository<T, ID> { … }
interface UserRepository extends MyBaseRepository<User, Long> { … }
MyRepository 和 UserRepository 在其类型层次结构中扩展了 JpaRepository。它们是 Spring Data JPA 模块的有效候选者。
以下示例展示了使用泛型接口的存储库
interface AmbiguousRepository extends Repository<User, Long> { … }
@NoRepositoryBean
interface MyBaseRepository<T, ID> extends CrudRepository<T, ID> { … }
interface AmbiguousUserRepository extends MyBaseRepository<User, Long> { … }
AmbiguousRepository 和 AmbiguousUserRepository 在其类型层次结构中仅扩展了 Repository 和 CrudRepository。虽然在使用唯一的 Spring Data 模块时这没有问题,但多个模块无法区分这些存储库应该绑定到哪个特定的 Spring Data 模块。
以下示例展示了使用带注解的领域类的存储库
interface PersonRepository extends Repository<Person, Long> { … }
@Entity
class Person { … }
interface UserRepository extends Repository<User, Long> { … }
@Document
class User { … }
PersonRepository 引用 Person,它用 JPA @Entity 注解进行注解,因此该存储库明确属于 Spring Data JPA。UserRepository 引用 User,它用 Spring Data MongoDB 的 @Document 注解进行注解。
以下不良示例展示了使用带混合注解的领域类的存储库
interface JpaPersonRepository extends Repository<Person, Long> { … }
interface MongoDBPersonRepository extends Repository<Person, Long> { … }
@Entity
@Document
class Person { … }
此示例展示了一个使用 JPA 和 Spring Data MongoDB 注解的领域类。它定义了两个存储库,JpaPersonRepository 和 MongoDBPersonRepository。一个用于 JPA,另一个用于 MongoDB。Spring Data 无法再区分这些存储库,这会导致未定义的行为。
存储库类型详细信息 和 区分领域类注解 用于严格的存储库配置,以识别特定 Spring Data 模块的存储库候选者。在同一个领域类型上使用多个持久化技术特定注解是可能的,并且可以在多种持久化技术中重用领域类型。但是,Spring Data 无法再确定要绑定存储库的唯一模块。
区分存储库的最后一种方法是通过限定存储库基本包。基本包定义了扫描存储库接口定义的起始点,这意味着存储库定义位于适当的包中。默认情况下,注解驱动配置使用配置类的包。基于 XML 配置中的基本包 是强制性的。
以下示例展示了基本包的注解驱动配置
@EnableJpaRepositories(basePackages = "com.acme.repositories.jpa")
@EnableMongoRepositories(basePackages = "com.acme.repositories.mongo")
class Configuration { … }