标签:
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
说明:
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 }
注意:
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 }
说明:该接口用于简单查询。这里是一个空接口,具有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 }
说明:
测试:
启动mongo,启动应用,打开swagger,访问即可。
参考:
http://blog.csdn.net/congcong68/article/details/47183209
第十二章 springboot + mongodb(复杂查询)
标签:
原文地址:http://www.cnblogs.com/java-zhao/p/5457333.html