14.SpringData-Mongo
SpringData-Mongo
Spring-data对MongoDB做了支持,使用spring-data-mongodb可以简化MongoDB的操作,封装了底层的mongodb-driver。
地址:https://spring.io/projects/spring-data-mongodb
使用Spring-Data-MongoDB很简单,只需要如下几步即可:
- 导入起步依赖
- 编写配置信息
- 编写实体类(配置注解 @Document,@Id)
- 操作mongodb
- 注入MongoTemplate对象,完成CRUD操作
- 编写Repository接口,注入接口完成基本Crud操作
一、环境搭建,导入起步依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
二、编写配置信息,application.yml
spring:
data:
mongodb:
uri: mongodb://192.168.136.160:27017/tb_student
三、编写实体类(配置注解 @Document,@Id
package com.zhang.mongodb.domain;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(value = "student")
public class Student {
private ObjectId id;
private String name;
private Integer age;
private String school;
public Student() {
}
public Student(String name, Integer age, String school) {
this.name = name;
this.age = age;
this.school = school;
}
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;
}
public String getSchool() {
return school;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", school='" + school + '\'' +
'}';
}
public void setSchool(String school) {
this.school = school;
}
}
四、编写启动类
package com.zhang.mongodb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//服务启动类
@SpringBootApplication
public class MongoDBApplication {
public static void main(String[] args) {
SpringApplication.run(MongoDBApplication.class, args);
}
}
五、新建测试类操作MongoDB,crud
import com.mongodb.MongoGridFSException;
import com.zhang.mongodb.MongoDBApplication;
import com.zhang.mongodb.domain.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.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.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MongoDBApplication.class)
public class TestMongoDb {
@Autowired
private MongoTemplate template;
//增
@Test
public void test(){
Student student =new Student();
student.setName("社会龙");
student.setAge(29);
student.setSchool("傻逼玩意");
template.save(student);
}
/**
* 查询所有
*/
@Test
public void findAll(){
List<Student> allStudent = template.findAll(Student.class);
for (Student student : allStudent) {
System.out.println(student);
}
}
/**
* 条件查询
*/
@Test
public void findOfContion(){
//1.创建Criteria对象,设置条件
Criteria criteria = Criteria.where("name").is("社会龙");
//2.根据Criteria创建query对象
Query query = Query.query(criteria);
List<Student> students = template.find(query, Student.class);
for (Student student : students) {
System.out.println(student);
}
}
/**
* 分页查询
*/
@Test
public void findPage(){
int page = 1;
int pageSize = 2;
Criteria criteria = Criteria.where("age").lt(50);
Query query = Query.query(criteria);
query.skip((page - 1) * pageSize);
query.limit(pageSize);
List<Student> students = template.find(query, Student.class);
for (Student student : students) {
System.out.println(student);
}
}
/**
* 修改
*/
@Test
public void update(){
//1.根据id修改
Criteria criteria = Criteria.where("id").is("63554b381e26b657eaa77aa4");
Query query = Query.query(criteria);
Update update = new Update();
update.set("name", "傻逼社会龙");
update.set("school","装逼社会long");
template.updateFirst(query, update, Student.class);
}
/**
* 删除
*/
@Test
public void remove(){
Criteria criteria = Criteria.where("id").is("63554b381e26b657eaa77aa4");
Query query = Query.query(criteria);
template.remove(query, Student.class);
}
}