码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2017-07-13 14:19:58      阅读:2957      评论:0      收藏:0      [点我收藏+]

标签: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

说明:

  • 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(复杂查询)

标签:base   min   value   val   opened   结果   mongod   import   isp   

原文地址:http://www.cnblogs.com/sunny3096/p/7160053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!