Paging query on the basis of the previous article Spring Data MongoDB Environment defines a common paging parameter class that implements the Pageable interface
Import java.io.Serializable;
Import org.springframework.data.domain.Pageable;
Import Org.springframework.data.domain.Sort;
public class Springdatapageable implements Serializable, pageable {private static final long serialversionuid = 1;
Current page Private Integer pagenumber = 1;
The current number of page bars private Integer pagesize = 10;
Sort criteria private sort sort;
Current page @Override public int getpagenumber () {return getpagenumber ();
}//Each page shows the number of bars @Override public int getpagesize () {return getpagesize ();
}//The number of additions required for the second page @Override public int getoffset () {return (Getpagenumber ()-1) * getpagesize ();
} @Override Public Sort Getsort () {return sort;
} public Integer Getpagenumber () {return pagenumber;
} public void Setpagenumber (Integer pagenumber) {this.pagenumber = pagenumber;
} public Integer GetPageSize () {return pagesize;
} public void SetPageSize (Integer pagesize) {this.pagesize = pagesize;
} public void Setsort (sort sort) {this.sort = sort;
} @Override Public pageable First () {//TODO auto-generated method stub return null;
} @Override public Boolean hasprevious () {//TODO auto-generated method stub return false;
} @Override public pageable next () {//TODO auto-generated method stub return null;
} @Override public pageable Previousorfirst () {//TODO auto-generated method stub return null; }
}
Interface Persondao
/** *
Paging query
* @param pagenum Start Page
* @return *
/public
page<person> Paginationquery ( Integer pagenum);
Interface Implementation Persondaoimpl
Public page<person> paginationquery (Integer pagenum) {
springdatapageable pageable = new Springdatapageable ( );
Query query = new query ();
list<order> orders = new arraylist<order> (); Sort
Orders.add (New Order (Direction.desc, "age"));
Sort sort = new sort (orders);
Start Page
pageable.setpagenumber (pagenum);
Number of pages per page
pageable.setpagesize (ten);
Sorting
pageable.setsort (sort);
Query out the total number of bars
Long count = Mongotemplate.count (query, person.class);
Query
list<person> List = Mongotemplate.find (Query.with (pageable), person.class);
Encapsulates a collection with a paged result
page<person> pagelist = new pageimpl<person> (list, pageable, count);
return pagelist;
}
Test
/**
* Paged Query *
/@Test public
void Paginationquery () {
page<person> persons = Persondao.paginationquery (2);
for (person p:persons) {
System.out.println (P.getname ());
}
}
Author: itmyhome