加入收藏 | 设为首页 | 会员中心 | 我要投稿 开发网_运城站长网 (https://www.0359zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

使用Spring Data JPA进行分页和排序

发布时间:2022-09-12 16:36:33 所属栏目:MsSql教程 来源:
导读:  概观

  在处理大量数据时Mssq分页存储过程,延迟处理通常是必不可少的。即使服务返回大量数据,消费者也不太可能使用它。考虑一个购物网站,客户在该网站上搜索产品,该网站有数千种产品可供展示。获取数

  概观
 
  在处理大量数据时Mssq分页存储过程,延迟处理通常是必不可少的。即使服务返回大量数据,消费者也不太可能使用它。考虑一个购物网站,客户在该网站上搜索产品,该网站有数千种产品可供展示。获取数千种产品并在网页上显示它们将非常耗时。在大多数情况下,客户甚至可能不会查看所有产品。
 
  对于这种情况,使用称为分页的技术。首先只显示一小部分产品(页面),客户可以要求查看下一个子集(页面)等。
 
  要了解JPA和Spring Data JPA的基础知识,请查看以下链接:
 
  实体
 
  为了本教程的目的,我们将考虑Employee 实体的最简单示例 。下面是 Employee 实体类。
 
  @Entity
  public class Employee {
      @Id private Long name;
      private String firstName;
      private String lastName;
      private Date dateOfBirth;
      private Integer age;
      private String designation;
      private double salary;
      private Date dateOfJoining;
      public Long getName() {
          return name;
      }
      public void setName(Long name) {
          this.name = name;
      }
      public String getFirstName() {
          return firstName;
      }
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
      public String getLastName() {
          return lastName;
      }
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
      public Date getDateOfBirth() {
          return dateOfBirth;
      }
      public void setDateOfBirth(Date dateOfBirth) {
          this.dateOfBirth = dateOfBirth;
      }
      public Integer getAge() {
          return age;
      }
      public void setAge(Integer age) {
          this.age = age;
      }
      public String getDesignation() {
          return designation;
      }
      public void setDesignation(String designation) {
          this.designation = designation;
      }
      public double getSalary() {
          return salary;
      }
      public void setSalary(double salary) {
          this.salary = salary;
      }
      public Date getDateOfJoining() {
          return dateOfJoining;
      }
      public void setDateOfJoining(Date dateOfJoining) {
          this.dateOfJoining = dateOfJoining;
      }
  }
  想要了解有关在Spring和Spring Boot中使用Java Persistence API(JPA)的更多信息?
 
  查看以下附加链接:
 
  员工存储库
 
  在 Spring Data JPA查询方法一文中,我们已经了解了Spring存储库接口和查询方法。在这里,我们需要学习分页,所以我们将使用Spring PagingAndSortingRepository。
 
  @Repository
  public interface EmployeeRepository extends PagingAndSortingRepository {
      Page findAll(Pageable pageable);
      Page findByFirstName(String firstName, Pageable pageable);
      Slice findByFirstNameAndLastName(String firstName, String lastName, Pageable pageable);
  }
  分页
 
  看看吧 EmployeeRepository。 该方法接受 Pageable 参数。 Pageable 是一个由Spring定义的接口,它拥有一个PageRequest。让我们看看如何创建一个 PageRequest。
 
  Pageable pageable = PageRequest.of(0, 10);
  Page page = employeeRepository.findAll(pageable);
  在第一行中,我们创建了 PageRequest10名员工,并要求提供第一页(0)。传递了页面请求findAll 以获取Employees页面作为响应。
 
  如果我们想要访问下一组后续页面,我们可以每次都增加页码。
 
  PageRequest.of(1, 10);
  PageRequest.of(2, 10);
  PageRequest.of(3, 10);
  ...
  排序
 
  Spring Data JPA提供了一个 Sort 对象以提供排序机制。我们来看看排序方式。
 
  employeeRepository.findAll(Sort.by("fistName"));
  employeeRepository.findAll(Sort.by("fistName").ascending().and(Sort.by("lastName").descending());
  显然,第一个按“firstName”排序,另一个按“firstName”升序和“lastName”降序排序。
 
  分页和排序
 
  Pageable pageable = PageRequest.of(0, 20, Sort.by("firstName"));
  Pageable pageable = PageRequest.of(0, 20, Sort.by("fistName").ascending().and(Sort.by("lastName").descending());
  切片与 页
 
  在EmployeeRepository,我们看到其中一个方法返回 Slice ,另一个返回 Page。它们都是Spring Data JPA,其中 Page 是子接口 Slice。它们都用于保存和返回数据子集。我们一个一个地看看它们
 
  切片
 
  该 Slice 知道,如果它有内容,如果它是第一个或最后一个切片。它还能够返回 Pageable当前和先前切片中使用的。我们来看看一些重要的方法 Slice。
 
  List getContent(); // get content of the slice
  Pageable getPageable(); // get current pageable
  boolean hasContent();
  boolean isFirst();
  boolean isLast();
  Pageable nextPageable(); // pageable of the next slice
  Pageable previousPageable(); // pageable of the previous slice
  页
 
  Page 是一个子接口, Slice 并有几个额外的方法。它知道表中的总页数以及记录总数。以下是一些重要的方法Page。
 
  static  Page empty; //create an empty page
  long getTotalElements(); // number of total elements in the table
  int totalPages() // number of total pages in the table
  摘要
 
  在使用Spring Data JPA的分页和排序示例中, 我们了解了为什么需要分页。我们还学习了如何获取分页和排序的数据子集。最后,我们也看到了 Slice 和 Page 接口以及他们之间的分歧。
 

(编辑:开发网_运城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!