简体   繁体   English

搜索功能中的排序顺序

[英]Sort order in search functionality

I use this code to get DB rows using JPA and Spring:我使用此代码使用 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")));
        }

I would like to implement the same sorting for this code:我想为此代码实现相同的排序:

return transactionService.getAllBySpecification(specification, pageable)

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

Do you know how I can implement sorting direction by column using specification.你知道我如何使用规范按列实现排序方向吗? Something like this:像这样的东西:

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

Additionnal question:补充问题:

Can I set Pagable object with sort direction?我可以设置带有排序方向的 Pagable 对象吗? Something like this:像这样的东西:

      @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")));
      }

You don't need anything else other than JpaSpecificationExecutor .除了JpaSpecificationExecutor之外,您不需要其他任何东西。 Spring will use this interface to automatically create the: Spring 将使用此接口自动创建:

Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable)

method you seem to be looking for.您似乎正在寻找的方法。 So, it is unclear what the problem is, maybe you are importing the wrong classes?所以,目前还不清楚问题是什么,也许你导入了错误的类? Or if you just want to generate PageRequest for the getAllBySpecification using page and size you can have something like this:或者,如果您只想使用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"));
    }

In any event, if you need a complete compiling example of these APIs...无论如何,如果您需要这些 API 的完整编译示例...

can you show me some code example please?你能告诉我一些代码示例吗?

... here it is: ... 这里是:

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 {}
}

Additional Question's Edit:附加问题的编辑:

Yes you can achieve this by extracting the pageNumber and the pageSize from the Pageable argument and using them to create your custom PageRequest ( PageRequest with hardcoded sorting criteria) with the createMyPageRequest utility that I included in the demo code.是的,您可以通过从Pageable参数中提取pageNumberpageSize并使用它们通过我包含在演示代码中的createMyPageRequest实用程序创建自定义PageRequest (带有硬编码排序标准的PageRequest )来实现这createMyPageRequest Finally, you can use that PageRequest to invoke the findAll method as usual:最后,您可以像往常一样使用该PageRequest调用findAll方法:

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

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

        return dao.findAll(specification, pageRequest);
    }   

I updated the previous full demo to reflect this new addition as well.我更新了之前的完整演示,以反映这一新增内容。 Also it is a little bit refactored, so if you paste this new method over a copy previous version of the demo it will have errors.它也有一点重构,所以如果你把这个新方法粘贴到演示的先前版本的副本上,它会出错。

Complete code on GitHub GitHub 上的完整代码

Hope this helps.希望这可以帮助。

You can use only naming convention and let Spring generate implementation for you.您只能使用命名约定并让 Spring 为您生成实现。 You have to use Spring data JPA repositories.您必须使用 Spring 数据 JPA 存储库。 You can follow the examples given and refactor your logic accordingly.您可以按照给出的示例并相应地重构您的逻辑。

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

It also an take parameter Pageable.它也是一个带参数 Pageable。 Please look at the examples:请看例子:

List<Person> findByLastnameOrderByFirstnameDesc(String lastname);

And this one:和这个:

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

Pageable and Slice are described in 4.4.4 Special parameter handling. Pageable 和 Slice 在 4.4.4 特殊参数处理中描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM