Spring Boot JPA分页查询
程序员文章站
2022-05-17 09:25:42
...
1、UML类图
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()));
}
结果如下:
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);
}
推荐阅读
-
在Spring Boot中使用Spring-data-jpa实现分页查询
-
spring boot + jpa + kotlin入门实例详解
-
Spring Boot JPA如何把ORM统一起来
-
序列化表单为json对象,datagrid带额外参提交一次查询 后台用Spring data JPA 实现带条件的分页查询 多表关联查询
-
使用Spring Data JPA进行数据分页与排序
-
spring-data-jpa实现增删改查以及分页操作方法
-
详解spring boot jpa整合QueryDSL来简化复杂操作
-
Spring Data JPA例子代码[基于Spring Boot、Mysql]
-
Angular+Bootstrap+Spring Boot实现分页功能实例代码
-
Spring Boot+mybatis多表链接查询,重复外键