服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - spring boot集成mongodb的增删改查的示例代码

spring boot集成mongodb的增删改查的示例代码

2021-08-26 11:54Java高级架构师 Java教程

这篇文章主要介绍了spring boot集成mongodb的增删改查的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

添加依赖

?
1
2
3
4
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

properties配置

?
1
2
3
4
5
6
7
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.database=enterprise_dc_db
spring.data.mongodb.username=enterprise_dc_dba
spring.data.mongodb.password=123456
 
server.port=8081

创建实体类

@Document为mongodb集合名

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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")
@RestController
public 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增删改查内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_17010193/article/details/114861203

延伸 · 阅读

精彩推荐