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 |
Mongodb |
Description |
Query limit (int limit) |
Limit |
method is to limit the number of results that the cursor returns |
Query Skip (int skip) |
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) |
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; }