- 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 URI 2 #spring. data.mongodb.host=192.168.22.110 3 #spring. data.mongodb.port=27017 4 #spring. Data.mongodb.database=myfirstmongodb 5 Spring.data.mongodb.uri=mongodb://192.168.22.110:27017/myfirstmongodb
View Code
Description
- Mongo2.x supports both of the above configuration methods
- mongo3.x only supports URI mode
2. Admin
1 PackageCom.xxx.firstboot.domain;2 3 Importorg.springframework.data.annotation.Id;4 5 /**6 * Test complex MONGO Queries7 */8 Public classAdmin {9 @IdTen PrivateString Adminid; One PrivateString name; A PrivateInteger sex; - PrivateString address; - the PublicString Getadminid () { - returnAdminid; - } - + Public voidSetadminid (String adminid) { - This. Adminid =Adminid; + } A at PublicString GetName () { - returnname; - } - - Public voidsetName (String name) { - This. Name =name; in } - to PublicInteger Getsex () { + returnsex; - } the * Public voidsetsex (Integer sex) { $ This. Sex =sex;Panax Notoginseng } - the PublicString getaddress () { + returnaddress; A } the + Public voidsetaddress (String address) { - This. Address =address; $ } $ -}
View Code
Attention:
3, Adminrepository
1 Package Com.xxx.firstboot.mongo; 2 3 Import org.springframework.data.mongodb.repository.MongoRepository; 4 5 Import com.xxx.firstboot.domain.Admin; 6 7 Public Interface extends Mongorepository<admin, string> {8 }
View Code
Description: This interface is used for simple queries. Here is an empty interface with CRUD functionality.
4, Customercontroller
1 /********************* to test complex MONGO queries **********************/2 @Autowired3 Privateadminrepository adminrepository;4 @Autowired5 Privatemongotemplate mongotemplate;6 7@ApiOperation ("Add an admin")8@RequestMapping (value = "/addadmin", method =requestmethod.get)9 PublicAdmin addadmin (@RequestParam ("name") String name,Ten@RequestParam ("Sex") Integer sex, One@RequestParam ("Address") (String address) { AAdmin admin =NewAdmin (); - admin.setname (name); - admin.setsex (sex); the admin.setaddress (address); - returnadminrepository.save (admin); - } - +@ApiOperation ("Get all admin") -@RequestMapping (value = "/getalladmin", method =requestmethod.get) + PublicList<admin>getalladmin () { A returnAdminrepository.findall (); at } - -@ApiOperation ("Complex admin query") -@RequestMapping (value = "/getadminbynameandsexoraddress", method =requestmethod.get) - PublicAdmin getadminbynameandsexoraddress (@RequestParam ("name") String name, -@RequestParam (value= "Sex", required=false) Integer sex, in@RequestParam (value= "Address", required=false) (String address) { - /** to * OR + */ -Basicdblist orlist =NewBasicdblist ();//used to record the if(Sex! =NULL) { *Orlist.add (NewBasicdbobject ("Sex", Sex)); $ }Panax Notoginseng if(Stringutils.isnotblank (address)) { -Orlist.add (NewBasicdbobject ("Address", address)); the } +Basicdbobject Ordbobject =NewBasicdbobject ("$or", orlist); A the /** + * and - */ $Basicdblist andlist =Newbasicdblist (); $Andlist.add (NewBasicdbobject ("name", name)); - Andlist.add (ordbobject); -Basicdbobject Anddbobject =NewBasicdbobject ("$and", andlist); the - returnMongotemplate.findone (NewBasicquery (Anddbobject), Admin.class);Wuyi the}
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.)
Test:
Start MONGO, launch the app, open swagger, access.
Reference:
http://blog.csdn.net/congcong68/article/details/47183209
12th Chapter Springboot + MONGODB (complex query)