码迷,mamicode.com
首页 > 数据库 > 详细

第十二章 springboot + mongodb(复杂查询)

时间:2016-05-04 10:25:29      阅读:1213      评论:0      收藏:0      [点我收藏+]

标签:

1、application.properties

技术分享
1 #mongodb note:mongo3.x will not use 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

说明:

  • mongo2.x支持以上两种配置方式
  • mongo3.x仅支持uri方式

2、Admin

技术分享
 1 package com.xxx.firstboot.domain;
 2 
 3 import org.springframework.data.annotation.Id;
 4 
 5 /**
 6  * 测试复杂的mongo查询
 7  */
 8 public class Admin {
 9     @Id
10     private String adminId;
11     private String name;
12     private Integer sex;
13     private String address;
14 
15     public String getAdminId() {
16         return adminId;
17     }
18 
19     public void setAdminId(String adminId) {
20         this.adminId = adminId;
21     }
22 
23     public String getName() {
24         return name;
25     }
26 
27     public void setName(String name) {
28         this.name = name;
29     }
30 
31     public Integer getSex() {
32         return sex;
33     }
34 
35     public void setSex(Integer sex) {
36         this.sex = sex;
37     }
38 
39     public String getAddress() {
40         return address;
41     }
42 
43     public void setAddress(String address) {
44         this.address = address;
45     }
46 
47 }
View Code

注意:

  • @Id必须有

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 AdminRepository extends MongoRepository<Admin, String> {
8 }
View Code

说明:该接口用于简单查询。这里是一个空接口,具有CRUD功能。

4、CustomerController

技术分享
 1 /*********************测试复杂的mongo查询**********************/
 2     @Autowired
 3     private AdminRepository adminRepository;
 4     @Autowired
 5     private MongoTemplate mongoTemplate;
 6     
 7     @ApiOperation("增加一个Admin")
 8     @RequestMapping(value = "/addAdmin", method = RequestMethod.GET)
 9     public Admin addAdmin(@RequestParam("name") String name,
10                           @RequestParam("sex") Integer sex,
11                           @RequestParam("address") String address) {
12         Admin admin = new Admin();
13         admin.setName(name);
14         admin.setSex(sex);
15         admin.setAddress(address);
16         return adminRepository.save(admin);
17     }
18     
19     @ApiOperation("获取所有的Admin")
20     @RequestMapping(value = "/getAllAdmin", method = RequestMethod.GET)
21     public List<Admin> getAllAdmin() {
22         return adminRepository.findAll();
23     }
24     
25     @ApiOperation("复杂的admin查询")
26     @RequestMapping(value = "/getAdminByNameAndSexOrAddress", method = RequestMethod.GET)
27     public Admin getAdminByNameAndSexOrAddress(@RequestParam("name") String name,
28                                                  @RequestParam(value="sex",required=false) Integer sex,
29                                                  @RequestParam(value="address",required=false) String address) {
30         /**
31          * OR
32          */
33         BasicDBList orList = new BasicDBList(); //用于记录
34         if (sex != null) {
35             orList.add(new BasicDBObject("sex", sex));
36         }
37         if (StringUtils.isNotBlank(address)) {
38             orList.add(new BasicDBObject("address", address));
39         }
40         BasicDBObject orDBObject = new BasicDBObject("$or", orList);
41         
42         /**
43          * and
44          */
45         BasicDBList andList = new BasicDBList();
46         andList.add(new BasicDBObject("name", name));
47         andList.add(orDBObject);
48         BasicDBObject andDBObject = new BasicDBObject("$and", andList);
49         
50         return mongoTemplate.findOne(new BasicQuery(andDBObject), Admin.class);
51 
52     }
View Code

说明:

  • getAdminByNameAndSexOrAddress要实现select * from admin where name = ? and (sex = ? or address = ?)
  • 通过BasicDBList、BasicDBObject构建查询参数
  • findOne返回第一个符合条件的结果、find返回符合条件的结果列表
  • 以上查询的collection是admin(VO的简单类名),也可以指定从某一个collection中查询(查看find、findOne等方法)

测试:

启动mongo,启动应用,打开swagger,访问即可。

 

参考:

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

第十二章 springboot + mongodb(复杂查询)

标签:

原文地址:http://www.cnblogs.com/java-zhao/p/5457333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!