jpa实现分页排序查询--笔记

@Test
    void queryPage() {
        /**
         * 自定义查询条件
         *      1. 实现Specification接口(提供泛型;查询的对象类型)
         *      2. 实现toPredicate方法(构造查询条件)
         *      3.  需要借助方法参数中的两个参数(
         *              root:获取需要查询的对象属性
         *              CriteriaBuilder:构造查询条件,内部封装了很多查询条件(模糊匹配,精准匹配)
         */
        Specification<Student> spec = (Specification<Student>) (root, query, criteriaBuilder) -> {
            Path<Student> path = root.get("isAvailable");
            Predicate equal = criteriaBuilder.equal(path, "1");
            return equal;
        };

        Integer pageNo = 0;
        Integer pageSize = 2;
        /**
         * 添加排序Sort
         *      Sort.Direction.DESC表示降序
         *      Sort.Direction.ASC表示升序
         *      properties是指实体类的属性名(不是字段名)
         */
        Sort.Order order = Sort.Order.asc("id");
        Sort sort = Sort.by(order);

        /**
         *  分页参数Pageable
         *      参数1:查询的页码
         *      参数2:每页查询的条数
         *      参数3:查询结果的排序规则(可选
         */
        Pageable pageable = PageRequest.of(pageNo, pageSize, sort); //原来的new PageRequest()已经过时
        /**
         *  分页查询
         *      参数1:查询条件Specification
         *      参数2:分页参数Pageable
         */
        Page<Student> page = userRepository.findAll(spec, pageable);
        System.err.println(page.getContent());
    }
    ```
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GG_com

1分也是爱!!!!!!!!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值