Springboot Operation MongoDB Implement paging query

Source: Internet
Author: User
Tags mongodb mongodb driver

A few days ago wrote an article on the introduction of the simple use of springboot. As well as using springboot JPA to do a database of a curd (address: http://blog.csdn.net/canot/article/details/51449589) This document is simply learning the spring Boot JPA Or a simple operation of spring data for a NoSQL product MongoDB that is now popular (primarily for paging queries).

Similar to the previous Springboot HelloWorld, additional packages (MongoDB driver packages) are required for normal drive MongoDB after importing the necessary core packs:

        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactid>mongo-java-driver</ artifactid>
            <version>3.2.2</version>
        </dependency>

In the previous demo of spring boot, in order to enable the project to drive the database correctly. A configuration file must be added to the root of the project: Application.properties. The configuration file writes information about the database, such as which database to operate, the password for the account, and so on. This file is not required in the Operation MongoDB. When your database does not have an account password set, this profile can be omitted when the collection you want to manipulate is in the database test. If this is not the case, then the configuration file must be required:

Spring.data.mongodb.database:ticket
spring.data.mongodb.uri:mongodb://localhost:27017
//xxxx

DAO Interface:

@Repository public
Interface Customerdao  extends pagingandsortingrepository<customer,string>{

}

Entity classes:

Specifies that corresponds to the collection customer
@Document (collection = "Customer") public
class Customer {
//primary key
    @Id
    Private String _id;
    private String name;
    Private String phone;
    private String gender;
    Private String birthday;
    private String passport;
XXXX
}

Controller layer:

@Autowired Customerdao Customerdao; Complete the paging request @RequestMapping ("/selectname") public list<customer> Selectname (@RequestParam ("id") int id) {//
        Construction of paging information pagerequest pagerequest = Buildpagerequest (id,5,null);
        The query specifies the content of the paging iterator<customer> customers = Customerdao.findall (pagerequest). iterator ();
        list<customer> lists = new arraylist<customer> ();
        while (Customers.hasnext ()) {Lists.add (Customers.next ());
    return lists;      
     /** * * Creates a paging request.
        * * Private pagerequest buildpagerequest (int pagenumber, int pagesize,string sorttype) {sort sort = null;
        if ("Auto". Equals (SortType)) {sort = new sort (DIRECTION.DESC, "id");
        else if ("Birthday". Equals (SortType)) {sort = new sort (DIRECTION.ASC, "Birthday");
    ///Parameter 1 indicates the current first few pages, parameter 2 indicates the size of each page, and parameter 3 indicates the sort return new Pagerequest (Pagenumber-1,pagesize,sort); }

Spring Boot or Spring data provides us with the form of an interface to help us complete the crur operation of the table. So we're going to think about how to implement a query for a field that we've written in our own DAO in our own handwriting by writing a findbyxxx, which is also provided for us in spring boot or spring data, and we just need to write it in the interface, Without having to implement a class, it can help us achieve it.

query all sex for men and sort by birthdays

@Repository public
Interface Customerdao  extends pagingandsortingrepository<customer,string>{
/ /This method name cannot be findbyxxx, so the class must have the XXX field in it. That is to say, there must be a column public
   page<customer> findbygender (String gender,pageable pageable) corresponding to the XXX field in the corresponding database;

note here that while we call a paging query in the Repository method, the incoming parameter is pagerequest. But be sure the parameter is defined as pageable when the method is repository defined. Otherwise there will be an error: Paging query needs to have a pageable parameter

Controller:

 @RequestMapping ("/selectbygender") public page<customer> Getbygender (String Gende R, @RequestParam ("pagenumber") int pagenumber) {//Build paging information pagerequest pagerequest = Buildpagerequest (pagenumber,5, "
Birthday ");
 page<customer> Customera = Customerdao.findbygender (gender,pagerequest);
     return customers; }
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.