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

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

【第十二章】 springboot + mongodb(复杂查询)的更多相关文章

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

    简单查询:使用自定义的XxxRepository接口即可.(见 第十一章 springboot + mongodb(简单查询)) 复杂查询:使用MongoTemplate以及一些查询条件构建类(Bas ...

  2. sql 入门经典(第五版) Ryan Stephens 学习笔记 (第六,七,八,九,十章,十一章,十二章)

    第六章: 管理数据库事务 事务 是 由第五章 数据操作语言完成的  DML ,是对数据库锁做的一个操作或者修改. 所有事务都有开始和结束 事务可以被保存和撤销 如果事务在中途失败,事务中的任何部分都不 ...

  3. 《Linux命令行与shell脚本编程大全》 第二十二章 学习笔记

    第二十二章:使用其他shell 什么是dash shell Debian的dash shell是ash shell的直系后代,ash shell是Unix系统上原来地Bourne shell的简化版本 ...

  4. 第十二章——SQLServer统计信息(4)——在过滤索引上的统计信息

    原文:第十二章--SQLServer统计信息(4)--在过滤索引上的统计信息 前言: 从2008开始,引入了一个增强非聚集索引的新功能--过滤索引(filter index),可以使用带有where条 ...

  5. 第十二章——SQLServer统计信息(1)——创建和更新统计信息

    原文:第十二章--SQLServer统计信息(1)--创建和更新统计信息 简介: 查询的统计信息: 目前为止,已经介绍了选择索引.维护索引.如果有合适的索引并实时更新统计信息,那么优化器会选择有用的索 ...

  6. 第十二章——SQLServer统计信息(2)——非索引键上统计信息的影响

    原文:第十二章--SQLServer统计信息(2)--非索引键上统计信息的影响 前言: 索引对性能方面总是扮演着一个重要的角色,实际上,查询优化器首先检查谓词上的统计信息,然后才决定用什么索引.一般情 ...

  7. JavaScript DOM编程艺术-学习笔记(第十二章)

    第十二章 1.本章是综合前面章节的所有东西的,一个综合实例 2.流程:①项目简介:a.获取原始资料(包括文本.图片.音视频等) b.站点结构(文件目录结构) c.页面(文件)结构 ②设计(切图) ③c ...

  8. 《Django By Example》第十二章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:第十二章,全书最后一章,终于到这章 ...

  9. 第二十五章 springboot + hystrixdashboard

    注意: hystrix基本使用:第十九章 springboot + hystrix(1) hystrix计数原理:附6 hystrix metrics and monitor 一.hystrixdas ...

随机推荐

  1. sass的@at-root

    一.首先理解sass的嵌套中 &表示是什么? &表示整个选择器,而不单个class属性值或id属性值或tagName.例如下面一段代码: .a { .b { & { color ...

  2. 几种outofmemory

    几种outofmemory的解决方法:1.  java.lang.OutOfMemoryError: PermGen space PermGen space的全称是Permanent Generati ...

  3. python 基础 内置函数

    内置参数 print(all([5,-1,5])) # 非0都是真 true print(all([0,-1,5])) # false print(any([1,0,5])) # 有一个数据为真,就为 ...

  4. 解决PHPStorm经常卡顿现象 调整内存限制

    https://www.jisec.com/other/451.html 为什么调整内存? 最近发现PHPstorm在打开一些大点的js, html文件时,会非常的卡顿,这个主要的原因是因为设置的内存 ...

  5. oj2894(贝尔曼福特模板)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2894 就因为粗心,一中午没A,题目说是2000 ...

  6. yum && 编译 安装mysql 5.7 多实例

    yum安装 [root@localhost ~]# wget http://repo.mysql.com/mysql57-community-release-el7.rpm [root@localho ...

  7. html的img标签 强大的title

    示例: <img src="smiley-2.gif" alt="Smiley face" width="42" height=&qu ...

  8. ML实践详细经典教程----用例图、顺序图、状态图、类图、包图、协作图

    面向对象的问题的处理的关键是建模问题.建模可以把在复杂世界的许多重要的细节给抽象出.许多建模工具封装了UML(也就是Unified Modeling Language?),这篇课程的目的是展示出UML ...

  9. Hive错误:Unable to load native-hadoop library for your platform

    WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin- ...

  10. jmeter Bean Shell的使用(一)

    未经作者允许,禁止转载!!! Jmeter有哪些Bean Shell 定时器: BeanShell Timer 前置处理器:BeanShell PreProcessor 采样器: BeanShell ...