定义仓库接口

要定义仓库接口,首先需要定义一个特定于领域类的仓库接口。该接口必须扩展 Repository 并指定领域类和 ID 类型。如果你想为该领域类型暴露 CRUD 方法,可以扩展 CrudRepository 或其变体之一,而不是 Repository

微调仓库定义

你可以通过几种不同的方式开始使用你的仓库接口。

典型方法是扩展 CrudRepository,它提供了 CRUD 功能的方法。CRUD 代表创建(Create)、读取(Read)、更新(Update)、删除(Delete)。在 3.0 版本中,我们还引入了 ListCrudRepository,它与 CrudRepository 非常相似,但对于返回多个实体的方法,它返回 List 而不是 Iterable,你可能会发现 List 更易于使用。

如果你正在使用响应式存储,根据你使用的响应式框架,可以选择 ReactiveCrudRepositoryRxJava3CrudRepository

如果你正在使用 Kotlin,可以选择利用 Kotlin 协程的 CoroutineCrudRepository

此外,如果你需要允许指定 Sort 抽象或(在第一种情况下)Pageable 抽象的方法,可以扩展 PagingAndSortingRepositoryReactiveSortingRepositoryRxJava3SortingRepositoryCoroutineSortingRepository。请注意,各种排序仓库在 Spring Data 3.0 版本之前扩展了各自的 CRUD 仓库,但现在不再如此。因此,如果你想要同时拥有这两种功能,需要同时扩展这两个接口。

如果你不想扩展 Spring Data 接口,也可以使用 `@RepositoryDefinition` 注解你的仓库接口。扩展其中一个 CRUD 仓库接口会暴露一套完整的方法来操作你的实体。如果你更倾向于有选择地暴露方法,可以将你想要暴露的方法从 CRUD 仓库复制到你的领域仓库中。这样做时,你可以更改方法的返回类型。如果可能,Spring Data 会遵循返回类型。例如,对于返回多个实体的方法,你可以选择 Iterable<T>List<T>Collection<T> 或 VAVR list。

如果你的应用程序中的许多仓库应该拥有相同的方法集,你可以定义自己的基础接口供其继承。这样的接口必须使用 `@NoRepositoryBean` 注解。这可以防止 Spring Data 直接尝试创建该接口的实例并失败,因为它无法确定该仓库对应的实体,因为该接口仍然包含一个泛型类型变量。

以下示例展示了如何有选择地暴露 CRUD 方法(在本例中为 findByIdsave

有选择地暴露 CRUD 方法
@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 模块

  1. 如果仓库定义扩展了特定于模块的仓库接口,则它是该特定 Spring Data 模块的有效候选者。

  2. 如果领域类使用特定于模块的类型注解进行标记,则它是该特定 Spring Data 模块的有效候选者。Spring Data 模块接受第三方注解(例如 JPA 的 @Entity)或提供自己的注解(例如 Spring Data MongoDB 和 Spring Data Elasticsearch 的 @Document)。

以下示例展示了使用特定于模块的接口(本例中为 JPA)的仓库

示例 1. 使用特定于模块的接口的仓库定义
interface MyRepository extends JpaRepository<User, Long> { }

@NoRepositoryBean
interface MyBaseRepository<T, ID> extends JpaRepository<T, ID> { … }

interface UserRepository extends MyBaseRepository<User, Long> { … }

MyRepositoryUserRepository 在其类型层级中扩展了 JpaRepository。它们是 Spring Data JPA 模块的有效候选者。

以下示例展示了使用通用接口的仓库

示例 2. 使用通用接口的仓库定义
interface AmbiguousRepository extends Repository<User, Long> { … }

@NoRepositoryBean
interface MyBaseRepository<T, ID> extends CrudRepository<T, ID> { … }

interface AmbiguousUserRepository extends MyBaseRepository<User, Long> { … }

AmbiguousRepositoryAmbiguousUserRepository 在其类型层级中只扩展了 RepositoryCrudRepository。虽然在使用唯一的 Spring Data 模块时这没有问题,但多个模块无法区分这些仓库应该绑定到哪个特定的 Spring Data 模块。

以下示例展示了使用带有注解的领域类的仓库

示例 3. 使用带有注解的领域类的仓库定义
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 注解进行标记。

以下不好的示例展示了使用带有混合注解的领域类的仓库

示例 4. 使用带有混合注解的领域类的仓库定义
interface JpaPersonRepository extends Repository<Person, Long> { … }

interface MongoDBPersonRepository extends Repository<Person, Long> { … }

@Entity
@Document
class Person { … }

此示例展示了一个领域类同时使用了 JPA 和 Spring Data MongoDB 注解。它定义了两个仓库,JpaPersonRepositoryMongoDBPersonRepository。一个用于 JPA,另一个用于 MongoDB。Spring Data 无法再区分这些仓库,这会导致未定义的行为。

仓库类型详情区分领域类注解用于严格仓库配置,以确定特定 Spring Data 模块的仓库候选者。在同一个领域类型上使用多个特定于持久化技术的注解是可能的,并且可以在多种持久化技术中重用领域类型。然而,在这种情况下,Spring Data 将无法确定一个唯一的模块来绑定仓库。

区分仓库的最后一种方式是通过界定仓库基础包(base package)的范围。基础包定义了扫描仓库接口定义的起始点,这意味着仓库定义应位于相应的包中。默认情况下,注解驱动的配置使用配置类所在的包。基于 XML 的配置中的基础包是必需的。

以下示例展示了基础包的注解驱动配置

基础包的注解驱动配置
@EnableJpaRepositories(basePackages = "com.acme.repositories.jpa")
@EnableMongoRepositories(basePackages = "com.acme.repositories.mongo")
class Configuration { … }