ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**1. Sort类与Pageable接口** Spring Data Jpa 可以在方法参数中直接传入`Pageable`或`Sort`来完成动态分页或排序,通常`Pageable`或`Sort`会是方法的最后一个参数。 ```java List<Student> findBySex(String sex, Sort sort); Page<Student> findBySex(String sex, Pageable pageable); /** * HSQL语句分页查询 */ @Query(value = "select s from student s where s.age >= ?1") Page<Student> findByAge(Integer age, Pageable pageable); /** * 原生SQL语句分页查询,必须要结合 countQuery 查询出总数 */ @Query( value = "select * from student where total >= ?1" , countQuery = "select count(id) from student where total >= ?1" , nativeQuery = true ) Page<Student> findByTotal(Integer total, Pageable pageable); ``` <br/> **2. 只排序,不分页** 可以调用下面重载的4个方法来完成排序规则。 ```java package org.springframework.data.domain; public class Sort implements Streamable<Sort.Order>, Serializable { //默认并且只能升序排序 public static Sort by(String... properties) //同一条语句既可以升序排序,也可以降序排序 public static Sort by(List<Sort.Order> orders) public static Sort by(Sort.Order... orders) //同一条语句只能按照升序,或降序排序 public static Sort by(Sort.Direction direction, String... properties) } ``` ```java @Test public void testSort() { //1. 根据age和height升序排序 Sort sort1 = Sort.by("age", "height"); List<Student> studentList1 = studentRepository.findBySex("男", sort1); System.out.println(studentList1); //[Student(id=1, name=张三, age=19, sex=男, height=170), // Student(id=2, name=张田, age=21, sex=男, height=165), // Student(id=7, name=小明, age=22, sex=男, height=173)] //2. 根据age和height升序排序 Sort sort2 = Sort.by(Sort.Direction.ASC, "age", "height"); List<Student> studentList2 = studentRepository.findBySex("男", sort2); System.out.println(studentList2); //[Student(id=1, name=张三, age=19, sex=男, height=170), // Student(id=2, name=张田, age=21, sex=男, height=165), // Student(id=7, name=小明, age=22, sex=男, height=173)] //3. 先根据age升序排序,然后再根据height降序排序 Sort sort3 = Sort.by(Sort.Order.asc("age"), Sort.Order.desc("height")); List<Student> studentList3 = studentRepository.findBySex("男", sort3); System.out.println(studentList3); //[Student(id=1, name=张三, age=19, sex=男, height=170), // Student(id=2, name=张田, age=21, sex=男, height=165), // Student(id=7, name=小明, age=22, sex=男, height=173)] //4. 先根据age升序排序,然后再根据height降序排序 List<Sort.Order> sortList = new ArrayList<>(2); sortList.add(Sort.Order.asc("age")); sortList.add(Sort.Order.desc("height")); Sort sort4 = Sort.by(sortList); List<Student> studentList4 = studentRepository.findBySex("男", sort4); System.out.println(studentList4); //[Student(id=1, name=张三, age=19, sex=男, height=170), // Student(id=2, name=张田, age=21, sex=男, height=165), // Student(id=7, name=小明, age=22, sex=男, height=173)] } ``` **3. 分页并排序** 分页可以调用接口`Pageable`来完成,而`Pageable`可以通过下面3个重载方法来构造。 ```java package org.springframework.data.domain; public class PageRequest extends AbstractPageRequest { //只分页不排序 public static PageRequest of(int page, int size) //既分页又排序。排序可以升序也可以降序 public static PageRequest of(int page, int size, Sort sort) //既分页又排序。排序只能是降序或升序 public static PageRequest of(int page, int size, Direction direction, String... properties) } ``` ```java @Test public void testPageable() { Pageable pageable = PageRequest.of(0, 2, Sort.by(Sort.Order.asc("age"),Sort.Order.desc("height"))); Page<Student> studentPage = studentRepository.findBySex("男", pageable); if(!studentPage.isEmpty()) { //获取数据 System.out.println(studentPage.getContent()); //[Student(id=1, name=张三, age=19, sex=男, height=170), // Student(id=2, name=张田, age=21, sex=男, height=165)] } } ```