SpringBoot操作MongoDB实现增删改查

时间:2024-03-24 16:03:38

本篇博客主讲如何使用SpringBoot操作MongoDB。

SpringBoot操作MongoDB实现增删改查

(1)pom.xml引入依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
1
2
3
4
(2)创建application.yml

spring:
data:
mongodb:
host: 192.168.72.129
database: studentdb
1
2
3
4
5
(3)创建实体类
创建包com.changan.mongodb,包下建包pojo 用于存放实体类,创建实体类

package com.changan.mongdb.pojo;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;

@Document(collection = "student")
public class Student implements Serializable {

@Id
private Long id;

private String name;

private String sex;

private String age;

private String introduce;

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public String getIntroduce() {
return introduce;
}

public void setIntroduce(String introduce) {
this.introduce = introduce;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}

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
(4)创建数据访问接口
com.changan.mongodb包下创建dao包,包下创建接口

package com.changan.mongdb.dao;

import com.changan.mongdb.pojo.Student;

import java.util.List;
import java.util.Map;

public interface StudentDao {

void save(Student student);

void update(Student student);

List<Student> findAll();

void delete(Integer id);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(5)创建业务逻辑类
com.changan.mongodb包下创建impl包,包下创建类

package com.changan.mongdb.dao.impl;

import com.changan.mongdb.dao.StudentDao;
import com.changan.mongdb.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.LookupOperation;
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.stereotype.Component;

import java.util.List;

@Component
public class StudentDaoImpl implements StudentDao {

@Autowired
private MongoTemplate mongoTemplate;

/**
* 新增信息
* @param student
*/
@Override
public void save(Student student) {
mongoTemplate.save(student);
}

/**
* 修改信息
* @param student
*/
@Override
public void update(Student student) {
//修改的条件
Query query = new Query(Criteria.where("id").is(student.getId()));

//修改的内容
Update update = new Update();
update.set("name",student.getName());

mongoTemplate.updateFirst(query,update,Student.class);
}

/**
* 查询所有信息
* @return
*/
@Override
public List<Student> findAll() {
return mongoTemplate.findAll(Student.class);
}

/**
* 根据id查询所有信息
* @param id
*/
@Override
public void delete(Integer id) {
Student byId = mongoTemplate.findById(1,Student.class);
mongoTemplate.remove(byId);
}

}
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
(6)创建测试类

package com.changan.mongdb;

import com.changan.mongdb.dao.StudentDao;
import com.changan.mongdb.pojo.Student;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MongdbApplicationTests {

@Autowired
private StudentDao studentDao;

/**
* 查询所有信息
*/
@Test
public void findAll() {
List<Student> all = studentDao.findAll();
System.out.println(all.size());
}

/**
* 新增信息
*/
@Test
public void save() {
Student student = new Student();
student.setId(6l);
student.setName("宋人头");
studentDao.save(student);
}

/**
* 修改信息
*/
@Test
public void update() {
Student student = new Student();
student.setId(2l);
student.setName("吴很帅");
studentDao.update(student);
}

/**
* 删除信息
*/
@Test
public void delete(http://www.my516.com) {
studentDao.delete(3);
}
}

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

---------------------