Spring Data Commons 提供了 Page 接口,用于表示和处理大型结果集的分页信息。Page 接口提供了一个抽象层,允许开发人员使用 PageRepository 创建和处理分页查询,而无需处理实际的分页实现细节,从而简化了应用程序开发。Page 接口还提供了对结果集的元数据信息的访问,例如总元素数、总页数和当前页号,使应用程序能够轻松地显示分页和导航信息。

Creating Repository Instances

本节介绍如何为定义的存储库接口创建实例和 bean 定义。

This section covers how to create instances and bean definitions for the defined repository interfaces.

Java Configuration

在 Java 配置类上使用特定于存储的 @Enable{store}Repositories 注释,以定义存储库激活的配置。有关基于 Java 配置 Spring 容器的介绍,请参阅 {spring-framework-docs}/core/beans/java.html[Spring 参考手册中的 JavaConfig]。

Use the store-specific @Enable{store}Repositories annotation on a Java configuration class to define a configuration for repository activation. For an introduction to Java-based configuration of the Spring container, see {spring-framework-docs}/core/beans/java.html[JavaConfig in the Spring reference documentation].

用于启用 Spring 数据存储库的示例配置类似于以下内容:

A sample configuration to enable Spring Data repositories resembles the following:

Sample annotation-based repository configuration
@Configuration
@EnableJpaRepositories("com.acme.repositories")
class ApplicationConfiguration {

  @Bean
  EntityManagerFactory entityManagerFactory() {
    // …
  }
}

前一个示例使用了 JPA 特定注释,您会根据实际使用的存储模块更改此注释。它同样适用于 EntityManagerFactory bean 的定义。请参阅涵盖存储特定配置的部分。

The preceding example uses the JPA-specific annotation, which you would change according to the store module you actually use. The same applies to the definition of the EntityManagerFactory bean. See the sections covering the store-specific configuration.

XML Configuration

每个 Spring 数据模块都包含一个 repositories 元素,它允许您定义一个基本包,以便 Spring 为您扫描,如以下示例所示:

Each Spring Data module includes a repositories element that lets you define a base package that Spring scans for you, as shown in the following example:

Enabling Spring Data repositories via XML
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/data/jpa"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa
    https://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

  <jpa:repositories base-package="com.acme.repositories" />

</beans:beans>

在前面的示例中,指示 Spring 扫描 com.acme.repositories 及其所有子包,以查找扩展 Repository 或其子接口之一的接口。对于找到的每个接口,基础设施会注册特定于持久性技术的 FactoryBean,以创建处理查询方法调用的适当代理。每个 bean 都注册在从接口名称派生的 bean 名称下,因此`UserRepository` 的接口将注册在 userRepository 下。嵌套存储库接口的 bean 名称前缀为其包含类型的名称。基本包属性允许使用通配符,这样您就可以定义已扫描包的模式。

In the preceding example, Spring is instructed to scan com.acme.repositories and all its sub-packages for interfaces extending Repository or one of its sub-interfaces. For each interface found, the infrastructure registers the persistence technology-specific FactoryBean to create the appropriate proxies that handle invocations of the query methods. Each bean is registered under a bean name that is derived from the interface name, so an interface of UserRepository would be registered under userRepository. Bean names for nested repository interfaces are prefixed with their enclosing type name. The base package attribute allows wildcards so that you can define a pattern of scanned packages.

Using Filters

默认情况下,基础设施会选择每个扩展持久性技术特定的位于配置的基本包下的 Repository 子接口的接口,并为其创建一个 bean 实例。但是,您可能希望更精细地控制为哪些接口创建 bean 实例。为此,请在存储库声明中使用 filter 元素。语义与 Spring 组件过滤器中的元素完全相同。有关详细信息,请参阅针对这些元素的 {spring-framework-docs}/core/beans/classpath-scanning.html[Spring 参考手册]。

By default, the infrastructure picks up every interface that extends the persistence technology-specific Repository sub-interface located under the configured base package and creates a bean instance for it. However, you might want more fine-grained control over which interfaces have bean instances created for them. To do so, use filter elements inside the repository declaration. The semantics are exactly equivalent to the elements in Spring’s component filters. For details, see the {spring-framework-docs}/core/beans/classpath-scanning.html[Spring reference documentation] for these elements.

例如,要排除某些接口作为存储库 bean 进行实例化,可以使用以下配置:

For example, to exclude certain interfaces from instantiation as repository beans, you could use the following configuration:

Using filters
  • Java

  • XML

@Configuration
@Enable{store}Repositories(basePackages = "com.acme.repositories",
    includeFilters = { @Filter(type = FilterType.REGEX, pattern = ".*SomeRepository") },
    excludeFilters = { @Filter(type = FilterType.REGEX, pattern = ".*SomeOtherRepository") })
class ApplicationConfiguration {

  @Bean
  EntityManagerFactory entityManagerFactory() {
    // …
  }
}
<repositories base-package="com.acme.repositories">
  <context:include-filter type="regex" expression=".*SomeRepository" />
  <context:exclude-filter type="regex" expression=".*SomeOtherRepository" />
</repositories>

前面的示例包括所有以 SomeRepository 结尾的接口,并排除以 SomeOtherRepository 结尾的接口进行实例化。

The preceding example includes all interfaces ending with SomeRepository and excludes those ending with SomeOtherRepository from being instantiated.

Standalone Usage

您还可以在 Spring 容器外部使用存储库基础设施,例如在 CDI 环境中。您仍然需要在类路径中包含一些 Spring 库,但一般来说,您也可以以编程方式设置存储库。提供存储库支持的 Spring 数据模块附带特定于持久性技术的 RepositoryFactory,您可以使用此工厂,如下所示:

You can also use the repository infrastructure outside of a Spring container — for example, in CDI environments.You still need some Spring libraries in your classpath, but, generally, you can set up repositories programmatically as well.The Spring Data modules that provide repository support ship with a persistence technology-specific RepositoryFactory that you can use, as follows:

Standalone usage of the repository factory
RepositoryFactorySupport factory = … // Instantiate factory here
UserRepository repository = factory.getRepository(UserRepository.class);