spring boot 1.5.2 操作mongodb3.4.0
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的更多相关文章
- Spring Boot(二):数据库操作
本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是JdbcTemplate,第二种是JPA,第三种是Mybatis.之前已经提到过,本系列会以一个博客系统 ...
- Spring Boot修改Thymeleaf版本(从Thymeleaf2.0到3.0)
Spring Boot默认选择的Thymeleaf是2.0版本的,那么如果我们就想要使用3.0版本或者说指定版本呢,那么怎么操作呢?在这里要说明下 3.0的配置在spring boot 1.4.0+才 ...
- spring boot +mybatis 整合 连接数据库测试(从0到1)
spring boot 整合mybatis 1.打开idea创建一个项目 2.在弹出的窗口中选择spring initializr(初始化项目),点击next 3.接下来填写group 与artifa ...
- Spring Boot实战之数据库操作
上篇文章中已经通过一个简单的HelloWorld程序讲解了Spring boot的基本原理和使用.本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是Jdb ...
- 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 ...
- spring boot通过Jedis来操作redis
idea中新建spring boot项目,引入jedis依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> ...
- 使用spring boot中的JPA操作数据库
前言 Spring boot中的JPA 使用的同学都会感觉到他的强大,简直就是神器一般,通俗的说,根本不需要你写sql,这就帮你节省了很多时间,那么下面我们来一起来体验下这款神器吧. 一.在pom中添 ...
- Spring Boot 使用 Dom4j XStream 操作 Xml
Xml 现在仍然占据着比较重要的地位,比如微信接口中使用了 Xml 进行消息的定义.本章重点讨论 Xml 的新建.编辑.查找.转化,可以这么理解,本章是使用了 dom4j.xstream 也是在开发者 ...
- 阿里云服务器 配置 tomcat 发布spring boot项目 的具体操作 【使用公网ip】
1.前言 spring boot 转成war包 后用tomcat发布的具体操作在我另一篇随笔有详细记载,不论是window系统还是Linux系统,tomcat的发布配置都是一样的,所以这里不具体讲这个 ...
随机推荐
- Aptana studio 3配色方案的修改方法
http://www.weste.net/2013/1-29/88614.html Aptana studio 3的确是一个不错的IDE,但是默认的黑底白色代码样式看时间长了有点审美疲劳了,如何能够更 ...
- sqlite - Sqlite Wrappers - Delphi
http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers Aducom's SQLite: Open source (NewBSD) Delphi (4. ...
- Effective JavaScript Item 35 使用闭包来保存私有数据
本系列作为EffectiveJavaScript的读书笔记. JavaScript的对象系统从其语法上而言并不鼓舞使用信息隐藏(Information Hiding).由于当使用诸如this.name ...
- STM32学习笔记3-IO配置输入输出
STM32的IO配置时没什么特殊的,有个注意点就是有用IO前须要先打开其时钟线,下面是验证过oK的程序: RCC->APB2ENR|=GpioBApb2enrEn; //使能PORTB时钟 GP ...
- How to install WP 8.0 SDK if WP 8.1 SDK is installed?
I have the Windows Phone 8.1 SDK on my dev pc installed together with VS 2013. I need to open some W ...
- win8.1快速启动选项突然消失了怎么办?
win8开始提供的快速启动功能是一种混合式的休眠模式,Windows系统 在关机时将系统的信息保存到硬盘上的一个文件中来实现下一次的快速启动.当再次启动电脑时, Windows 使用该系统信息文件来恢 ...
- 用最简单的例子理解复合模式(Composite Pattern)
在显示树形结构时,复合模式有很好的体现.本篇显示如下部门结构: 以上,有的节点包含子节点,有的节点部包含子节点.不管是什么节点,每个节点就代表一个部门. 首先设计一个关于部门的抽象基类. public ...
- 几款开源的ETL工具及ELT初探
ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract).转换(transform).加载(load)至目的端的过程.ETL 是构建数据仓 ...
- 基于CXF框架下的SOAP Webservice服务端接口开发
最近对webservice 进行入门学习,网上也是找了很多的学习资料.总得感觉就是这了解点,那了解点.感觉不够系统,不够容易入门.差不多断断续续看了一个星期了,今天小有成果,把客户端,服务端都搞定了. ...
- 原创D3D几何实例化的DEMO
CUBE的几何实例化DEMO 鼠标右键按下并拖动 旋转视角WSAD 前后左右RF ...