欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Data JPA(2)--利用PagingAndSortingRespository实现分页和排序

程序员文章站 2022-04-25 16:22:46
...

通过查看PagingAndSortingRepository接口源码,我们可以看到有两个方法来实现功能:

        /**
	 * Returns all entities sorted by the given options.
	 * 
	 * @param sort
	 * @return all entities sorted by the given options
	 */
	Iterable<T> findAll(Sort sort);//升序或者降序的查询所有

	/**
	 * Returns a {@link Page} of entities meeting the paging restriction provided in the {@code Pageable} object.
	 * 
	 * @param pageable
	 * @return a page of entities
	 */
	Page<T> findAll(Pageable pageable);//分页查询,传入Pageable对象,返回一个Page对象
下面通过实例来说明如何实现分页和排序的功能,并了解Sort对象,Pageable对象,Page对象如何声明实现.

1.Student实体类

@Entity
public class Student {
	private int sno;
	private String sname;
	private int sage;
	@Override
	public String toString() {
		return "Student [sno=" + sno + ", sname=" + sname + ", sage=" + sage
				+ "]";
	}
	@GeneratedValue
	@Id
	public int getSno() {
		return sno;
	}
getter,setter...

2.创建一个接口,继承PagingAndSortingRepository

public interface StudentPagingAndSortingRepository extends PagingAndSortingRepository<Student, Integer>{

}
3.测试类进行单元测试,代码
public class StudentPagingAndSortingRepositoryTest {
	private ApplicationContext ctx = null;
	private StudentPagingAndSortingRepository studentPagingAndSortingRepository = null;
	@Before
	public void setUp(){
		ctx = new ClassPathXmlApplicationContext("beans.xml");
		studentPagingAndSortingRepository = ctx.getBean(StudentPagingAndSortingRepository.class);
		System.out.println("setup");
	}
	@After
	public void tearDown(){
		ctx = null;
		System.out.println("teardown");
	}	
	//PagingAndSortingRepository
	//分页
	@Test
	public void testPage(){
		System.out.println("start");
		//PageRequest(page,size)  page从index0开始
		Pageable pageable = new PageRequest(0,2);
		Page<Student> page = studentPagingAndSortingRepository.findAll(pageable);
		System.out.println("查询的总页数:"+page.getTotalPages());
		System.out.println("查询的记录数:"+page.getTotalElements());
		System.out.println("查询当前的第几页:"+(page.getNumber()+1));
		System.out.println("查询当前页面的集合:"+page.getContent());
		System.out.println("查询当前页面的记录数:"+page.getNumberOfElements());
		System.out.println("started");
	}
	//分页加排序
	@Test
	public void testPageAndSort(){
		System.out.println("start");
		Order order = new Order(Direction.ASC,"sage");
		Sort sort = new Sort(order);
		Pageable pageable = new PageRequest(0,2,sort);
		Page<Student> page = studentPagingAndSortingRepository.findAll(pageable);
		System.out.println("查询的总页数:"+page.getTotalPages());
		System.out.println("查询的记录数:"+page.getTotalElements());
		System.out.println("查询当前的第几页:"+(page.getNumber()+1));
		System.out.println("查询当前页面的集合:"+page.getContent());
		System.out.println("查询当前页面的记录数:"+page.getNumberOfElements());
		System.out.println("started");
	}
}
这样就实现了分页与排序的功能.

我们回头看看实现的过程:

Pageable接口的实现方法,通过源码发现,它有两个子类PageRequest和QPageRequest,我们选用常用的PageRequest来实现它.

PageRequest继承了AbstractPageRequest类,它提供了一系列的方法,常用的有:

        /**
	 * Creates a new {@link PageRequest}. Pages are zero indexed, thus providing 0 for {@code page} will return the first
	 * page.
	 * 
	 * @param page zero-based page index.
	 * @param size the size of the page to be returned.
	 */
	public PageRequest(int page, int size) {
		this(page, size, null);//从哪一页开始(从0开始),页面容量
	}

	/**
	 * Creates a new {@link PageRequest} with sort parameters applied.
	 * 
	 * @param page zero-based page index.
	 * @param size the size of the page to be returned.
	 * @param direction the direction of the {@link Sort} to be specified, can be {@literal null}.
	 * @param properties the properties to sort by, must not be {@literal null} or empty.
	 */
	public PageRequest(int page, int size, Direction direction, String... properties) {
		this(page, size, new Sort(direction, properties));//开始页面,页面容量,排序方向,按照什么字段排序
	}

	/**
	 * Creates a new {@link PageRequest} with sort parameters applied.
	 * 
	 * @param page zero-based page index.
	 * @param size the size of the page to be returned.
	 * @param sort can be {@literal null}.
	 */
	public PageRequest(int page, int size, Sort sort) {
		super(page, size);
		this.sort = sort;//开始页面,页面容量,排序方向
	}

	/*
	 * (non-Javadoc)
	 * @see org.springframework.data.domain.Pageable#getSort()
	 */
	public Sort getSort() {
		return sort;
	}

	/* 
	 * (non-Javadoc)
	 * @see org.springframework.data.domain.Pageable#next()
	 */
	public Pageable next() {
		return new PageRequest(getPageNumber() + 1, getPageSize(), getSort());//下一页
	}

	/* 
	 * (non-Javadoc)
	 * @see org.springframework.data.domain.AbstractPageRequest#previous()
	 */
	public PageRequest previous() {
		return getPageNumber() == 0 ? this : new PageRequest(getPageNumber() - 1, getPageSize(), getSort());//是否存在页面
	}

	/* 
	 * (non-Javadoc)
	 * @see org.springframework.data.domain.Pageable#first()
	 */
	public Pageable first() {
		return new PageRequest(0, getPageSize(), getSort());//第一页
	}

Sort类源码:

        /**
	 * Creates a new {@link Sort} instance using the given {@link Order}s.
	 * 
	 * @param orders must not be {@literal null}.
	 */
	public Sort(Order... orders) {
		this(Arrays.asList(orders));//Sort的构建需要先实例化Order对象,Order order = new Order(Direction.ASC,"sage");ASC升序,DESC降序
	}