1:build.gradle 添加mongodb依赖

dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.4.2' testCompile('org.springframework.boot:spring-boot-starter-test')
}

2:启动本地mongdb服务

D:\MongoDB\Server\3.4\bin>mongod --config "D:\MongoDB\Server\3.4\mongo.conf" --auth

3:配置application.properties文件

#mongodb config
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=
spring.data.mongodb.username=gwzh
spring.data.mongodb.password=gwzh
spring.data.mongodb.database=gwzh
spring.data.mongodb.authentication-database=gwzh #server config
server.port=

4:代码

(1)UserVo.java

package com.example.user;

import org.springframework.data.annotation.Id;

/**
* Created by yan on 2017/4/25.
*/
public class UserVo {
@Id
private String userid;
private String username;
private Integer age; public String getUserid() {
return userid;
} public void setUserid(String userid) {
this.userid = userid;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}

(2)UserRepository.java

package com.example.user;

import org.springframework.data.mongodb.repository.MongoRepository;

/**
* Created by yan on 2017/4/25.
*/
public interface UserRepository extends MongoRepository<UserVo,String> { UserVo findByUserid(String userid);
}

(3)UserCtrl.java

package com.example.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import java.util.List; /**
* Created by yan on 2017/4/25.
*/
@RestController
@RequestMapping(
value = "/user",
produces = "application/json;charset=utf-8",
headers = "Accept=application/json"
)
public class UserCtrl { @Autowired
UserRepository userRepository; @RequestMapping(
value = "/add",
method = RequestMethod.POST
)
public UserVo addUser(@RequestBody UserVo vo){
return userRepository.save(vo);
} @RequestMapping(
value = "/list",
method = RequestMethod.GET
)
public List<UserVo> getUsers(){
return userRepository.findAll();
} @RequestMapping(
value = "/delete",
method = RequestMethod.POST
)
public boolean deleteUser(@RequestBody UserVo vo){
userRepository.delete(vo.getUserid());
return true;
}
}

5:测试

spring boot 1.5.2 操作mongodb3.4.0的更多相关文章

  1. Spring Boot(二):数据库操作

    本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是JdbcTemplate,第二种是JPA,第三种是Mybatis.之前已经提到过,本系列会以一个博客系统 ...

  2. Spring Boot修改Thymeleaf版本(从Thymeleaf2.0到3.0)

    Spring Boot默认选择的Thymeleaf是2.0版本的,那么如果我们就想要使用3.0版本或者说指定版本呢,那么怎么操作呢?在这里要说明下 3.0的配置在spring boot 1.4.0+才 ...

  3. spring boot +mybatis 整合 连接数据库测试(从0到1)

    spring boot 整合mybatis 1.打开idea创建一个项目 2.在弹出的窗口中选择spring initializr(初始化项目),点击next 3.接下来填写group 与artifa ...

  4. Spring Boot实战之数据库操作

    上篇文章中已经通过一个简单的HelloWorld程序讲解了Spring boot的基本原理和使用.本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是Jdb ...

  5. spring boot ----> jpa连接和操作mysql数据库

    环境: centos6.8,jdk1.8.0_172,maven3.5.4,vim,spring boot 1.5.13,mysql-5.7.23 1.引入jpa起步依赖和mysql驱动jar包 &l ...

  6. spring boot通过Jedis来操作redis

    idea中新建spring boot项目,引入jedis依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> ...

  7. 使用spring boot中的JPA操作数据库

    前言 Spring boot中的JPA 使用的同学都会感觉到他的强大,简直就是神器一般,通俗的说,根本不需要你写sql,这就帮你节省了很多时间,那么下面我们来一起来体验下这款神器吧. 一.在pom中添 ...

  8. Spring Boot 使用 Dom4j XStream 操作 Xml

    Xml 现在仍然占据着比较重要的地位,比如微信接口中使用了 Xml 进行消息的定义.本章重点讨论 Xml 的新建.编辑.查找.转化,可以这么理解,本章是使用了 dom4j.xstream 也是在开发者 ...

  9. 阿里云服务器 配置 tomcat 发布spring boot项目 的具体操作 【使用公网ip】

    1.前言 spring boot 转成war包 后用tomcat发布的具体操作在我另一篇随笔有详细记载,不论是window系统还是Linux系统,tomcat的发布配置都是一样的,所以这里不具体讲这个 ...

随机推荐

  1. Codeforces Round #279 (Div. 2) C. Hacking Cypher 机智的前缀和处理

    #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...

  2. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  3. DEX 可视化查阅

    参考: http://bbs.pediy.com/thread-208828.htm 010 Editor 下载地址: http://www.sweetscape.com/download/ //-- ...

  4. spring---transaction(3)---源代码分析(事务的管理器PlatformTransactionManager)

    写在前面 由于实现事务功能的方式各不相同,Spring进行了统一的抽象,形成了PlatformTransactionManager事务管理器顶级接口(平台事务管理器),事务的提交.回滚等操作全部交给它 ...

  5. maven中跳过单元测试(转)

    你可能想要配置 Maven 使其完全跳过单元测试. 可能你有一个很大的系统,单元测试需要花好多分钟来完成,而你不想在生成最终输出前等单元测试完成. 你可能正工作在一个遗留系统上面,这个系统有一系列的失 ...

  6. JS实现《黑客帝国》落地字母背景

    JS实现<黑客帝国>落地字母背景.这个特别有意思,主要是通过设置字符相关属性.控制循环字母距离顶部的高度值,来达到字母不断循环下落的功能. 恩,还有加上一个随机机制,出现各种大小 各个位置 ...

  7. SCSI Pass-Through Interface Tool

    http://code.msdn.microsoft.com/SCSI-Pass-Through-a906ceef/sourcecode?fileId=59048&pathId=1919073 ...

  8. Vue 组件 data为什么是函数?

    在创建或注册模板的时候,传入一个data属性作为用来绑定的数据.但是在组件中,data必须是一个函数,而不能直接把一个对象赋值给它. Vue.component('my-component', { t ...

  9. 使用IP访问Mantis显示空白页的解决办法

    使用http://localhost/mantis/ 可成功访问Mantis,但使用IP地址:http://172.16.20.111/Mantis却访不了,显示“无法显示网页”. 在aphache中 ...

  10. (a*b)%c 小的技巧

    (a*b)%c这个问题看上去好简单啊. 当然我们不是来说这么简单的问题了.你想一想,我们会不会遇到这种情况,a是__int64 ,b也是__int64 当两个数足够大的时候我们直接相乘的就会出现__i ...