微信公众号:一个优秀的废人。若有问题,请后台留言,反正我也不会听。html
如题,今天介绍下 SpringBoot 是如何整合 MongoDB 的。前端
MongoDB 是由 C++ 编写的非关系型数据库,是一个基于分布式文件存储的开源数据库系统,它将数据存储为一个文档,数据结构由键值 (key=>value) 对组成。MongoDB 文档相似于 JSON 对象。字段值能够包含其余文档,数组及文档数组,很是灵活。存储结构以下:java
{
"studentId": "201311611405",
"age":24,
"gender":"男",
"name":"一个优秀的废人"
}
复制代码
spring:
data:
mongodb:
uri: mongodb://localhost:27017/test
复制代码
以上是无密码写法,若是 MongoDB 设置了密码应这样设置:git
spring:
data:
mongodb:
uri: mongodb://name:password@localhost:27017/test
复制代码
<!-- mongodb 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<!-- web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- lombok 依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- test 依赖(没用到) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
复制代码
@Data
public class Student {
@Id
private String id;
@NotNull
private String studentId;
private Integer age;
private String name;
private String gender;
}
复制代码
和 JPA 同样,SpringBoot 一样为开发者准备了一套 Repository ,只须要继承 MongoRepository 传入实体类型以及主键类型便可。github
@Repository
public interface StudentRepository extends MongoRepository<Student, String> {
}
复制代码
public interface StudentService {
Student addStudent(Student student);
void deleteStudent(String id);
Student updateStudent(Student student);
Student findStudentById(String id);
List<Student> findAllStudent();
}
复制代码
实现类:web
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentRepository studentRepository;
/** * 添加学生信息 * @param student * @return */
@Override
@Transactional(rollbackFor = Exception.class)
public Student addStudent(Student student) {
return studentRepository.save(student);
}
/** * 根据 id 删除学生信息 * @param id */
@Override
public void deleteStudent(String id) {
studentRepository.deleteById(id);
}
/** * 更新学生信息 * @param student * @return */
@Override
@Transactional(rollbackFor = Exception.class)
public Student updateStudent(Student student) {
Student oldStudent = this.findStudentById(student.getId());
if (oldStudent != null){
oldStudent.setStudentId(student.getStudentId());
oldStudent.setAge(student.getAge());
oldStudent.setName(student.getName());
oldStudent.setGender(student.getGender());
return studentRepository.save(oldStudent);
} else {
return null;
}
}
/** * 根据 id 查询学生信息 * @param id * @return */
@Override
public Student findStudentById(String id) {
return studentRepository.findById(id).get();
}
/** * 查询学生信息列表 * @return */
@Override
public List<Student> findAllStudent() {
return studentRepository.findAll();
}
}
复制代码
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@PostMapping("/add")
public Student addStudent(@RequestBody Student student){
return studentService.addStudent(student);
}
@PutMapping("/update")
public Student updateStudent(@RequestBody Student student){
return studentService.updateStudent(student);
}
@GetMapping("/{id}")
public Student findStudentById(@PathVariable("id") String id){
return studentService.findStudentById(id);
}
@DeleteMapping("/{id}")
public void deleteStudentById(@PathVariable("id") String id){
studentService.deleteStudent(id);
}
@GetMapping("/list")
public List<Student> findAllStudent(){
return studentService.findAllStudent();
}
}
复制代码
Postman 测试已经所有经过,这里仅展现了保存操做。算法
这里推荐一个数据库可视化工具 Robo 3T 。下载地址:https://robomongo.org/downloadspring
https://github.com/turoDog/Demo/tree/master/springboot_mongodb_demomongodb
若是以为对你有帮助,请给个 Star 再走呗,很是感谢。数据库
若是本文对你哪怕有一丁点帮助,请帮忙点好看。你的好看是我坚持写做的动力。
另外,关注以后在发送 1024 可领取免费学习资料。
资料详情请看这篇旧文:Python、C++、Java、Linux、Go、前端、算法资料分享