博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring boot 1.5.2 操作mongodb3.4.0
阅读量:6149 次
发布时间:2019-06-21

本文共 2828 字,大约阅读时间需要 9 分钟。

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 configspring.data.mongodb.host=127.0.0.1spring.data.mongodb.port=27017spring.data.mongodb.username=gwzhspring.data.mongodb.password=gwzhspring.data.mongodb.database=gwzhspring.data.mongodb.authentication-database=gwzh#server configserver.port=9001

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 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
getUsers(){ return userRepository.findAll(); } @RequestMapping( value = "/delete", method = RequestMethod.POST ) public boolean deleteUser(@RequestBody UserVo vo){ userRepository.delete(vo.getUserid()); return true; }}

5:测试

 

转载地址:http://xfmya.baihongyu.com/

你可能感兴趣的文章
java父子进程通信
查看>>
Android ADB server didn't ACK * failed to start daemon * 简单有效的解决方案
查看>>
Olap学习笔记
查看>>
Codeforces Round #431 (Div. 1)
查看>>
如何进行数组去重
查看>>
将标题空格替换为 '_' , 并自动复制到剪切板上
查看>>
List Collections sort
查看>>
Mysql -- You can't specify target table 'address' for update in FROM clause
查看>>
使用局部标准差实现图像的局部对比度增强算法。
查看>>
2017-2018-1 20165313 《信息安全系统设计基础》第八周学习总结
查看>>
《代码敲不队》第四次作业:项目需求调研与分析
查看>>
菜鸡互啄队—— 团队合作
查看>>
HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法...
查看>>
SparseArray
查看>>
第二章
查看>>
android背景选择器selector用法汇总
查看>>
[转]Paul Adams:为社交设计
查看>>
showdialog弹出窗口刷新问题
查看>>
java
查看>>
Vue.js连接后台数据jsp页面  ̄▽ ̄
查看>>