Spring Data and MongoDB integration five: Operations (pagination) __mongodb

Source: Internet
Author: User
Tags mongodb mongodb find rowcount

This article is reproduced to: http://blog.csdn.net/congcong68/article/details/47624221

one. Brief introduction

Springdata MongoDB provides org.springframework.data.mongodb.core.MongoTemplate to the MongoDB find operation, we last introduced a basic document query, we introduced today paging query, paging query is returned to the matching document cursor. The ability to modify query restrictions, jumps, and sort orders is optional.

We find in the query () Method accepts the query type with Org.springframework.data.mongodb.core.query and Org.springframework.data.mongodb.core.query.BasicQuery

The query class provides functionality for limit, skip, sort query restrictions, jumps, and sort orders, Basicquery inherits the query class.




Query limit (int limit)


method is to limit the number of results that the cursor returns

Query Skip (int skip)


Method can skip over the number of bars of the specified value, returning the result of the remaining number of bars, which can be combined with the limit () method to achieve pagination effect

Sort sort () is obsolete

Now it's using query.with (sort)


method to sort the data, depending on the specified field, and using 1 or-one to specify whether the sort is ascending or descending, similar to the SQL order by.

two. Basic Page Pagination

The query class provides the ability to limit, skip, sort query restrictions, jumps, and sort orders, and we implement query paging

First step: Implement the Paging tool class

[Java]  View plain  copy/**   *  page    *  @author  zhengcy   *   *  @param <T>   */   public classpagemodel<t>{        //result set        privateList<T> datas;       //Query record number        privateintrowCount;        //How many pieces of data per page        privateintpageSize=20;      //the first few pages        privateintpageNo=1;      //skipped a few        privateintskip=0;       /**       *   Total pages        *  @return        */        publicintgettotalpages () {             return (rowcount+pagesize-1)/pagesize;       }             public list<t>getdatas ()  {          return datas;      }      public  Void setdatas (list<t>datas)  {         this.datas =  datas;      }      public int getrowcount ()  {         return rowCount;      }       public void setrowcount (int rowcount)  {          this.rowCount = rowCount;      }      public int getpagesize ()  {          Return pagesize;      }      public void setpagesize (int  pageSize)  {         this.pageSize = pageSize;       }  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.