SpringDataJPA的PagingAndSortingRepository接口详解

学习了CrudRepository接口后还需要知道PagingAndSortingRepository接口,此接口是继承自CrudRepository,这个接口主要实现了分页和排序的功能,不需要像以前那样写多个的方法,比如TotalCount统计总条数,PageToTal统计总页数等等,就使用了这个方法,只要实现了PagingAndSortingRepository接口就写短短几行的代码就能实现分页.
如果不知道怎么搭建项目的可以参考我其他笔记:CrudRepository接口详解

先实现一个接口:

package com.gaozhi.repository;

import com.gaozhi.bean.Employee;
import org.springframework.data.repository.PagingAndSortingRepository;

public interface EmployeePagingAndSortingRepository extends PagingAndSortingRepository<Employee,Integer> {
}

编写测试类:

import com.gaozhi.bean.Employee;
import com.gaozhi.repository.EmployeePagingAndSortingRepository;
import com.gaozhi.repository.EmployeeRepository;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
 * Author:   Administrator
 * Date:     2020/1/12 15:19
 * Description:
 * History:
 */

public class TestEmployeePagingAndSortingRepository {
    private ApplicationContext applicationContext=null;
    private EmployeePagingAndSortingRepository employeePagingAndSortingRepository=null;
    @Before
    public void setUp(){
        applicationContext=new ClassPathXmlApplicationContext("beans.xml");
        employeePagingAndSortingRepository=applicationContext.getBean(EmployeePagingAndSortingRepository.class);
    }
    @After
    public void after(){
        applicationContext=null;
    }

    @Test
    public void testPage(){
        //从0开始,显示的条数是5条   index是从0开始的 而不是从1开始
        Pageable pageable=new PageRequest(0,5);
        Page<Employee> page = employeePagingAndSortingRepository.findAll(pageable);
        System.out.println("查询的总页数: "+page.getTotalPages());
        System.out.println("查询的总记录数: "+page.getTotalElements());
        //这个是显示的是第几页 如是第一页的会显示的是第0页,一般来说是从第一页开始的所以+1
        System.out.println("查询的当前第几页: "+(page.getNumber()+1));
        System.out.println("查询的当前页面的集合: "+page.getContent());
        System.out.println("查询的当前页面的记录数: "+page.getNumberOfElements());

    }

这里需要注意的是导包是导
import org.springframework.data.domain.Pageable 这个包而不是其他的包,怕有些手速够快的导成其他去了,要仔细点.
底层源码:

public PageRequest(int page, int size) {
		this(page, size, null);
	}

Pageable 有两个参数,第一个参数是第几页(从0开始),第二个参数是一页显示多少条的条数,具体详细的自己点开源文件看下,里面的底层是怎么样的,

分页排序

 @Test
    //分页排序
    public void TestPageAndSort(){
        Sort.Order order=new Sort.Order(Sort.Direction.DESC,"id");
        Sort sort=new Sort(order);
        //从0开始,显示的条数是5条   index是从0开始的 而不是从1开始
        Pageable pageable=new PageRequest(0,5,sort);
        Page<Employee> page = employeePagingAndSortingRepository.findAll(pageable);
        System.out.println("查询的总页数: "+page.getTotalPages());
        System.out.println("查询的总记录数: "+page.getTotalElements());
        //这个是显示的是第几页 如是第一页的会显示的是第0页,一般来说是从第一页开始的所以+1
        System.out.println("查询的当前第几页: "+(page.getNumber()+1));
        System.out.println("查询的当前页面的集合: "+page.getContent());
        System.out.println("查询的当前页面的记录数: "+page.getNumberOfElements());
    }

或许一开始看到这个接口的使用虽然很难,不是很清楚,还是要多看几遍,才能慢慢了解,每个人都是这样过来的!

格言:不驰于空想,不骛于虚声

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值