Spring Data JPA使用Sort进行排序(Using Sort) | 您所在的位置:网站首页 › jpa使用方法 › Spring Data JPA使用Sort进行排序(Using Sort) |
结合@Query注解,我们可以使用Sort来对结果进行排序。 1、在CustomerRepository内添加方法 ? 1 2 3 4 5 6 7 8 9 10 /** * 一个参数,匹配两个字段 * @param name2 * @param sort 指定排序的参数,可以根据需要进行调整 * @return * 这里Param的值和=:后面的参数匹配,但不需要和方法名对应的参数值对应 * */ @Query("select c from Customer c where c.firstName=:name or c.lastName=:name") List findByName4(@Param("name") String name2,Sort sort); 方法一如既往,是声明式的,只是在原有方法的基础上,加上Sort(org.springframework.data.domain.Sort)作为参数即可。 2、在CustomerController中测试 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 /** * @Query注解方式查询, * 用@Param指定参数,匹配firstName和lastName */ @RequestMapping("/findByName") public void findByName4(){ //按照ID倒序排列 System.out.println("直接创建sort对象,通过排序方法和属性名"); Sort sort = new Sort(Sort.Direction.DESC,"id"); List result = repository.findByName4("Bauer",sort); for (Customer customer:result){ System.out.println(customer.toString()); } System.out.println("-------------------------------------------"); //按照ID倒序排列 System.out.println("通过Sort.Order对象创建sort对象"); Sort sortx = new Sort(new Sort.Order(Sort.Direction.DESC,"id")); List resultx = repository.findByName4("Bauer",sort); for (Customer customer:result){ System.out.println(customer.toString()); } System.out.println("-------------------------------------------");
System.out.println("通过排序方法和属性List创建sort对象"); List sortProperties = new ArrayList(); sortProperties.add("id"); sortProperties.add("firstName"); Sort sort2 = new Sort(Sort.Direction.DESC,sortProperties); List result2 = repository.findByName4("Bauer",sort2); for (Customer customer:result2){ System.out.println(customer.toString()); } System.out.println("-------------------------------------------");
System.out.println("通过创建Sort.Order对象的集合创建sort对象"); List orders = new ArrayList(); orders.add(new Sort.Order(Sort.Direction.DESC,"id")); orders.add(new Sort.Order(Sort.Direction.ASC,"firstName")); List result3 = repository.findByName4("Bauer",new Sort(orders)); for (Customer customer:result3){ System.out.println(customer.toString()); } System.out.println("-------------------------------------------"); } 这里总共列举了四种排序方式: 1)直接创建Sort对象,适合对单一属性做排序 2)通过Sort.Order对象创建Sort对象,适合对单一属性做排序 3)通过属性的List集合创建Sort对象,适合对多个属性,采取同一种排序方式的排序 4)通过Sort.Order对象的List集合创建Sort对象,适合所有情况,比较容易设置排序方式 对应着我们的使用场景来进行选择创建Sort对象的方式。 注意,这里并没有列举所有的Sort使用方式,还有忽略大小写,使用JpaSort.unsafe、聚合函数等进行排序,查询的属性值是Entity的属性名,不是数据库的字段,要注意到!! 更多用法,请参考源码:http://www.icnws.com/wp-content/uploads/2017/06/Sort.java_.txt 参考: 官方文档,http://docs.spring.io/spring-data/jpa/docs/current/reference/html/ DEMO,https://github.com/icnws/spring-data-jpa-demo
先说下Sort类常用的几个构造方法 1. public Sort(Order... orders) { this(Arrays.asList(orders)); } 2. public Sort(List orders) { Assert.notNull(orders, "Orders must not be null!"); this.orders = Collections.unmodifiableList(orders); } 3. public Sort(String... properties) { this(DEFAULT_DIRECTION, properties); } 4. public Sort(Direction direction, String... properties) { this(direction, properties == null ? new ArrayList() : Arrays.asList(properties)); } 5. public Sort(Direction direction, List properties) { if (properties == null || properties.isEmpty()) { throw new IllegalArgumentException("You have to provide at least one property to sort by!"); } this.orders = new ArrayList(properties.size()); for (String property : properties) { this.orders.add(new Order(direction, property)); } } 注:Direction是用来标识列属性升序还是降序排序的 properties即为列属性 在上面5个方法中,4,5方法只能实现一种排序方向。 3方法输入列名,按照默认的排序方式(ASC) 在介绍下Order的构造方法: public Order (Direction direction,String properties); --------Order维护一个Direction和一个列属性 所以,采用的方法; @RequestMapping("/sort") public List sort(){ List orders=new ArrayList(); orders.add(new Sort.Order(Sort.Direction.DESC,"age")); ---age降序 orders.add(new Sort.Order(Sort.Direction.ASC,"name")); ---naem升序 return personRepository.findAll(Sort.by(orders)); } |
今日新闻 |
推荐新闻 |
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 |