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的发布配置都是一样的,所以这里不具体讲这个 ...
随机推荐
- Codeforces Round #288 (Div. 2) E. Arthur and Brackets 贪心
E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input stand ...
- UVALive 4863 Balloons 贪心/费用流
There will be several test cases in the input. Each test case will begin with a line with three inte ...
- PAT甲级1114. Family Property
PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...
- 基于直接最小二乘的椭圆拟合(Direct Least Squares Fitting of Ellipses)
算法思想: 算法通过最小化约束条件4ac-b^2 = 1,最小化距离误差.利用最小二乘法进行求解,首先引入拉格朗日乘子算法获得等式组,然后求解等式组得到最优的拟合椭圆. 算法的优点: a.椭圆的特异性 ...
- .net orm类库 kiss.data 简单文档记录
kiss.data的简单记录 == [github地址](https://github.com/sdming/Kiss.Data) kiss.data是golang的数据库访问类库[kdb](http ...
- Git_工作区和暂存区
Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory):就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工 ...
- IOS快速入门
http://www.cnblogs.com/wellsoho/p/4313312.html
- leetcode第一刷_Restore IP Addresses
字符串的问题真是难.一般递归比較好写代码,一般地归还会超时,并且測试用例特别多.. 这道题刚拿到手时直接慌了,这情况也太多了.后来冷静下来想想,事实上还是比較单纯的. 一个ip地址,肯定是四个整数加三 ...
- MVC中使用CKEditor01-基础
本篇体验在MVC中使用CKEditor,仅仅算思路.基础,暂没有把验证等与CKEditor结合在一起考虑. □ 1 使用NUGET引入CKEditorPM> Install-Package CK ...
- spring Boot打可执行的jar包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...