標籤:
- 簡單查詢:使用自訂的XxxRepository介面即可。(見 第十一章 springboot + mongodb(簡單查詢))
- 複雜查詢:使用MongoTemplate以及一些查詢條件構建類(BasicDBList、BasicDBObject、Criteria等)
1、application.properties
1 #mongodb note:mongo3.x will not use host and port,only use uri2 #spring.data.mongodb.host=192.168.22.1103 #spring.data.mongodb.port=270174 #spring.data.mongodb.database=myfirstMongodb5 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 @Id10 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
注意:
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 * OR32 */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 * and44 */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(複雜查詢)