Springboot整合MongoDB
1、pom.xml引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2、创建application.yml
spring:
data:
mongodb:
host: #ip 地址
database: studentdb # 数据库名称
3、创建实体类
@Data
@Document(collection = "student") //对应的集合
public class Student {
@Id
private Integer id;
private String name;
private String sex;
private Integer gradeId;
private String gradeName;
public Student() {
}
public Student(Integer id, String name, String sex, Integer gradeId) {
this.id = id;
this.name = name;
this.sex = sex;
this.gradeId = gradeId;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", gradeId=" + gradeId +
", gradeName='" + gradeName + '\'' +
'}';
}
}