欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring data jpa 分页查询

程序员文章站 2022-04-25 11:16:37
...
    public Page<GoodsInfo> searchGoodsListPage(int pageIndex, int pageSize, final TblTaoyGoodsInfo goodsInfo, final Map paramMap)
    {
        String priceOrd = StringUtil.getStrValFromMap(paramMap, "priceOrd");
        String salesOrd = StringUtil.getStrValFromMap(paramMap, "salesOrd");
        String isHighProfit = StringUtil.getStrValFromMap(paramMap, "isHighProfit");
        String couponOrd = StringUtil.getStrValFromMap(paramMap, "couponOrd");
        final String isCoupon = StringUtil.getStrValFromMap(paramMap, "isCoupon");
        final String isDayCoupon = StringUtil.getStrValFromMap(paramMap, "isDayCoupon");
        final String startTime= StringUtil.getStrValFromMap(paramMap, "startTime");
        final String endTime= StringUtil.getStrValFromMap(paramMap, "endTime");
        //规格定义
        Specification<GoodsInfo> specification = new Specification<GoodsInfo>() {

            /**
             * 构造断言
             * @param root 实体对象引用
             * @param query 规则查询对象
             * @param cb 规则构建对象
             * @return 断言
             */
            @Override
            public Predicate toPredicate(Root<GoodsInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>(); //所有的断言
                if(1 == goodsInfo.getIs99()){ //添加断言 9.9包邮
                    Predicate is99 = cb.equal(root.get("is99").as(Integer.class), goodsInfo.getIs99());
                    predicates.add(is99);
                }
                if(1 == goodsInfo.getIsJuhuasuan()){ //添加断言 聚划算
                    Predicate isJuhuasuan = cb.equal(root.get("isJuhuasuan").as(Integer.class), goodsInfo.getIsJuhuasuan());
                    predicates.add(isJuhuasuan);
                }
                if(1 == goodsInfo.getIsTaoqg()){ //添加断言 淘抢购
                    Predicate isTaoqg = cb.equal(root.get("isTaoqg").as(Integer.class), goodsInfo.getIsTaoqg());
                    predicates.add(isTaoqg);
                }
                if(1 == goodsInfo.getIsHot()){ //添加断言 淘抢购
                    Predicate isHot = cb.equal(root.get("isHot").as(Integer.class), goodsInfo.getIsHot());
                    predicates.add(isHot);
                }
                if(ExStringUtils.isNotBlank(goodsInfo.getGoodsTitle())){ // 按标题搜索
                    Predicate goodsTitle = cb.like(root.get("goodsTitle").as(String.class), "%"+goodsInfo.getGoodsTitle()+"%");
                    predicates.add(goodsTitle);
                }
                if("1".equals(isCoupon)){
                    Predicate isCoupon = cb.isNotNull(root.get("couponId").as(String.class));
                    predicates.add(isCoupon);
                }
                // 每日神券
                if("1".equals(isDayCoupon)){ // 开始时间<= 当日 00:00:00 结束时间 >= 当日23:59:59
                    predicates.add(cb.lessThanOrEqualTo(root.get("couponStartTime").as(String.class), startTime);
                    predicates.add(cb.greaterThan(root.get("couponEndTime").as(String.class), endTime));
                }
                return cb.and(predicates.toArray(new Predicate[0]));
            }
        };
        
        pageIndex = pageIndex <= 0 ? 0 : pageIndex - 1;
        Pageable pageable = null;
        //多属性排序 按add先后顺序
        List< Order> orders=new ArrayList< Order>();
        if("1".equals(isHighProfit)){
            orders.add( new Order(Sort.Direction. DESC, "aboutFee"));
        }
        if("1".equals(couponOrd)){
            orders.add( new Order(Sort.Direction. DESC, "couponPrice"));
        }
        if("1".equals(priceOrd)){
            orders.add( new Order(Sort.Direction. ASC, "goodsPrice"));
        }
        if("0".equals(priceOrd)){
            orders.add( new Order(Sort.Direction. DESC, "goodsPrice"));
        }
        
        if("1".equals(salesOrd)){
            orders.add( new Order(Sort.Direction. ASC, "volume"));
        }
        if("0".equals(salesOrd)){
            orders.add( new Order(Sort.Direction. DESC, "volume"));
        }
        if(orders.size()>0){
            pageable = new PageRequest(pageIndex, pageSize, new Sort(orders));
        }else{
            pageable = new PageRequest(pageIndex, pageSize);
        }
            
        Page<GoodsInfo> all = goodsRepository.findAll(specification, pageable);
        return all;
    }