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. mysql find_in_set函数详解

    Mysql函数FIND_IN_SET()的使用方法 有了FIND_IN_SET这个函数.我们可以设计一个如:一只手机即是智能机,又是Andriod系统的. 比如:有个产品表里有一个type字段,他存储 ...

  2. Mendeley文献管理软件使用介绍

    <!DOCTYPE html> New Document /* GitHub stylesheet for MarkdownPad (http://markdownpad.com) / / ...

  3. 关于table边框,设置了border-collapse:collapse之后,设置border-radius没效果

    做项目遇到边框需要设置圆角,然后发现在设置了border-collapse:collapse之后,border-radius:10px不起作用了,发现这个是css本身的问题,两者不能混在一起使用. 代 ...

  4. 论 异常处理机制中的return关键字

    Java中,执行try-catch-finally语句需要注意: 第一:return语句并不是函数的最终出口,如果有finally语句,这在return之后还会执行finally(return的值会暂 ...

  5. Python监控目录和文件变化

    一.os.listdir import os, time path_to_watch = "." before = dict ([(f, None) for f in os.lis ...

  6. gdg shell

    export TIMESTAMP=`date +%Y%m%d_%H%M%S`GDGFILE=file1_${TIMESTAMP}.txtsuffix=${GDGFILE#*_}prefix=${suf ...

  7. 下拉框搜索插件chosen

    {% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta ...

  8. hdu 3338 最大流 ****

    题意: 黑格子右上代表该行的和,左下代表该列下的和 链接:点我 这题可以用网络流做.以空白格为节点,假设流是从左流入,从上流出的,流入的容量为行和,流出来容量为列和,其余容量不变.求满足的最大流.由于 ...

  9. 2016 UESTC Training for Data Structures 题解

    题解在下已经写过一次了,所以就不再写了,下面只有代码 题解下载(1):http://pan.baidu.com/s/1hsAUjMs 题解下载(2):http://pan.baidu.com/s/1m ...

  10. Oil Deposits 搜索 bfs 强联通

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...