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

guykilcj  于 2022-11-10  发布在  Spring
关注(0)|答案(2)|浏览(236)

我正在使用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);

}

我要应用排序。我要基于自定义分级算法对Thing列表进行排序。

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

    // custom logic

    return things;
};

如果可能的话,我想使用一个自定义函数来对结果进行排序。在Spring JPA中,处理这种事情的最佳方法是什么?我如何让我的仓库使用我的函数来对结果集进行排序?

dsekswqp

dsekswqp1#

您可以自定义对象页面(可分页)。如果您获取请求的数据:

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:

order by oldParam ASC/DESC;

之后的SQL:

order by newParam ASC/DESC;
4dc9hkyq

4dc9hkyq2#

Spring Data提供基本的排序,如here所示,您也可以使用@Queryorder by来注解查询,但我相信您需要的是更复杂的排序逻辑。
如果你正在使用Java 8,你可以使用它的特性来排序你的列表,this文章给出了很好的例子,它需要更多的工作,但似乎是你所追求的。注意,为了从stream转换到list,你必须在stream的末尾包括.collect(Collectors.toList());
示例(摘自文章):

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

Edit:我没有注意到您使用了分页。我也遇到过类似的问题,在返回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());
    }
}

这里最重要的是setContent方法,它不存在于基类中。你可以将它与排序方法一起使用:

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

相关问题