简体   繁体   English

Spring Data JPA - JpaRepository 中的自定义排序

[英]Spring Data JPA - Custom Sort in JpaRepository

I am using Spring Data JPA with Spring Data REST and i have created a JpaRepository for my Thing entity.我正在使用 Spring Data JPA 和 Spring Data REST,并且我为我的Thing实体创建了一个 JpaRepository。

@Repository
public interface ThingRepository extends JpaRepository<Thing, Long> {

    @RestResource(path = "findByName", rel = "findByName")
    Page findByName(@Param("name") String name, Pageable page);

}

I want to apply sorting.我想应用排序。 I want to sort the Thing List based on a custom rating algorithm.我想根据自定义评级算法对Thing列表进行排序。

List<Thing> sortByRating(List<Thing> things){

    // custom logic

    return things;
};

I would like if possible to use a custom function in order to sort the results.如果可能的话,我想使用自定义函数来对结果进行排序。 What is the best approach to this kind of thing in Spring JPA? Spring JPA 中这种事情的最佳方法是什么? How can i make my Repository use my function to sort the result set?如何让我的存储库使用我的函数对结果集进行排序?

Spring Data offers basic sorting as seen here . Spring Data 提供了基本的排序,如此处所示 You can also annotate your query with @Query and use order by , but I believe what you need is a more complex sorting logic.您还可以使用@Query注释您的查询并使用order by ,但我相信您需要的是更复杂的排序逻辑。

If youre using Java 8 you can use its features to sort your list, this article shows good examples of that, it takes a bit more work but seems like what you're after.如果您使用的是 Java 8,您可以使用它的功能对列表进行排序, 本文展示了很好的示例,这需要更多的工作,但看起来就像您所追求的那样。 Note that in order to convert from stream to list you have to include .collect(Collectors.toList());请注意,为了从stream转换为list ,您必须包含.collect(Collectors.toList()); at the end of the stream .stream的末尾。

Example(taken from the article):示例(取自文章):

employees.stream().sorted((Employee e1, Employee e2) -> e1.getHireDate()
                        .compareTo(e2.getHireDate())).collect(Collectors.toList());

Edit : I didn't notice you were using pagination.编辑:我没有注意到您正在使用分页。 I had a similar problem where I had to do something with the page contents before returning the Page object, so I ended up creating an extension of the PageImpl<T> class:我有一个类似的问题,我必须在返回Page对象之前对页面内容做一些事情,所以我最终创建了PageImpl<T>类的扩展:

public class PageImplBean<T> extends PageImpl<T> {

    private static final long serialVersionUID = 1L;
    private int number;
    private int size;
    private int totalPages;
    private int numberOfElements;
    private long totalElements;
    private boolean previousPage;
    private boolean first;
    private boolean nextPage;
    private boolean last;
    private List<T> content;
    @JsonIgnore
    private Sort sort;

    public PageImplBean() {
        super(new ArrayList<T>());
    }

    public PageImplBean(Page pagina){
        super(new ArrayList<T>());
        this.number = pagina.getNumber();
        this.size = pagina.getSize();
        this.totalPages = pagina.getTotalPages();
        this.numberOfElements = pagina.getNumberOfElements();
        this.totalElements = pagina.getTotalElements();
        this.previousPage = pagina.hasPrevious();
        this.first = pagina.isFirst();
        this.nextPage = pagina.hasNext();
        this.last = pagina.isLast();
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public int getNumberOfElements() {
        return numberOfElements;
    }

    public void setNumberOfElements(int numberOfElements) {
        this.numberOfElements = numberOfElements;
    }

    public long getTotalElements() {
        return totalElements;
    }

    public void setTotalElements(long totalElements) {
        this.totalElements = totalElements;
    }

    public boolean isPreviousPage() {
        return previousPage;
    }

    public void setPreviousPage(boolean previousPage) {
        this.previousPage = previousPage;
    }

    public boolean isFirst() {
        return first;
    }

    public void setFirst(boolean first) {
        this.first = first;
    }

    public boolean isNextPage() {
        return nextPage;
    }

    public void setNextPage(boolean nextPage) {
        this.nextPage = nextPage;
    }

    public boolean isLast() {
        return last;
    }

    public void setLast(boolean last) {
        this.last = last;
    }

    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }

    public Sort getSort() {
        return sort;
    }

    public void setSort(Sort sort) {
        this.sort = sort;
    }

    public PageImpl<T> pageImpl() {
        return new PageImpl<T>(getContent(), new PageRequest(getNumber(),
                getSize(), getSort()), getTotalElements());
    }
}

What's important here in your case is the setContent method which doesnt exist in the base class.在您的情况下,重要的是基类中不存在的setContent方法。 You can use that with your sorting method:您可以将其与您的排序方法一起使用:

PageImplBean<Thing> page = //call your rest repository
List<Thing> pageContent = page.getContent();
page.setContent(sortByRating(pageContent));

You can customize your object page(Pageable).您可以自定义您的对象页面(Pageable)。 If you get data of request:如果您获得请求数据:

public PageRequest customSort(Args...) {
    String[] newParams = {"newParam"};
    Sort sort = page.getSort();
    Sort.Order dataSort =
        sort.iterator().next();
    Sort newSort = Sort.by(dataSort.getDirection(), newParams);
    return PageRequest.of(page.getPageNumber(), this.getPageSize(), newSort);
}

SQL Before:之前的 SQL:

order by oldParam ASC/DESC;

SQL After: SQL之后:

order by newParam ASC/DESC;

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

相关问题 如何从 Spring 数据 JPA 在 JpaRepository 中创建自定义方法? - How to make a custom method in JpaRepository from Spring Data JPA? 关于Spring Data JPA Rest(JpaRepository)的安全性问题 - Security questions on Spring Data JPA Rest (JpaRepository) Spring Data JPA:没有JpaRepository的命名方法 - Spring Data JPA: Named method without JpaRepository 使用JpaRepository Spring-data-jpa在子列表上排序总数 - Sort On Child List total count using JpaRepository Spring-data-jpa 为什么我的 JpaRepository (spring-data-jpa) 自定义查询返回一个空列表? - Why does my JpaRepository (spring-data-jpa) custom query return me an empty list? Spring Data JpaRepository findAll(Iterable <ID> ids)+ findAll(排序排序) - Spring Data JpaRepository findAll(Iterable<ID> ids) + findAll(Sort sort) Spring Data JPA JpaRepository.save()不返回默认值 - Spring Data JPA JpaRepository.save() does not return default values 为什么 JpaRepository 在 Spring Data Jpa 中没有 saveAll 方法? - Why doesn't JpaRepository have a saveAll method in Spring Data Jpa? 在spring数据jpa中实现JpaRepository接口的存储库是否可以返回一个sortedset? - Can a repository that implements the JpaRepository interface in spring data jpa return a sortedset? 如何使用 Spring 数据 JPA (JPARepository) 实现延迟加载? - How to implement lazy loading using Spring data JPA (JPARepository)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM