自定义java Pageable分页对象

前两天写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");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值