Spring Data Commons 中的 Page 接口为分页和排序提供了一个标准方法,允许通过针对特定存储库接口实现自定义实现来实现自定义分页操作。该方法提供了一种灵活且可扩展的方式来管理和检索数据,同时保持与框架的集成和一致性。它允许开发人员根据需要微调分页行为,例如通过提供自定义查询条件或排序规则,从而获得对分页操作的更大控制。

Custom Repository Implementations

Spring Data 提供了各种选项来创建几乎无需编码的查询方法。但是,当那些选项不符合您的需要时,您也可以为存储库方法提供您自己的自定义实现。本部分介绍如何执行此操作。

Customizing Individual Repositories

要使用自定义功能丰富存储库,您必须首先按以下方式定义片段接口和自定义功能的实现:

Interface for custom repository functionality
interface CustomizedUserRepository {
  void someCustomMethod(User user);
}
Implementation of custom repository functionality
class CustomizedUserRepositoryImpl implements CustomizedUserRepository {

  public void someCustomMethod(User user) {
    // Your custom implementation
  }
}

类名中与片段接口相对应最重要的部分是 Impl 后缀。

该实现本身不依赖于 Spring Data,并且可以是常规 Spring Bean。因此,可以使用标准依赖注入行为注入对其他 Bean(例如 JdbcTemplate )的引用,可以使用切面等等。

然后,您可以让您的存储库接口扩展片段接口,如下所示:

Changes to your repository interface
interface UserRepository extends CrudRepository<User, Long>, CustomizedUserRepository {

  // Declare query methods here
}

用您的存储库接口扩展片段接口将 CRUD 和自定义功能组合在一起,并使其可供客户端使用。

使用形成存储库组合的片段来实现 Spring Data 存储库。片段是基础存储库、功能方面(例如 QueryDsl)以及自定义接口及其实现。每次将一个界面添加到您的存储库界面时,都会通过添加一个片段来增强组合。基础存储库和存储库方面实现由每个 Spring Data 模块提供。

以下示例显示了自定义接口及其实现:

Fragments with their implementations
interface HumanRepository {
  void someHumanMethod(User user);
}

class HumanRepositoryImpl implements HumanRepository {

  public void someHumanMethod(User user) {
    // Your custom implementation
  }
}

interface ContactRepository {

  void someContactMethod(User user);

  User anotherContactMethod(User user);
}

class ContactRepositoryImpl implements ContactRepository {

  public void someContactMethod(User user) {
    // Your custom implementation
  }

  public User anotherContactMethod(User user) {
    // Your custom implementation
  }
}

以下示例显示了扩展 CrudRepository 的自定义存储库的接口:

Changes to your repository interface
interface UserRepository extends CrudRepository<User, Long>, HumanRepository, ContactRepository {

  // Declare query methods here
}

存储库可以由按声明顺序导入的多个自定义实现组成。自定义实现的优先级高于基础实现和存储库切面。这种排序方式可以覆盖基础存储库和切面方法,如果两个片段提供相同的函数签名,还可以解决模糊性。存储库片段不限于在单个存储库接口中使用。多个存储库可以使用片段接口,从而允许跨不同存储库重用自定义项。

以下示例展示了存储库片段及其实现:

Fragments overriding save(…)
interface CustomizedSave<T> {
  <S extends T> S save(S entity);
}

class CustomizedSaveImpl<T> implements CustomizedSave<T> {

  public <S extends T> S save(S entity) {
    // Your custom implementation
  }
}

以下示例展示了使用上述存储库片段的存储库:

Customized repository interfaces
interface UserRepository extends CrudRepository<User, Long>, CustomizedSave<User> {
}

interface PersonRepository extends CrudRepository<Person, Long>, CustomizedSave<Person> {
}

Configuration

存储库基础设施尝试通过扫描发现存储库的包下方的类,以自动检测自定义实现片段。这些类需要遵循命名规范,附加默认值为“Impl”的后缀。

以下示例展示了使用默认后缀的存储库和设置后缀的自定义值的存储库:

Example 1. Configuration example
Java
@Enable{store}Repositories(repositoryImplementationPostfix = "MyPostfix")
class Configuration { … }
XML
<repositories base-package="com.acme.repository" />

<repositories base-package="com.acme.repository" repository-impl-postfix="MyPostfix" />

上一示例中的第一个配置尝试查找称为“com.acme.repository.CustomizedUserRepositoryImpl”的类,以用作自定义存储库实现。第二个示例尝试查找“com.acme.repository.CustomizedUserRepositoryMyPostfix”。

Resolution of Ambiguity

如果在不同包中发现具有匹配类名的多个实现,Spring Data 会使用 Bean 名称来识别要使用哪一个。

鉴于前面显示的“CustomizedUserRepository”的两个自定义实现,使用了第一个实现。其 Bean 名称是“customizedUserRepositoryImpl”,它匹配片段接口(“CustomizedUserRepository”)加上后缀“Impl”。

Example 2. Resolution of ambiguous implementations
package com.acme.impl.one;

class CustomizedUserRepositoryImpl implements CustomizedUserRepository {

  // Your custom implementation
}
package com.acme.impl.two;

@Component("specialCustomImpl")
class CustomizedUserRepositoryImpl implements CustomizedUserRepository {

  // Your custom implementation
}

如果你使用“@Component("specialCustom")”对“UserRepository”接口进行注释,则 Bean 名称加上“Impl”匹配在“com.acme.impl.two”中为存储库实现定义的名称,并且它将替代第一个名称。

Manual Wiring

如果您的自定义实现仅使用基于注释的配置和自动装配,则前面所示的方法将很好地工作,因为它被视为任何其他 Spring bean。如果您的实现片段 bean 需要特殊连接,您可以声明该 bean 并根据 preceding section中描述的约定对其命名。然后,基础架构将按名称引用手动定义的 bean 定义,而不是自己创建 bean 定义。以下示例展示如何手动连接自定义实现:

Example 3. Manual wiring of custom implementations
Java
class MyClass {
  MyClass(@Qualifier("userRepositoryImpl") UserRepository userRepository) {
    …
  }
}
XML
<repositories base-package="com.acme.repository" />

<beans:bean id="userRepositoryImpl" class="…">
  <!-- further configuration -->
</beans:bean>

Customize the Base Repository

preceding section 中所述的方法需要在您想要自定义基本存储库行为时自定义每个存储库接口,以影响所有存储库。而要更改所有存储库的行为,您可以创建实现,以扩展特定的持久性技术存储库基础类。然后,此类充当存储库代理的自定义基础类,如以下示例所示:

Custom repository base class
class MyRepositoryImpl<T, ID>
  extends SimpleJpaRepository<T, ID> {

  private final EntityManager entityManager;

  MyRepositoryImpl(JpaEntityInformation entityInformation,
                          EntityManager entityManager) {
    super(entityInformation, entityManager);

    // Keep the EntityManager around to used from the newly introduced methods.
    this.entityManager = entityManager;
  }

  @Transactional
  public <S extends T> S save(S entity) {
    // implementation goes here
  }
}

该类需要具有存储特定仓库工厂实现使用的超类的构造函数。如果仓库基本类有多个构造函数,请重写采用 EntityInformation 加上存储特定基础设施对象(例如 EntityManager 或模板类)的那个构造函数。

最后一步是让 Spring Data 基础设施注意自定义存储库基本类。在配置中,你可以使用“repositoryBaseClass”来实现,如以下示例所示:

Example 4. Configuring a custom repository base class
Java
@Configuration
@Enable{store}Repositories(repositoryBaseClass = MyRepositoryImpl.class)
class ApplicationConfiguration { … }
XML
<repositories base-package="com.acme.repository"
     base-class="….MyRepositoryImpl" />