时间:2021-05-19
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId></dependency>properties配置
spring.data.mongodb.host=127.0.0.1spring.data.mongodb.port=27017spring.data.mongodb.database=enterprise_dc_dbspring.data.mongodb.username=enterprise_dc_dbaspring.data.mongodb.password=123456 server.port=8081创建实体类
@Document为mongodb集合名
package com.example.springbootmongodb.domian; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection = "mydb")public class Users { private String userId; private String name; private Integer age; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}controller
package com.example.springbootmongodb.controller;public class ResultObject { private int code; private String desc; private Object data; public ResultObject() { } public ResultObject(int code) { this.code = code; } public ResultObject(int code, Object data) { this.code = code; this.data = data; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; }}package com.example.springbootmongodb.controller; import com.example.springbootmongodb.domian.Users;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.mongodb.core.MongoTemplate;import org.springframework.data.mongodb.core.query.Criteria;import org.springframework.data.mongodb.core.query.Query;import org.springframework.data.mongodb.core.query.Update;import org.springframework.web.bind.annotation.*; import java.util.List; @RequestMapping("/mongo")@RestControllerpublic class UserController { @Autowired private MongoTemplate mongoTemplate; @RequestMapping(value = "/hello",method = RequestMethod.GET) public String hello(){ return "hello"; } /** * 新增 */ @ResponseBody @RequestMapping(value = "/insert", method = RequestMethod.GET) public ResultObject insert(){ Users user = new Users(); user.setUserId("1002"); user.setName("eric"); user.setAge(18); mongoTemplate.insert(user); return new ResultObject(200); } /**删除 * * @return */ @ResponseBody @RequestMapping("/delete") public ResultObject delete(){ Query query = Query.query(Criteria.where("userId").is("1001")); mongoTemplate.remove(query,"mydb"); return new ResultObject(200); } /** * 删除 * @return */ @ResponseBody @RequestMapping("/update") public ResultObject update(){ Query query = Query.query(Criteria.where("userId").is("1002")); Update update = new Update(); update.set("name","name1"); update.set("age",18); mongoTemplate.updateFirst(query,update,"mydb"); return new ResultObject(200); } /** * 查询 * @return */ @ResponseBody @RequestMapping("/query") public ResultObject query(){ Query query = Query.query(Criteria.where("userId").is("1001")); List<Users> list = mongoTemplate.find(query,Users.class); return new ResultObject(200,list); }}到此这篇关于spring boot集成mongodb的增删改查的示例代码的文章就介绍到这了,更多相关springboot mongodb增删改查内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
复制代码代码如下:extJs中常用到的增删改查操作的示例代码
Java连接MongoDB进行增删改查的操作1.创建数据库的连接,进行增删改查(分别为接口和实现类)packagecom.dao;importjava.util
本文介绍了ASP.NET实现Hadoop增删改查的示例代码,分享给大家,具体如下:packages.configHDFSAccess.csusingMicros
用AngularJS实现对表格的增删改查(仅限前端),具体代码:实现表格的增删改查.add{position:relative;top:-40px;left:1
Mongoose是什么?Mongoose是MongoDB的一个对象模型工具,封装了许多MongoDB对文档的的增删改查等常用方法,让NodeJS操作Mongod