【问题标题】:Sort order in search functionality搜索功能中的排序顺序
【发布时间】:2019-05-16 15:50:23
【问题描述】:

我使用此代码通过 JPA 和 Spring 获取 DB 行:

return transactionService.findAll(page, size)


        public Page<PaymentTransactions> findAll(int page, int size) {
        return dao.findAll(PageRequest.of(page,size, new Sort(Sort.Direction.DESC, "createdAt")));
        }

我想为此代码实现相同的排序:

return transactionService.getAllBySpecification(specification, pageable)

      @Override
      public Page<PaymentTransactions> getAllBySpecification(final Specification<PaymentTransactions> specification, final Pageable pageable) {
          return dao.findAll(specification, pageable);
      }

你知道我如何使用规范按列实现排序方向吗?像这样的:

return dao.findAll(specification, PageRequest.of(page,size, new Sort(Sort.Direction.DESC, "createdAt")));

其他问题:

我可以设置带有排序方向的可分页对象吗?像这样的:

      @Override
      public Page<PaymentTransactions> getAllBySpecification(final Specification<PaymentTransactions> specification, final Pageable pageable) {
          return dao.findAll(specification, PageRequest.of(pageable, new Sort(Sort.Direction.DESC, "createdAt")));
      }

【问题讨论】:

  • 究竟是什么问题? dao 的类是从 CrudRepository 派生的还是你的自定义类?
  • Dao 是一个存储库:public interface PaymentTransactionRepository extends JpaRepository&lt;PaymentTransactions, Integer&gt;, JpaSpecificationExecutor&lt;PaymentTransactions&gt; 我找不到如何实现具有规范、分页和排序方向的 findAll。
  • 我错过了什么吗?只要JpaSpecificationExecutor 已经提供了Page&lt;T&gt; findAll(Specification&lt;T&gt; spec, Pageable pageable); 方法,就不需要实现它。排序设置包含在您的 PageRequest 中
  • hm....你能给我看一些代码示例吗?
  • 就我而言,我想设置int page, int size

标签: java spring spring-boot design-patterns spring-data


【解决方案1】:

除了JpaSpecificationExecutor,您不需要其他任何东西。 Spring会使用这个接口来自动创建:

Page&lt;T&gt; findAll(@Nullable Specification&lt;T&gt; spec, Pageable pageable)

您似乎正在寻找的方法。所以,不清楚问题是什么,也许你导入了错误的类?或者,如果您只想使用pagesizegetAllBySpecification 生成PageRequest,您可以使用以下内容:

    public Page<Entity> getAllBySpecification(
            Specification<Entity> specification, 
            int page, int size) {

        return dao.findAll(specification, createMyPageRequest(page, size));
    }

    private PageRequest createMyPageRequest(int page, int size) {
        return PageRequest.of(page, size, new Sort(Sort.Direction.DESC, "createdAt"));
    }

无论如何,如果您需要这些 API 的完整编译示例...

你能给我一些代码示例吗?

...这里是:

import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public class SortFunctionality {
    private Dao dao;

    public Page<Entity> findAll(int page, int size) {
        return dao.findAll(createMyPageRequest(page, size));
    }

    public Page<Entity> getAllBySpecification(
            Specification<Entity> specification, 
            int page, int size) {

        return dao.findAll(specification, createMyPageRequest(page, size));
    }

    public Page<Entity> getAllBySpecification(
            Specification<Entity> specification, 
            Pageable pageable) {

        PageRequest pageRequest = createMyPageRequest(
                pageable.getPageNumber(), 
                pageable.getPageSize());

        return dao.findAll(specification, pageRequest);
    }   

    private PageRequest createMyPageRequest(int page, int size) {
        return PageRequest.of(page, size, new Sort(Sort.Direction.DESC, "createdAt"));
    }

    static interface Dao extends 
        JpaRepository<Entity, Integer>, JpaSpecificationExecutor<Entity> {}

    static class Entity {}
}

其他问题的编辑:

是的,您可以通过从Pageable 参数中提取pageNumberpageSize 并使用它们来创建您的自定义PageRequestPageRequest 带有硬编码的排序标准)和createMyPageRequest 实用程序来实现此目的我包含在演示代码中。最后,您可以像往常一样使用PageRequest 调用findAll 方法:

    public Page<Entity> getAllBySpecification(
            Specification<Entity> specification, 
            Pageable pageable) {

        PageRequest pageRequest = createMyPageRequest(
                pageable.getPageNumber(), 
                pageable.getPageSize());

        return dao.findAll(specification, pageRequest);
    }   

我更新了之前的完整演示以反映这一新增内容。它也进行了一点重构,所以如果你将这个新方法粘贴到复制之前版本的演示上,它会出现错误。

Complete code on GitHub

希望这会有所帮助。

【讨论】:

  • 谢谢。我还有一个问题。请查看更新后的帖子。
  • 我刚刚更新了答案以考虑附加问题。很高兴为您提供帮助。
【解决方案2】:

您可以只使用命名约定,让 Spring 为您生成实现。您必须使用 Spring 数据 JPA 存储库。您可以按照给出的示例并相应地重构您的逻辑。

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.special-parameters

它也是一个带参数Pageable。 请看例子:

List<Person> findByLastnameOrderByFirstnameDesc(String lastname);

还有这个:

Page<User> findByLastname(String lastname, Pageable pageable);

Pageable 和 Slice 在 4.4.4 特殊参数处理中描述。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    相关资源
    最近更新 更多