- 一、不使用@Query注解完成简单的分页:查询所有
- 二、不使用@Query注解完成简单的分页:通过姓名字段查询学生记录
- 三、不使用@Query注解完成简单的分页:通过分数范围查询学生记录
- 四、使用@Query注解完成简单的分页:通过课程名称查询学生记录
- 五、使用Specification完成简单的分页+查询条件查询:通过限制性条件查询学生记录
- 六、使用Specification完成简单的分页+查询条件查询:通过限制性条件查询学生记录
- 总结
今天学习使用JPA以及Pageable完成SpringBoot项目中的分页功能
一、不使用@Query注解完成简单的分页:查询所有
在repo层(持久层)接口中建立函数findAll()
代码如下:
public interface StudentGradeRepo1 extends JpaRepository{ Page findAll(Pageable pageable); }
在表现层(controller)中传入参数并测试(业务层代码省略)
@RequestMapping("/findAll")
public Page findAll(@RequestParam("pageNo") Integer pageNo){
//固定每页展示10条记录
int pageSize = 10;
//Pageable页码从0开始,所以传入的页码要-1
return studentGradeService1.findAll(PageRequest.of(pageNo-1, pageSize));
}
二、不使用@Query注解完成简单的分页:通过姓名字段查询学生记录
代码如下:
在repo层(持久层)接口中建立函数findByStudentName()
代码如下:
public interface StudentGradeRepo1 extends JpaRepository{ Page findByStudentName(String studentName, Pageable pageable); }
在表现层(controller)中传入参数并测试(业务层代码省略)
@RequestMapping("/findByStudentName")
public Page findByStudentName(@RequestParam("studentName") String studentName,
@RequestParam("pageNo") Integer pageNo){
//固定每页展示10条记录
int pageSize = 10;
//page从0开始,所以实际值要-1
return studentGradeService1.findByStudentName(studentName, PageRequest.of(pageNo-1, pageSize));
}
三、不使用@Query注解完成简单的分页:通过分数范围查询学生记录
代码如下:
public interface StudentGradeRepo1 extends JpaRepository{ Page findByGradeBetween(BigDecimal lowGrade, BigDecimal highGrade, Pageable pageable); }
在表现层(controller)中传入参数并测试(业务层代码省略)
@RequestMapping("/findByGradeBetween")
public Page findByGradeBetween(@RequestParam("lowGrade") BigDecimal lowGrade,
@RequestParam("highGrade") BigDecimal highGrade,
@RequestParam("pageNo") Integer pageNo){
//固定每页展示10条记录
int pageSize = 10;
//page从0开始,所以实际值要-1
return studentGradeService1.findByGradeBetween(lowGrade, highGrade, PageRequest.of(pageNo-1, pageSize));
}
四、使用@Query注解完成简单的分页:通过课程名称查询学生记录
代码如下:
public interface StudentGradeRepo1 extends JpaRepository{ @Query(value = "select new 包路径.StudentGrade(sg.id, sg.studentName, sg.course, sg.grade, sg.className) n" + "from student_grade sg n" + "where sg.course = :course") Page findByCourse(String course, Pageable pageable); }
在表现层(controller)中传入参数并测试(业务层代码省略)
@RequestMapping("/findByCourse")
public Page findByCourse(@RequestParam("course") String course,
@RequestParam("pageNo") Integer pageNo){
//固定每页展示10条记录
int pageSize = 10;
//page从0开始,所以实际值要-1
return studentGradeService1.findByCourse(course, PageRequest.of(pageNo, pageSize));
}
五、使用Specification完成简单的分页+查询条件查询:通过限制性条件查询学生记录
对输入字段做了限制,代码如下:
public interface StudentGradeRepo1 extends JpaRepository{ Page findAll(Specification specification, Pageable pageable); }
在表现层(controller)中传入参数并测试(业务层代码省略)
@RequestMapping("/findAll")
public Page findAll(@RequestParam(value = "studentName", required = false) String studentName,
@RequestParam(value = "course", required = false) String course,
@RequestParam(value = "lowGrade", required = false) BigDecimal lowGrade,
@RequestParam(value = "highGrade", required = false) BigDecimal highGrade,
@RequestParam(value = "pageNo", required = false) Integer pageNo){
//固定每页展示10条记录
int pageSize = 10;
//构造查询条件
Specification specification = new Specification() {
@Override
public Predicate toPredicate(Root root, //查询的类型(实体类的一个路径,告诉查询用的是哪个字段),使用get获取字段
CriteriaQuery> query, //查询的条件
CriteriaBuilder criteriaBuilder) { //构造条件
//创建条件集合
List predicates = new ArrayList<>();
//判断传入参数是否为空,不为空就添加至查询条件中
if(studentName != null){
//equal表示传入字段需要等于数据库字段(这里可以改成like变为模糊查询)
predicates.add(criteriaBuilder.equal(root.get("studentName"), studentName));
}
if(course != null){
predicates.add(criteriaBuilder.equal(root.get("course"), course));
}
if((lowGrade != null) && (highGrade != null)){
predicates.add(criteriaBuilder.between(root.get("grade"), lowGrade, highGrade));
}
//创建一个条件的集合,长度为上面满足条件的个数(目的是将predicates列表转为集合)
Predicate[] pre = new Predicate[predicates.size()];
return query.where(predicates.toArray(pre)).getRestriction();
}
};
return studentGradeService1.findAll(specification, PageRequest.of(pageNo-1, pageSize));
}
六、使用Specification完成简单的分页+查询条件查询:通过限制性条件查询学生记录
对输入字段做了限制:
1.传入是json对象,而非form形式
2.返回为设定的类,而非普通分页
3.分数范围作出限制,不在此范围内的不予查询
代码如下:
public interface StudentGradeRepo1 extends JpaRepository{ Page findAll(Specification specification, Pageable pageable); }
在表现层(controller)中传入参数并测试(业务层代码省略)
@RequestMapping("/findAll2")
public Result> findAll2(@RequestBody Map map){
//固定每页展示10条记录
int pageSize = 10;
//判断输入是否合法(在[0,120]之内)
if(!(map.get("lowGrade").isEmpty()) && (!map.get("highGrade").isEmpty())){
if((Double.parseDouble(map.get("lowGrade")) < 0) || (Double.parseDouble(map.get("lowGrade")) > 120)){
return Result.errorWithMsg("输入分数范围不对,目标值范围为[0,120]");
}
if((Double.parseDouble(map.get("highGrade")) < 0) || (Double.parseDouble(map.get("highGrade")) > 120)){
return Result.errorWithMsg("输入分数范围不对,目标值范围为[0,120]");
}
}
//构造查询条件
Specification specification = new Specification() {
@Override
public Predicate toPredicate(Root root, //查询的类型(实体类的一个路径,告诉查询用的是哪个字段),使用get获取字段
CriteriaQuery> query, //查询的条件
CriteriaBuilder criteriaBuilder) { //构造条件
//创建条件集合
List predicates = new ArrayList<>();
//判断传入参数是否为空,不为空就添加至查询条件中
if(!map.get("studentName").isEmpty()){
//equal表示传入字段需要等于数据库字段(这里可以改成like变为模糊查询)
//第一个参数指定字段,第二个参数指定在查询时该字段的具体内容
predicates.add(criteriaBuilder.like(root.get("studentName"),"%" + map.get("studentName") + "%"));
}
if(!map.get("course").isEmpty()){
predicates.add(criteriaBuilder.equal(root.get("course"), map.get("course")));
}
if(!(map.get("lowGrade").isEmpty()) && (!map.get("highGrade").isEmpty())){
predicates.add(criteriaBuilder.between(root.get("grade"),
new BigDecimal(map.get("lowGrade")),
new BigDecimal(map.get("highGrade"))));
}
//创建一个条件的集合,长度为上面满足条件的个数(目的是将predicates列表转为集合)
Predicate[] pre = new Predicate[predicates.size()];
return query.where(predicates.toArray(pre)).getRestriction();
}
};
return Result.ok(studentGradeService1.findAll(specification, PageRequest.of(Integer.parseInt(map.get("pageNo"))-1, pageSize)));
}
总结
自己的一个学习整理~



