12th Chapter Springboot + MONGODB (complex query)

Source: Internet
Author: User
Tags findone

    • Simple query: Use a custom Xxxrepository interface. (See Chapter 11th Springboot + mongodb (simple query))
    • Complex Queries : Building classes with mongotemplate and some query criteria (basicdblist, basicdbobject, criteria, etc.)

1, Application.properties

1 #mongodb note:mongo3.x would not use the host and port,only use Uri2 #spring. data.mongodb.host=192.168.22.1103 #spring. data.m ongodb.port=270174 #spring. DATA.MONGODB.DATABASE=MYFIRSTMONGODB5 spring.data.mongodb.uri=mongodb:// 192.168.22.110:27017/myfirstmongodb

Description

    • Mongo2.x supports both of the above configuration methods
    • mongo3.x only supports URI mode

2. Admin

 PackageCom.xxx.firstboot.domain;Importorg.springframework.data.annotation.Id;/*** Test complex MONGO Queries*/ Public classAdmin {@IdPrivateString Adminid; PrivateString name; PrivateInteger sex; PrivateString address;  PublicString Getadminid () {returnAdminid; }     Public voidSetadminid (String adminid) { This. Adminid =Adminid; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicInteger Getsex () {returnsex; }     Public voidsetsex (Integer sex) { This. Sex =sex; }     PublicString getaddress () {returnaddress; }     Public voidsetaddress (String address) { This. Address =address; }}
View Code

Attention:

    • @Id must have

3, Adminrepository

 Package Com.xxx.firstboot.mongo; Import org.springframework.data.mongodb.repository.MongoRepository; Import com.xxx.firstboot.domain.Admin;  Public Interface extends Mongorepository<admin, string> {}

Description: This interface is used for simple queries. Here is an empty interface with CRUD functionality.

4, Customercontroller

/********************* to test complex MONGO queries **********************/@AutowiredPrivateadminrepository adminrepository; @AutowiredPrivatemongotemplate mongotemplate; @ApiOperation ("Add an admin") @RequestMapping (value= "/addadmin", method =requestmethod.get) PublicAdmin addadmin (@RequestParam ("name") String name, @RequestParam ("Sex") Integer sex, @RequestParam ("Address") (String address) {Admin Admin=NewAdmin ();        Admin.setname (name);        Admin.setsex (Sex);        Admin.setaddress (address); returnadminrepository.save (admin); } @ApiOperation ("Get all admin") @RequestMapping (value= "/getalladmin", method =requestmethod.get) PublicList<admin>getalladmin () {returnAdminrepository.findall (); } @ApiOperation ("Complex admin Query") @RequestMapping (value= "/getadminbynameandsexoraddress", method =requestmethod.get) PublicAdmin getadminbynameandsexoraddress (@RequestParam ("name") String name, @RequestParam (value= "Sex", required=false) Integer sex, @RequestParam (value= "Address", required=false) (String address) {/*** OR*/basicdblist orlist=NewBasicdblist ();//used to record        if(Sex! =NULL) {Orlist.add (NewBasicdbobject ("Sex", Sex)); }        if(Stringutils.isnotblank (address)) {Orlist.add (NewBasicdbobject ("Address", address)); } basicdbobject Ordbobject=NewBasicdbobject ("$or", orlist); /*** and*/basicdblist andlist=Newbasicdblist (); Andlist.add (NewBasicdbobject ("name", name));        Andlist.add (Ordbobject); Basicdbobject Anddbobject=NewBasicdbobject ("$and", andlist); returnMongotemplate.findone (NewBasicquery (Anddbobject), Admin.class); }
View Code

Description

    • Getadminbynameandsexoraddress to implement SELECT * from admin where name =? and (sex =? or address =?)
    • Build query parameters with Basicdblist and Basicdbobject
    • FindOne returns the first qualifying result, find returns a list of results that match the criteria
    • The collection of the above query is admin (VO's simple class name) or you can specify a query from a collection (see Find, FindOne, etc.)

Note: The query field for MongoDB must be lowercase .

Test:

Start MONGO, launch the app, open swagger, access.

Reference:

http://blog.csdn.net/congcong68/article/details/47183209

12th Chapter Springboot + MONGODB (complex query)

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.