本文基于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基本操作的更多相关文章

  1. Key/Value之王Memcached初探:二、Memcached在.Net中的基本操作

    一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅仅memcached网站上列出的语言就有: ...

  2. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  3. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  4. 三、Redis基本操作——List

    小喵的唠叨话:前面我们介绍了Redis的string的数据结构的原理和操作.当时我们提到Redis的键值对不仅仅是字符串.而这次我们就要介绍Redis的第二个数据结构了,List(链表).由于List ...

  5. 二、Redis基本操作——String(实战篇)

    小喵万万没想到,上一篇博客,居然已经被阅读600次了!!!让小喵感觉压力颇大.万一有写错的地方,岂不是会误导很多筒子们.所以,恳请大家,如果看到小喵的博客有什么不对的地方,请尽快指正!谢谢! 小喵的唠 ...

  6. 一、Redis基本操作——String(原理篇)

    小喵的唠叨话:最近京东图书大减价,小喵手痒了就买了本<Redis设计与实现>[1]来看看.这里权当小喵看书的笔记啦.这一系列的模式,主要是先介绍Redis的实现原理(可能很大一部分会直接照 ...

  7. Linq查询基本操作

    摘要:本文介绍Linq查询基本操作(查询关键字) - from 子句 - where 子句 - select子句 - group 子句 - into 子句 - orderby 子句 - join 子句 ...

  8. C++ map的基本操作和使用

    原文地址:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可 ...

  9. python之最强王者(10)———文件(File)、输入输出的基本操作

    1. Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 2.打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式. ...

随机推荐

  1. 黄包车比赛 python学习

    将性别进行编码: https://github.com/Bifzivkar/Boutique-Travel-Services-Predict/blob/master/feature/2_feature ...

  2. G - Ice_cream's world I (并查集)

    点击打开链接 ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_cream ...

  3. 【OCP-12c】2019年CUUG OCP 071考试题库(78题)

    78.View the exhibit and examine the structure of the CUSTOMERStable. Which two tasks would require s ...

  4. 【OCP-12c】CUUG 071题库考试原题及答案解析(13)

    13.(6-7) choose twoWhich two statements are true regarding operators used with subqueries? (Choose t ...

  5. “全栈2019”Java异常第六章:finally代码块作用域详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...

  6. acedSSSetFirst选择集夹点亮显实例

    ads_name ss; //执行预选 好像可以无视PICKSTYLE变量 if (RTNORM != acedSSGet(_T("I"),NULL,NULL,NULL,ss)) ...

  7. USB-Redirector-Technician 永久破解版(USB设备映射软件)

    USB-Redirector-Technician 这个软件对于搞安卓刷机的人想必非常熟悉,淘宝破解版售价:38 一个的东西 除了远程刷机,用于映射一些小型设备是没问题的,只要网跟得上~ USB-Re ...

  8. Elasticsearch基础知识

    ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口Elasticsearch是用Java开发的,并作为Apache ...

  9. 基于.NET的开源搜索引擎-DotLucene(2)

    NLucene是将 Lucene 从 Java 移植到 .NET 的一个 SourceForge 项目,它从 Lucene 1.2 版本转化而来. Lucene.Net因为 NLucene 项目到20 ...

  10. scrapy实战1,基础知识回顾和虚拟环境准备

        视频地址 https://coding.imooc.com/learn/list/92.html   一. 基础知识回顾     1. 正则表达式 1)贪婪匹配,非贪婪匹配 .*? 非贪婪 . ...