前两天写service层的方法中需要对数据库中的数据进行分页查询,本来都是在接口层由前端传过来一个Pageable对象,在接口中对Pageable对象用注解进行定义,所以一时间不知道怎么写,后来得知,其实可以这样写:
可以使用Pageable的实现类AbstractPageRequest的子类PageRequest.of定义
/**
* Creates a new {@link PageRequest} with sort direction and properties 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 direction must not be {@literal null}.//排序方向,不能为空
* @param properties must not be {@literal null}.//排序属性,不能为空
* @since 2.0
* @since 2.0
*/
public static PageRequest of(int page, int size, Direction direction, String... properties) {
return of(page, size, Sort.by(direction, properties));
}
例:
/**
*
*第0页开始,每页100个,排序方向递减,排序属性id
*/
int page = 0;
int pageSize = 100;
PageRequest pageRequest = PageRequest.of(page, pageSize, Sort.Direction.DESC, "id");