标签:base min value val opened 结果 mongod import isp
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
package com.xxx.firstboot.domain; import org.springframework.data.annotation.Id; /** * 测试复杂的mongo查询 */ public class Admin { @Id private String adminId; private String name; private Integer sex; private String address; public String getAdminId() { return adminId; } public void setAdminId(String adminId) { this.adminId = adminId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
注意:
3、AdminRepository
package com.xxx.firstboot.mongo; import org.springframework.data.mongodb.repository.MongoRepository; import com.xxx.firstboot.domain.Admin; public interface AdminRepository extends MongoRepository<Admin, String> { }
说明:该接口用于简单查询。这里是一个空接口,具有CRUD功能。
4、CustomerController
/*********************测试复杂的mongo查询**********************/ @Autowired private AdminRepository adminRepository; @Autowired private MongoTemplate mongoTemplate; @ApiOperation("增加一个Admin") @RequestMapping(value = "/addAdmin", method = RequestMethod.GET) public Admin addAdmin(@RequestParam("name") String name, @RequestParam("sex") Integer sex, @RequestParam("address") String address) { Admin admin = new Admin(); admin.setName(name); admin.setSex(sex); admin.setAddress(address); return adminRepository.save(admin); } @ApiOperation("获取所有的Admin") @RequestMapping(value = "/getAllAdmin", method = RequestMethod.GET) public List<Admin> getAllAdmin() { return adminRepository.findAll(); } @ApiOperation("复杂的admin查询") @RequestMapping(value = "/getAdminByNameAndSexOrAddress", method = RequestMethod.GET) public Admin getAdminByNameAndSexOrAddress(@RequestParam("name") String name, @RequestParam(value="sex",required=false) Integer sex, @RequestParam(value="address",required=false) String address) { /** * OR */ BasicDBList orList = new BasicDBList(); //用于记录 if (sex != null) { orList.add(new BasicDBObject("sex", sex)); } if (StringUtils.isNotBlank(address)) { orList.add(new BasicDBObject("address", address)); } BasicDBObject orDBObject = new BasicDBObject("$or", orList); /** * and */ BasicDBList andList = new BasicDBList(); andList.add(new BasicDBObject("name", name)); andList.add(orDBObject); BasicDBObject andDBObject = new BasicDBObject("$and", andList); return mongoTemplate.findOne(new BasicQuery(andDBObject), Admin.class); }
说明:
注意:mongodb的查询字段必须是小写。
测试:
启动mongo,启动应用,打开swagger,访问即可。
参考:
http://blog.csdn.net/congcong68/article/details/47183209
第十二章 springboot + mongodb(复杂查询)
标签:base min value val opened 结果 mongod import isp
原文地址:http://www.cnblogs.com/sunny3096/p/7160053.html