Spring Data MongoDB Paging Query

Source: Internet
Author: User
Tags mongodb stub

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

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.