【第十二章】 springboot + mongodb(复杂查询)
- 简单查询:使用自定义的XxxRepository接口即可。(见 第十一章 springboot + mongodb(简单查询))
- 复杂查询:使用MongoTemplate以及一些查询条件构建类(BasicDBList、BasicDBObject、Criteria等)
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(复杂查询)的更多相关文章
- 第十二章 springboot + mongodb(复杂查询)
简单查询:使用自定义的XxxRepository接口即可.(见 第十一章 springboot + mongodb(简单查询)) 复杂查询:使用MongoTemplate以及一些查询条件构建类(Bas ...
- sql 入门经典(第五版) Ryan Stephens 学习笔记 (第六,七,八,九,十章,十一章,十二章)
第六章: 管理数据库事务 事务 是 由第五章 数据操作语言完成的 DML ,是对数据库锁做的一个操作或者修改. 所有事务都有开始和结束 事务可以被保存和撤销 如果事务在中途失败,事务中的任何部分都不 ...
- 《Linux命令行与shell脚本编程大全》 第二十二章 学习笔记
第二十二章:使用其他shell 什么是dash shell Debian的dash shell是ash shell的直系后代,ash shell是Unix系统上原来地Bourne shell的简化版本 ...
- 第十二章——SQLServer统计信息(4)——在过滤索引上的统计信息
原文:第十二章--SQLServer统计信息(4)--在过滤索引上的统计信息 前言: 从2008开始,引入了一个增强非聚集索引的新功能--过滤索引(filter index),可以使用带有where条 ...
- 第十二章——SQLServer统计信息(1)——创建和更新统计信息
原文:第十二章--SQLServer统计信息(1)--创建和更新统计信息 简介: 查询的统计信息: 目前为止,已经介绍了选择索引.维护索引.如果有合适的索引并实时更新统计信息,那么优化器会选择有用的索 ...
- 第十二章——SQLServer统计信息(2)——非索引键上统计信息的影响
原文:第十二章--SQLServer统计信息(2)--非索引键上统计信息的影响 前言: 索引对性能方面总是扮演着一个重要的角色,实际上,查询优化器首先检查谓词上的统计信息,然后才决定用什么索引.一般情 ...
- JavaScript DOM编程艺术-学习笔记(第十二章)
第十二章 1.本章是综合前面章节的所有东西的,一个综合实例 2.流程:①项目简介:a.获取原始资料(包括文本.图片.音视频等) b.站点结构(文件目录结构) c.页面(文件)结构 ②设计(切图) ③c ...
- 《Django By Example》第十二章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:第十二章,全书最后一章,终于到这章 ...
- 第二十五章 springboot + hystrixdashboard
注意: hystrix基本使用:第十九章 springboot + hystrix(1) hystrix计数原理:附6 hystrix metrics and monitor 一.hystrixdas ...
随机推荐
- ovs的主要代码函数及大体结构图
作者: 北京-小武 邮箱:night_elf1020@163.com 新浪微博:北京-小武 最终抽时间把openvswitch的2.0代码看的思路汇总了下,因为是曾经好早下载的.看完了也才看到2.1依 ...
- 001-window版redis安装
一.参考地址 官方地址:https://redis.io/ windows版本[学习使用]:https://github.com/MicrosoftArchive/redis 二.windows版re ...
- js-jquery-SweetAlert【二】配置方法
一.配置 Argument Default value 含义 Description title null (required) 模态对话框的标题.它可以在参数对象的title参数中设置,也可以在 ...
- python3中pymysql模块安装及连接数据库(同‘python安装HTMLTestRunner’)
https://pypi.org/project/PyMySQL/#files 安装完成之后就是连接数据库了 机器上安装了mysql的数据库,并且已经创建了两张表用于测试 python3连接数据库及删 ...
- 梯度消失与梯度爆炸 ==> 如何选择随机初始权重
梯度消失与梯度爆炸 当训练神经网络时,导数或坡度有时会变得非常大或非常小,甚至以指数方式变小,这加大了训练的难度 这里忽略了常数项b.为了让z不会过大或者过小,思路是让w与n有关,且n越大,w应该越小 ...
- matplotlib —— 调整坐标轴
import matplotlib.pyplot as plt import numpy as np # 绘制普通图像 x = np.linspace(-1, 1, 50) y1 = 2 * x + ...
- Python qq企业邮箱发送邮件
Python qq企业邮箱发送邮件 进入客户端设置: 下面是代码部分: from email.header import Header from email.mime.text import MIME ...
- C#可扩展数组转变为String[]数组
简单备忘: 由于需要将数据最终以逗号隔开来拼接,因而写了下面的处理方法. public void GetJoinString() { ArrayList arr = new ArrayList(); ...
- Leetcode: Pow(x, n) and Summary: 负数补码总结
Implement pow(x, n). Analysis: Time Complexity: O(LogN) Iterative code: refer to https://discuss.le ...
- 问题排查之'org.apache.rocketmq.spring.starter.core.RocketMQTemplate' that could not be found.- Bean method 'rocketMQTemplate' in 'RocketMQAutoConfiguration' not loaded.
背景 今天将一个SpringBoot项目的配置参数从原有的.yml文件迁移到Apollo后,启动报错“Bean method 'rocketMQTemplate' in 'RocketMQAutoCo ...