SpringBoot 实战 (十八) | 整合 MongoDB

发布时间:2019-08-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了SpringBoot 实战 (十八) | 整合 MongoDB脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
微信公众号:一个优秀的废人。如有问题,请后台留言,反正我也不会听。

SpringBoot 实战 (十八) | 整合 MongoDB

前言

如题,今天介绍下 SPRingBoot 是如何整合 MongoDB 的。

MongoDB 简介

MongoDB 是由 C++ 编写的非关系型数据库,是一个基于分布式文件存储的开数据库系统,它将数据存储为一个文档,数据结构由键值 (key=>value) 对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组,非常灵活。存储结构如下:

{
    "studentId": "201311611405",
    "age":24,
    "gender":"男",
    "name":"一个优秀的废人"
}

准备工作

  • SpringBoot 2.1.3 RELEASE
  • MongnDB 2.1.3 RELEASE
  • MongoDB 4.0
  • IDEA
  • JDK8
  • 创建一个名为 test 的数据库,不会建的。参考菜鸟教程:

http://www.runoob.com/mongodb...

配置数据源

spring:
  data:
    mongodb:
      uri: mongodb://localhost:27017/test

以上是无密码写法,如果 MongoDB 设置了密码应这样设置:

spring:
  data:
    mongodb:
      uri: mongodb://name:password@localhost:27017/test

pom 依赖配置

public interface StudentService {

    Student addStudent(Student student);

    void deleteStudent(String id);

    Student updateStudent(Student student);

    Student findStudentById(String id);

    List<Student> findAllStudent();

}

实现类:

@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();
    }
}

controller 层

@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();
    }

}

测试结果

SpringBoot 实战 (十八) | 整合 MongoDB

Postman 测试已经全部通过,这里仅展示了保存操作。

SpringBoot 实战 (十八) | 整合 MongoDB

这里推荐一个数据库可视化工具 Robo 3T 。下载地址:https://robomongo.org/download

完整代码

https://github.com/turoDog/De...

如果觉得对你有帮助,请给个 Star 再走呗,非常感谢。

后语

如果本文对你哪怕有一丁点帮助,请帮忙点好看。你的好看是我坚持写作的动力。

另外,关注之后在发送 1024 可领取免费学习资料

资料详情请看这篇旧文:Python、C++、Java、Linux、Go、前端、算法资料分享

SpringBoot 实战 (十八) | 整合 MongoDB

脚本宝典总结

以上是脚本宝典为你收集整理的SpringBoot 实战 (十八) | 整合 MongoDB全部内容,希望文章能够帮你解决SpringBoot 实战 (十八) | 整合 MongoDB所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。