Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 搜索功能中的排序顺序_Java_Spring_Spring Boot_Design Patterns_Spring Data - Fatal编程技术网

Java 搜索功能中的排序顺序

Java 搜索功能中的排序顺序,java,spring,spring-boot,design-patterns,spring-data,Java,Spring,Spring Boot,Design Patterns,Spring Data,我使用以下代码使用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")));

我使用以下代码使用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 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")));
      }
    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"));
    }
@覆盖
公共页面getAllBySpecification(最终规范规范、最终可分页规范){
返回dao.findAll(规范,PageRequest.of(可分页,新排序(Sort.Direction.DESC,“createdAt”));
}

除了这些,你不需要其他任何东西。Spring将使用此界面自动创建:

您似乎正在寻找的方法。所以,不清楚问题是什么,可能是导入了错误的类?或者,如果您只想使用
page
size
getAllBySpecification
生成
PageRequest
,您可以有如下功能:

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")));
      }
    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"));
    }
我更新了之前的完整演示,以反映这一新添加。此外,它还有一点重构,所以如果您将这个新方法粘贴到演示的前一个版本的副本上,它将出现错误


希望这有帮助。

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

它也是一个可分页的take参数。 请看以下例子:

List<Person> findByLastnameOrderByFirstnameDesc(String lastname);
列出findByLastnameOrderByFirstnameDesc(字符串lastname);
还有这个:

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

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

确切的问题是什么?
dao
的类是从Crudepository派生的还是您的自定义类?dao是一个存储库:
公共接口PaymentTransactionRepository扩展了JpaRepository,JpaSpecificationExecutor
我找不到如何使用规范、分页和排序方向来实现findAll。我缺少什么吗?只要
JpaSpecificationExecutor
已经提供了
Page findAll(规范规范规范,可分页)方法。排序设置打包在您的PageRequesthm…中。。。。你能给我看一些代码示例吗?在我的例子中,我想设置
int页面,int大小
谢谢。我还有一个问题。请看更新后的帖子。我刚刚更新了答案,考虑了另外一个问题。很乐意帮忙。