如何在SpringBoot2怎么中实现一个JPA分页和排序分页功能

  介绍

今天就跟大家聊聊有关如何在SpringBoot2怎么中实现一个JPA分页和排序分页功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

分页

应用程序。yml

春:   ,数据源:   ,url: jdbc: mysql://127.0.0.1/jpa ? useUnicode=true& characterEncoding=utf-8& useSSL=false   ,用户名:根   ,密码:123456   ,driver-class-name: com.mysql.jdbc.Driver   ,jpa:   ,hibernate:   ,#更新或者创建数据表结构   ddl-auto:才能更新   ,#控制台显示SQL   ,show-sql:真的   ,属性:   hibernate.format_sql:,才能真正的

实体类

@ entity   @ table (=name “employee")   public  class  Employee  {   ,@ id   ,@GeneratedValue (strategy =, GenerationType.IDENTITY)   ,private  Integer  empId;   ,private  String 姓;   ,private  String 电子邮件;   ,@Temporal (TemporalType.DATE)   出生,private  Date ;   ,@Temporal (TemporalType.TIMESTAMP)   ,private  Date  createTime;   ,@ManyToOne   ,@JoinColumn (=name “dept_id")   ,private  Department ;   ,//省去,set 得到的方法   }   @ entity   @ table (=name “department")   public  class  Department  {   ,@ id   ,@GeneratedValue (strategy =, GenerationType.IDENTITY)   ,private  Integer  deptId;   ,private  String  deptName;   ,//省去,set 得到的方法   }

库接口类

import  com.springboot.jpa.entity.Employee;   import  org.springframework.data.jpa.repository.JpaRepository;   public  interface  EmployeeRepository  extends  JpaRepository<员工,Integer>, {   }

服务接口类

import  com.springboot.jpa.entity.Employee;   import  org.springframework.data.domain.Page;   public  interface  EmployeeService  {   ,//普通分页   ,Page getPage (Integer  pageNum, Integer  pageLimit);   ,//排序分页   ,Page getPageSort (Integer  pageNum, Integer  pageLimit);   }

服务实现类

import  com.springboot.jpa.dao.EmployeeRepository;   import  com.springboot.jpa.entity.Employee;   import  com.springboot.jpa.service.EmployeeService;   import  org.springframework.beans.factory.annotation.Autowired;   import  org.springframework.data.domain.Page;   import  org.springframework.data.domain.PageRequest;   import  org.springframework.data.domain.Pageable;   import  org.springframework.data.domain.Sort;   import  org.springframework.stereotype.Service;   import  org.springframework.transaction.annotation.Transactional;   @ service   public  class  EmployeeServiceImpl  implements  EmployeeService  {   ,@ autowired   ,private  EmployeeRepository  employeeRepository;   ,//普通分页   ,@Override   ,@ transactional (readOnly =, true),//,只读事务   ,public  Page getPage (Integer  pageNum, Integer  pageLimit), {   Pageable 才能;Pageable =new  PageRequest (pageNum 安康;1,pageLimit);   return 才能employeeRepository.findAll(可分页);   ,}   ,//分页排序   ,@Override   ,@ transactional (readOnly =, true),   ,public  Page getPageSort (Integer  pageNum, Integer  pageLimit), {   Sort 才能;Sort =, new 排序(Sort.Direction.DESC,“createTime");   Pageable 才能;Pageable =new  PageRequest (pageNum 安康;1,pageLimit,,排序);   return 才能employeeRepository.findAll(可分页);   ,}   }

控制器控制器类

import  com.springboot.jpa.entity.Employee;   import  com.springboot.jpa.service.EmployeeService;   import  org.springframework.beans.factory.annotation.Autowired;   import  org.springframework.data.domain.Page;   import  org.springframework.web.bind.annotation.GetMapping;   import  org.springframework.web.bind.annotation.RequestParam;   import  org.springframework.web.bind.annotation.RestController;   @RestController   public  class  EmployeeController  {   ,@ autowired   ,private  EmployeeService  employeeService;   ,//分页显示数据   ,@GetMapping (“/emp")   ,public  Page

如何在SpringBoot2怎么中实现一个JPA分页和排序分页功能