SpringBoot-16-Spring-Data-Jpa实现分页排序
我们之前介绍了
但是我们在进行项目开发的时候,对数据进行查询有时候一张表包含上百万甚至上亿的数据,如果我们将这些数据一下返回前端网页进行展示,那么网页加载将会十分缓慢等各种问题,用户体验会变得十分的差,因此我们能需要对数据进行一定顺序的排序,以及分页查询。
代码的实现
定义实体类
还是上一章节的实体类Student
@Table(name="student") @Entity @Data public class Student { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) private Long id; private String name; private String sex; private int age; private String email; private String mobile; private int isEnabled; private Date createDate; private Date updateDate; }
注解的具体含义可以在上一章进行查看,这里就不过多进行重复结束了。
Service接口的定义
public interface StudentService extends PagingAndSortingRepository<Student, Long> { /** * 获取所有学生信息 * @return */ @Query("from Student s where s.isEnabled=1") Slice<Student> getAllSutdents(Pageable pageable); }
分页实现
分页的实现是通过Pageable进行的,Pageable是Spring的一个接口,用来进行分页参数的传递。例如:
Pageable pageable = PageRequest.of(0,10);第一页 //数据库操作获取查询结果 Page<Student> studentPage = studentService.findAll(pageable); //将查询结果转换为List List<Student> studentList = studentPage.getContent();
排序
Spring-Data-Jpa提供了一个sort
对象进行排序,例子:
//createTime的升序进行排序 studentService.findAll(Sort.by("createTime")); //name的升序排序,再按照createTime的降序进行排序 studentService.findAll(Sort.by("name").ascending() .and(Sort.by("createTime").descending()));
排序和分页一起
Page<Student> createDate = studentService.getAllSutdents(PageRequest.of(0, 1, Sort.by("createDate"))); return createDate.getContent();
控制层的实现
@Slf4j @RestController @RequestMapping("/student") public class StudentController { @Autowired private StudentService studentService; @GetMapping("getallstudents") public List<Student> getAllSutdents(PageNumber pageNumber){ if(pageNumber.getNumber()==0){ pageNumber =new PageNumber(); pageNumber.setNumber(0); pageNumber.setSize(10); } //分页查询 Page<Student> createDate = studentService.getAllSutdents(PageRequest.of(pageNumber.getNumber(), pageNumber.getSize(), Sort.by("createDate"))); return createDate.getContent(); } /** * 保存数据 * @param student * @return */ @PostMapping("create") public Student saveStudent(@RequestBody Student student) { //保存一个对象到数据库,insert studentService.save(student); return student; } @GetMapping("/{id}") public Student getSutdentInfo(@PathVariable("id") Long id) { Optional<Student> optional = studentService.findById(id); return optional.orElseGet(Student::new); } @GetMapping("/delete/{id}") public void deleteSutdent(@PathVariable("id") Long id) { //根据id删除1条数据库记录 studentService.deleteById(id); } @PostMapping("update") public @ResponseBody Student updatSutdent(@RequestBody Student student) { Optional<Student> optional = studentService.findById(student.getId()); Student stu = optional.orElseGet(Student::new); stu.setEmail(student.getEmail()); stu.setMobile(student.getEmail()); stu.setAge(student.getAge()); stu.setSex(student.getSex()); stu.setName(student.getName()); stu.setUpdateDate(new Date()); //保存一个对象到数据库,insert Student save = studentService.save(student); return save; } @GetMapping("getall") public Page<Student> getAll( PageNumber pageNumber) { if(pageNumber.getNumber()==0){ pageNumber =new PageNumber(); pageNumber.setNumber(0); pageNumber.setSize(10); } Page<Student> studentList =studentService.findAll(PageRequest.of(pageNumber.getNumber(),pageNumber.getSize())); //查询article表的所有数据 return studentList; } }
注:细心的同学我们发现分页我使用了连个对象Slice和Page
两者的区别在于Page是Slice的子接口,相比Slice多了两个方法:
//总页数 int getTotalPages(); //总数据条数 long getTotalElements();
测试:
启动项目打开postman,测试http://localhost:8899/student/getallstudents:结果为:
{ "content": [ { "id": 11, "name": "小芳", "sex": "女", "age": 23, "email": "825505212@qq.com", "mobile": "13333835900", "isEnabled": 1, "createDate": "2022-02-26T14:40:30.000+00:00", "updateDate": "2022-02-26T14:40:30.000+00:00" }, { "id": 24, "name": "李四", "sex": "男", "age": 23, "email": "825505242@qq.com", "mobile": "133338359345", "isEnabled": 1, "createDate": "2022-02-28T11:42:04.000+00:00", "updateDate": "2022-02-28T11:42:04.000+00:00" } ], "pageable": { "sort": { "empty": false, "sorted": true, "unsorted": false }, "offset": 0, "pageNumber": 0, "pageSize": 10, "unpaged": false, "paged": true }, "number": 0, "size": 10, "sort": { "empty": false, "sorted": true, "unsorted": false }, "numberOfElements": 2, "last": true, "first": true, "empty": false }
如果您觉得本文不错,欢迎关注支持,您的关注是我坚持的动力!
搜索公众号 springboot葵花宝典,回复 springboot 获取springboot相关资料