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);

    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM