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

时间:2024-05-04 09:03:19

1、application.properties

 #mongodb note:mongo3.x will not use host and port,only use uri
#spring.data.mongodb.host=192.168.22.110
#spring.data.mongodb.port=27017
#spring.data.mongodb.database=myfirstMongodb
spring.data.mongodb.uri=mongodb://192.168.22.110:27017/myfirstMongodb

说明:

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

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;
} }

注意:

  • @Id必须有

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); }

说明:

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

注意:mongodb的查询字段必须是小写

测试:

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

参考:

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