springboot-mongodb 翻页

两种方式:

方式一:

int currentPage = 10;

int pageNum=20;

int start = (currentPage -1)*pageNum;

 List ll = mongoTemplate.find(new Query(Criteria.where("bookId").is(bookId)).with(new Sort(Sort.Direction.DESC,"currenttime")).skip(start).limit(pageNum), CKDXH.class,”zcy1“);  

方式二:

 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").lte(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);

    }

游标的方式:


DBObject queryObject =new BasicDBObject();  
      queryObject.put("x","x")  

DBObjectfilterDBObject=new BasicDBObject();  
      filterDBObject.put("_id", 0);  
      filterDBObject.put("cname",1);  
      filterDBObject.put("onumber",1);  
         
      DBCursordbCursor=mongoTemplate.getCollection(collectionName).find(queryObject,filterDBObject);  
       
      //排序  
      DBObjectsortDBObject=newBasicDBObject();  
      sortDBObject.put("onumber",1);  
      dbCursor.sort(sortDBObject);  
      //分页查询  
      dbCursor.skip(page.getSkip()).limit(page.getPageSize());  
       
      //总数  
      int count=dbCursor.count();  
      //循环指针  
      List<Orders>datas=newArrayList<Orders>();  
      while (dbCursor.hasNext()) {  
        datas.add(morphia.fromDBObject(Orders.class, dbCursor.next()));  
      }  
       
      page.setRowCount(count);  
      page.setDatas(datas);  
      return page;  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值