MongoTemplate基本操作
本文基于spring boot项目,快速构建项目请参考:https://www.cnblogs.com/lay2017/p/8836273.html
添加mongo依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
配置连接
有密码
spring.data.mongodb.uri=mongodb://name:password@localhost:27017/test
无密码
spring.data.mongodb.uri=mongodb://localhost:27017/test
代码示例
实体类
package cn.lay.mongo.entity;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author lay
* @date 2018/09/13 14:53
*/
@Document(collection = "user_collection")
public class User {
private Long id;
private String name;
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
service类
package cn.lay.mongo.service;
import cn.lay.mongo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author lay
* @date 2018/09/13 14:54
*/
@Service
public class UserService {
@Autowired
private MongoTemplate mt;
public void saveUser(User user) {
mt.save(user);
}
public void removeUser(User user) {
Query query = new Query(Criteria.where("id").is(user.getId()));
mt.remove(query, User.class);
}
public void updateUser(User user) {
Query query = new Query(Criteria.where("id").is(user.getId()));
Update update = new Update().set("name", "gary");
mt.updateMulti(query, update, User.class);
}
public User select(User user) {
Query query = new Query(Criteria.where("id").is(user.getId()));
User user1 = mt.findOne(query, User.class);
return user1;
}
public List<User> selectAll() {
List<User> users = mt.findAll(User.class);
return users;
}
}
controller
package cn.lay.mongo.controller;
import cn.lay.mongo.entity.User;
import cn.lay.mongo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author lay
* @date 2018/09/13 14:45
*/
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("get")
public User getUser() {
User user = new User();
user.setId(1L);
return userService.select(user);
}
@GetMapping("save")
public void saveUser() {
User user = new User();
user.setId(1L);
user.setName("marry");
user.setAge(24);
userService.saveUser(user);
}
@GetMapping(value = "remove")
public void removeUser() {
User user = new User();
user.setId(1L);
user.setName("lay");
user.setAge(24);
userService.removeUser(user);
}
@GetMapping(value = "update")
public void updateUser() {
User user = new User();
user.setId(1L);
user.setName("lay");
user.setAge(24);
userService.updateUser(user);
}
@GetMapping("list")
public List<User> listUser() {
return userService.selectAll();
}
}
MongoTemplate基本操作的更多相关文章
- Key/Value之王Memcached初探:二、Memcached在.Net中的基本操作
一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅仅memcached网站上列出的语言就有: ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- 三、Redis基本操作——List
小喵的唠叨话:前面我们介绍了Redis的string的数据结构的原理和操作.当时我们提到Redis的键值对不仅仅是字符串.而这次我们就要介绍Redis的第二个数据结构了,List(链表).由于List ...
- 二、Redis基本操作——String(实战篇)
小喵万万没想到,上一篇博客,居然已经被阅读600次了!!!让小喵感觉压力颇大.万一有写错的地方,岂不是会误导很多筒子们.所以,恳请大家,如果看到小喵的博客有什么不对的地方,请尽快指正!谢谢! 小喵的唠 ...
- 一、Redis基本操作——String(原理篇)
小喵的唠叨话:最近京东图书大减价,小喵手痒了就买了本<Redis设计与实现>[1]来看看.这里权当小喵看书的笔记啦.这一系列的模式,主要是先介绍Redis的实现原理(可能很大一部分会直接照 ...
- Linq查询基本操作
摘要:本文介绍Linq查询基本操作(查询关键字) - from 子句 - where 子句 - select子句 - group 子句 - into 子句 - orderby 子句 - join 子句 ...
- C++ map的基本操作和使用
原文地址:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可 ...
- python之最强王者(10)———文件(File)、输入输出的基本操作
1. Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 2.打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式. ...
随机推荐
- Java50道经典习题-程序14 求日期
题目:输入某年某月某日,判断这一天是这一年的第几天?分析:(1)以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天 (2)特殊情况,闰年2月份的天数是29天,否则是28天 impo ...
- isset()、empty()、is_NULL()的区别
1,isset():变量不存在,或变量为null,返回false,否则返回true: 2,empty():变量不存在,或变量为null,返回true,另外"".0."0& ...
- Redis Sentinel初体验
自Redis增加Sentinel集群工具以来,本博主就从未尝试过使用该工具.最近在调研目前主流的Redis集群部署方案,所以详细地看了一遍官方对于Sentinel的介绍并在自己的台式机上完成了 ...
- leetcode-200-岛屿的个数(dfs找所有的连通分量)
题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...
- iOS 时区获取问题
时区缩写 UTC, CST, GMT, CEST 以及转换 UTC是协调世界时(Universal Time Coordinated)英文缩写,是由国际无线电咨询委员会规定和推荐,并由国际时间局(BI ...
- Xcode 工程文件“.xcodeproj”文件夹解析
项目.xcodeproj 文件夹底下一般有4个文件: project.pbxproj 文件 xcuserdata 文件夹 xcshareddata 文件夹 project.xcworkspace 文件 ...
- 编译原理(六)自底向上分析之LR分析法
自底向上分析之LR分析法 说明:以老师PPT为标准,借鉴部分教材内容,AlvinZH学习笔记. 基本概念 1. LR分析:从左到右扫描(L)自底向上进行规约(R),是规范规约,也即最右推导(规范推导) ...
- HTML02--引用样式、表格、列表、div布局
接上一篇“HTML01随笔” 1.使用样式: 内联样式:标签中使用style属性 内部样式:<head>使用<style type="text/css" ...
- (C/C++) Array 印出所有排列組合
#include <stdio.h> #include <stdlib.h> #define N 4 , , , }; void swap(int *a, int *b) { ...
- CentOS 7 安装程序介绍
(一).引导菜单 使用 UEFI 引导 Install CentOS Linux 7 选择此选项开始在你的计算机系统中使用图形安装程序安装 CentOS 7 Test this media & ...