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的发布配置都是一样的,所以这里不具体讲这个 ...
随机推荐
- Set常用子类特点
HashSet: 重写 hashCode和equals方法 特点:无序,唯一 底层结构是: ...
- 你的C/C++程序为什么无法运行?揭秘Segmentation fault (1)
什么让你对C/C++如此恐惧? 晦涩的语法?还是优秀IDE的欠缺? 我想那都不是问题,最多的可能是一个类似这样的错误: 段错误(Segmentation fault) 这是新手无法避免的错误,也是老手 ...
- oracle开发学习篇之集合函数
集合函数; declare type list_nested ) not null; v_all list_nested := list_nested('changan','hubei','shang ...
- 在EntityFramework6中管理DbContext的正确方式——4DbContextScope:一个简单的,正确的并且灵活的管理DbContext实例的方式(外文翻译)
(译者注:使用EF开发应用程序的一个难点就在于对其DbContext的生命周期管理,你的管理策略是否能很好的支持上层服务 使用独立事务,使用嵌套事务,并行执行,异步执行等需求? Mehdi El Gu ...
- iOS 调用短信、电话、邮件、浏览器等
1.调用 自带mail[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@hzl ...
- Android 面试题(答案最全)
1. Android dvm的进程和Linux的进程, 应用程序的进程是否为同一个概念DVM指dalivk的虚拟机.每一个Android应用程序都在它自己的进程中运行,都拥有一个独立的Dalvik虚拟 ...
- go语言 defer 高级
go语言defer语句的用法 defer的语法 defer后面必须是函数调用语句,不能是其他语句,否则编译器会出错. package main import "log" func ...
- Top N的MapReduce程序MapReduce for Top N items
In this post we'll see how to count the top-n items of a dataset; we'll again use the flatland book ...
- 观察者模式与Guava EventBus
观察者模式 结构图 代码实现 public abstract class Subject { private List<Observer> observerList = new Array ...
- 【BZOJ】【2120】数颜色 & 【2453】维护队列
莫队算法 分块大法吼 这题乍一看跟HH的项链很像啊……只是多了一个修改操作……然而我就不会做了