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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第十二章 springboot + mongodb(复杂查询)相关的知识,希望对你有一定的参考价值。

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

说明:

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

}
View Code

注意:

  • @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);

    }
View Code

说明:

  • 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.csdn.net/congcong68/article/details/47183209

以上是关于第十二章 springboot + mongodb(复杂查询)的主要内容,如果未能解决你的问题,请参考以下文章

Linux系统:第十二章:AWS服务器X86架构安装配置Mysql与MongoDB

Linux系统:第十二章:AWS服务器X86架构安装配置Mysql与MongoDB

第十二章课下测试补交博客

第十二章 并发编程

Java(第十二章)

第十二章软件包管理