SpringBoot操作MongoDB实现增删改查
本篇博客主讲如何使用SpringBoot操作MongoDB。
SpringBoot操作MongoDB实现增删改查
(1)pom.xml引入依赖
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
(2)创建application.yml
spring:
data:
mongodb:
host: 192.168.72.129
database: studentdb
(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;
}
}
(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);
}
(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);
}
}
(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() {
studentDao.delete(3);
}
}
作者:丢了微笑该如何释怀
来源:CSDN
原文:https://blog.csdn.net/weixin_42236165/article/details/91884029
版权声明:本文为博主原创文章,转载请附上博文链接!