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.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. [python selenium] 操作方法整理

    个人笔记,摘抄自虫师python selenum,仅供个人参考 1.安装: pip install selenium 下载webdriver # webdriver 下载并放置在python主目录 · ...

  2. 取得项目的 Git 仓库

    有两种取得 Git 项目仓库的方法.第一种是在现存的目录下,通过导入所有文件来创建新的 Git 仓库.第二种是从已有的 Git 仓库克隆出一个新的镜像仓库来. 在工作目录中初始化新仓库 要对现有的某个 ...

  3. IdentityServer4之JWT签名(RSA加密证书)及验签

    一.前言 在IdentityServer4中有两种令牌,一个是JWT和Reference Token,在IDS4中默认用的是JWT,那么这两者有什么区别呢? 二.JWT与Reference Token ...

  4. Django的orm中get和filter的不同

    Django的orm框架对于业务复杂度不是很高的应用来说还是不错的,写起来很方面,用起来也简单.对于新手来说查询操作中最长用的两个方法get和filter有时候一不注意就会犯下一些小错误.那么今天就来 ...

  5. Linux怎么开启ssh

    一.查看ssh开启状态 service ssh status 这是已经开启了的状态 二.如果没有开启  键入以下命令开启 service ssh start 三.开启后如果不能利用xshell远程访问 ...

  6. Codeforces Round #357 (Div. 2) D. Gifts by the List 水题

    D. Gifts by the List 题目连接: http://www.codeforces.com/contest/681/problem/D Description Sasha lives i ...

  7. UVALive 5971

    Problem J Permutation Counting Dexter considers a permutation of first N natural numbers good if it ...

  8. XMOJ 1133: 膜拜大牛 计算几何/两圆相交

    1133: 膜拜大牛 Time Limit: 1 Sec  Memory Limit: 131072KiBSubmit: 9619  Solved: 3287 题目连接 http://acm.xmu. ...

  9. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  10. Codeforces Round #293 (Div. 2) A. Vitaly and Strings

    A. Vitaly and Strings time limit per test 1 second memory limit per test 256 megabytes input standar ...