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. 一步一步写数据结构(BST-二叉排序树)

    二叉排序树的重要性不用多说,下面用c++实现二叉排序树的建立,插入,查找,修改,和删除.难点在于删除,其他几个相对比较简单. 以下是代码: #include<iostream> using ...

  2. [leetcode tree]102. Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  3. django使用admin

    1.在应用下的admin.py添加 #!/usr/bin/python # coding:utf-8 from django.contrib import admin from .models imp ...

  4. luoguP1659 [国际集训队]拉拉队排练 manacher算法

    直接统计长度为$i$的回文子串有多少个 然后倒叙枚举长度,快速幂统计一下即可 复杂度$O(n \log n)$ #include <cstdio> #include <cstring ...

  5. [UOJ240]aliens

    学习了一下凸优化DP,感觉挺有意思的 首先把所有点对称到左下角,然后以每个点为顶点画等腰直角三角形,将被覆盖的点去掉,现在所有点从左上到右下横纵坐标都是递增的,设坐标为$(x_{1\cdots M}, ...

  6. windows提权的几种姿势

    想象这种画面:你拿到了一台机器上Meterpreter会话了,然后你准备运行 getsystem 命令进行提权,但如果提权没有成功,你就准备认输了吗?只有懦夫才会认输.但是你不是,对吗?你是一个勇者! ...

  7. python开发_thread_线程基础

    说到线程,我们要知道啥是串行,啥是并行程序 举个例子: 串行程序,就是一个一个的执行程序 #python threading import time ''' 每一秒中,输出:this is a dem ...

  8. Codechef December Challenge 2014 Chef and Apple Trees 水题

    Chef and Apple Trees Chef loves to prepare delicious dishes. This time, Chef has decided to prepare ...

  9. 文件上传demo

    前端代码: <form action="upload.php" enctype="multipart/form-data" method="po ...

  10. thrift 安装 make 失败 ar: .libs/ThriftTest_constants.o: No such file or directory

    $wget http://mirrors.cnnic.cn/apache/thrift/0.9.1/thrift-0.9.1.tar.gz $tar zxvf thrift-0.9.1.tar.gz ...