我正在使用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中,这种事情的最佳方法是什么?如何让我的存储库使用我的函数对结果集进行排序?
答案 0 :(得分:0)
您可以自定义对象页面(可分页)。如果您获得请求数据:
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;
答案 1 :(得分:-1)
Spring Data提供基本排序,如here所示。您还可以使用@Query
为您的查询添加注释并使用order by
,但我相信您需要的是更复杂的排序逻辑。
如果您使用的是Java 8,则可以使用其功能对列表进行排序,this文章显示了很好的示例,它需要更多的工作,但看起来就像您所追求的那样。请注意,要从stream
转换为list
,您必须在.collect(Collectors.toList());
的末尾添加stream
。
示例(摘自文章):
employees.stream().sorted((Employee e1, Employee e2) -> e1.getHireDate()
.compareTo(e2.getHireDate())).collect(Collectors.toList());
编辑:我没有注意到您正在使用分页。我有一个类似的问题,我必须在返回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));