欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Data JPA 多属性排序

程序员文章站 2022-03-02 15:39:01
...

参考https://segmentfault.com/a/1190000000412105
参考http://blog.csdn.net/qq_23660243/article/details/43371969

public class WatchPoint  {

    private Section section;// 区间
    private String rangeMileage;里程
}

public class Section{

    private Railway railway;//线路
}

public class Railway {

    private Railway railway;//线路
    private String name;//名称
}

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;

public void test(){

    //按线路降序和里程升序排序  
    Sort sort = new Sort(Sort.Direction.DESC, "section.railway.name").and(new Sort(Sort.Direction.ASC, "rangeMileage"));

    //按线路降序和里程升序排序  
    Order nameOrder = new Order(Direction.DESC, "section.railway.name");
    Order rangeOrder = new Order(Direction.ASC, "rangeMileage");
    List<Order> orders = new ArrayList<Order>();    
    orders.add(nameOrder);//先按线路降序
    orders.add(rangeOrder);//再按里程升序
    Sort sort2 = new Sort(orders);

    //按线路升序和里程升序排序              
    Sort sort = new Sort(Direction.fromString("asc"), "section.railway.name", "rangeMileage");
}
相关标签: spring jpa