你的位置:首页 > Java教程

[Java教程]第十二章 springboot + mongodb(复杂查询)


  • 简单查询:使用自定义的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

注意:

  • @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      * 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