springboot jpa mongodb 多条件分页查询

public Page<Recorded> getRecordeds(Integer page, Integer size, Recorded recorded) {

        if (page<1){

            page=1;

        }

        Sort sort = new Sort(Sort.Direction.DESC,"createTime");

        Pageable pageable = new PageRequest(page-1,size,sort);

        Query query = new Query();

        //条件id =XX

        Criteria criteria = Criteria.where("callerId").is(recorded.getCallerId());

criteria.and(“status”).is(Recorded.SUCCESS);

if(startTime!=null&&endTime!=null){

            criteria.andOperator(

                    Criteria.where("createTime").gte(startTime),

                    Criteria.where("createTime").lt(endTime)

            );

        }

query.addCriteria(criteria);

        //mongoTemplate.count计算总数

        long total = mongoTemplate.count(query, Recorded.class);   

        // mongoTemplate.find 查询结果集

        List<Recorded> items = mongoTemplate.find(query.with(pageable), Recorded.class);

        return new PageImpl(items, pageable, total);

    }

猜你喜欢

转载自blog.csdn.net/qq_31275085/article/details/86507455