1、UML类图

Spring Boot JPA分页查询_ide

2、接口方法

/*
* Copyright 2008-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
* Extension of {@link CrudRepository} to provide additional methods to retrieve entities using the pagination and
* sorting abstraction.
*
* @author Oliver Gierke
* @see Sort
* @see Pageable
* @see Page
*/
@NoRepositoryBean
public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {

/**
* 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);
}

(1)根据排序取所有对象的集合。
(2)根据分页和排序进行查询,并用Page对象封装。Pageable对象包含分页和Sort对象。

3、接口实现

/*
* (non-Javadoc)
* @see org.springframework.data.jpa.repository.JpaRepository#findAll(org.springframework.data.domain.Sort)
*/
@Override
public List<T> findAll(Sort sort) {
return getQuery(null, sort).getResultList();
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Pageable)
*/
@Override
public Page<T> findAll(Pageable pageable) {

if (isUnpaged(pageable)) {
return new PageImpl<T>(findAll());
}

return findAll((Specification<T>) null, pageable);
}

4、使用示例

4.1 继承PagingAndSortingRepository
/**
* 用户DAO接口层
* @author zhuhuix
* @date 2020-04-03
*/
public interface UserRepository extends PagingAndSortingRepository<User,Long>
4.2 测试
@Test
void test4(){
UserRepository userRepository = SpringContextHolder.getBean(UserRepository.class);
Page<User> page1= userRepository.findAll( PageRequest.of(0,10));
page1.forEach(user -> System.out.println(user.toString()));
Page<User> page2= userRepository.findAll( PageRequest.of(0,10,Sort.by("id")));
page2.forEach(user -> System.out.println(user.toString()));
}

结果如下:

Spring Boot JPA分页查询_ide_02

4.2 注意点:PageRequest

代替分页功能的方法是不要new PageRequest,而是直接调用PageRequest.of这个方法,并根据你的有需求选择参数
/**
* Creates a new {@link PageRequest} with sort parameters applied.
*
* @param page zero-based page index, must not be negative.
* @param size the size of the page to be returned, must be greater than 0.
* @param sort must not be {@literal null}, use {@link Sort#unsorted()} instead.
*/
protected PageRequest(int page, int size, Sort sort) {

  super(page, size);

Assert.notNull(sort, "Sort must not be null!");

this.sort = sort;
}

/**
* Creates a new unsorted {@link PageRequest}.
*
* @param page zero-based page index, must not be negative.
* @param size the size of the page to be returned, must be greater than 0.
* @since 2.0
*/
public static PageRequest of(int page, int size) {
return of(page, size, Sort.unsorted());
}

/**
* 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 must not be {@literal null}, use {@link Sort#unsorted()} instead.
* @since 2.0
*/
public static PageRequest of(int page, int size, Sort sort) {
return new PageRequest(page, size, sort);
}