资讯专栏INFORMATION COLUMN

SpringBoot 2.X Kotlin 系列之Reactive Mongodb 与 JPA

MSchumi / 2458人阅读

摘要:一本节目标前两章主要讲了的基本操作,这一章我们将学习使用访问,并通过完成简单操作。这里有一个问题什么不选用数据库呢答案是目前支持。突出点是,即非阻塞的。二构建项目及配置本章不在讲解如何构建项目了,大家可以参考第一章。

一、本节目标

前两章主要讲了SpringBoot Kotlin的基本操作,这一章我们将学习使用Kotlin访问MongoDB,并通过JPA完成(Create,Read,Update,Delete)简单操作。这里有一个问题什么不选用MySQL数据库呢?

答案是 Spring Data Reactive Repositories 目前支持 Mongo、Cassandra、Redis、Couchbase。不支持 MySQL,那究竟为啥呢?那就说明下 JDBC 和 Spring Data 的关系。

Spring Data Reactive Repositories 突出点是 Reactive,即非阻塞的。区别如下:

基于 JDBC 实现的 Spring Data,比如 Spring Data JPA 是阻塞的。原理是基于阻塞 IO 模型 消耗每个调用数据库的线程(Connection)。

事务只能在一个 java.sql.Connection 使用,即一个事务一个操作。

二、构建项目及配置

本章不在讲解如何构建项目了,大家可以参考第一章。这里我们主要引入了mongodb-reactive框架,在pom文件加入下列内容即可。

</>复制代码

  1. org.springframework.boot
  2. spring-boot-starter-data-mongodb-reactive

如何jar包后我们需要配置一下MongoDB数据库,在application.properties文件中加入一下配置即可,密码和用户名需要替换自己的,不然会报错的。

</>复制代码

  1. spring.data.mongodb.host=localhost
  2. spring.data.mongodb.port=27017
  3. spring.data.mongodb.password=student2018.Docker_
  4. spring.data.mongodb.database=student
  5. spring.data.mongodb.username=student
三、创建实体及具体实现 3.1实体创建

</>复制代码

  1. package io.intodream.kotlin03.entity
  2. import org.springframework.data.annotation.Id
  3. import org.springframework.data.mongodb.core.mapping.Document
  4. /**
  5. * @description
  6. *
  7. * @author Jwenk
  8. * @copyright intoDream.io 筑梦科技
  9. * @email xmsjgzs@163.com
  10. * @date 2019-03-25,18:05
  11. */
  12. @Document
  13. class Student {
  14. @Id
  15. var id :String? = null
  16. var name :String? = null
  17. var age :Int? = 0
  18. var gender :String? = null
  19. var sClass :String ?= null
  20. override fun toString(): String {
  21. return ObjectMapper().writeValueAsString(this)
  22. }
  23. }
3.2Repository

</>复制代码

  1. package io.intodream.kotlin03.dao
  2. import io.intodream.kotlin03.entity.Student
  3. import org.springframework.data.mongodb.repository.ReactiveMongoRepository
  4. /**
  5. * @description
  6. *
  7. * @author Jwenk
  8. * @copyright intoDream.io 筑梦科技
  9. * @email xmsjgzs@163.com
  10. * @date 2019-03-25,18:04
  11. */
  12. interface StudentRepository : ReactiveMongoRepository{
  13. }
3.3Service

</>复制代码

  1. package io.intodream.kotlin03.service
  2. import io.intodream.kotlin03.entity.Student
  3. import reactor.core.publisher.Flux
  4. import reactor.core.publisher.Mono
  5. /**
  6. * @description
  7. *
  8. * @author Jwenk
  9. * @copyright intoDream.io 筑梦科技
  10. * @email xmsjgzs@163.com
  11. * @date 2019-03-25,18:04
  12. */
  13. interface StudentService {
  14. /**
  15. * 通过学生编号获取学生信息
  16. */
  17. fun find(id : String): Mono
  18. /**
  19. * 查找所有学生信息
  20. */
  21. fun list(): Flux
  22. /**
  23. * 创建一个学生信息
  24. */
  25. fun create(student: Student): Mono
  26. /**
  27. * 通过学生编号删除学生信息
  28. */
  29. fun delete(id: String): Mono
  30. }
  31. // 接口实现类
  32. package io.intodream.kotlin03.service.impl
  33. import io.intodream.kotlin03.dao.StudentRepository
  34. import io.intodream.kotlin03.entity.Student
  35. import io.intodream.kotlin03.service.StudentService
  36. import org.springframework.beans.factory.annotation.Autowired
  37. import org.springframework.stereotype.Service
  38. import reactor.core.publisher.Flux
  39. import reactor.core.publisher.Mono
  40. /**
  41. * @description
  42. *
  43. * @author Jwenk
  44. * @copyright intoDream.io 筑梦科技
  45. * @email xmsjgzs@163.com
  46. * @date 2019-03-25,18:23
  47. */
  48. @Service
  49. class StudentServiceImpl : StudentService{
  50. @Autowired lateinit var studentRepository: StudentRepository
  51. override fun find(id: String): Mono {
  52. return studentRepository.findById(id)
  53. }
  54. override fun list(): Flux {
  55. return studentRepository.findAll()
  56. }
  57. override fun create(student: Student): Mono {
  58. return studentRepository.save(student)
  59. }
  60. override fun delete(id: String): Mono {
  61. return studentRepository.deleteById(id)
  62. }
  63. }
3.4 Controller实现

</>复制代码

  1. package io.intodream.kotlin03.web
  2. import io.intodream.kotlin03.entity.Student
  3. import io.intodream.kotlin03.service.StudentService
  4. import org.slf4j.LoggerFactory
  5. import org.springframework.beans.factory.annotation.Autowired
  6. import org.springframework.web.bind.annotation.*
  7. import reactor.core.publisher.Flux
  8. import reactor.core.publisher.Mono
  9. /**
  10. * @description
  11. *
  12. * @author Jwenk
  13. * @copyright intoDream.io 筑梦科技
  14. * @email xmsjgzs@163.com
  15. * @date 2019-03-25,18:03
  16. */
  17. @RestController
  18. @RequestMapping("/api/student")
  19. class StudentController {
  20. @Autowired lateinit var studentService: StudentService
  21. val logger = LoggerFactory.getLogger(this.javaClass)
  22. /**
  23. * 保存或新增学生信息
  24. */
  25. @PostMapping("/")
  26. fun create(@RequestBody student: Student): Mono {
  27. logger.info("【保存学生信息】请求参数:{}", student)
  28. return studentService.create(student)
  29. }
  30. /**
  31. * 更新学生信息
  32. */
  33. @PutMapping("/")
  34. fun update(@RequestBody student: Student): Mono {
  35. logger.info("【更新学生信息】请求参数:{}", student)
  36. return studentService.create(student)
  37. }
  38. /**
  39. * 查找所有学生信息
  40. */
  41. @GetMapping("/list")
  42. fun listStudent(): Flux {
  43. return studentService.list()
  44. }
  45. /**
  46. * 通过学生编号查找学生信息
  47. */
  48. @GetMapping("/id")
  49. fun student(@RequestParam id : String): Mono {
  50. logger.info("查询学生编号:{}", id)
  51. return studentService.find(id)
  52. }
  53. /**
  54. * 通过学生编号删除学生信息
  55. */
  56. @DeleteMapping("/")
  57. fun delete(@RequestParam id: String): Mono {
  58. logger.info("删除学生编号:{}", id)
  59. return studentService.delete(id)
  60. }
  61. }
四、接口测试

这里我们使用Postman来对接口进行测试,关于Postman这里接不用做过多的介绍了,不懂可以自行百度。

控制台打印如下:

</>复制代码

  1. 2019-03-25 18:57:04.333 INFO 2705 --- [ctor-http-nio-3] i.i.kotlin03.web.StudentController : 【保存学生信息】请求参数:{"id":"1","name":"Tom","age":18,"gender":"Boy","sclass":"First class"}

我们看一下数据库是否存储了

其他接口测试情况




如果大家觉得文章有用麻烦点一下赞,有问题的地方欢迎大家指出来。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77524.html

相关文章

  • SpringBoot 2.X Kotlin 系列Reactive Mongodb JPA

    摘要:一本节目标前两章主要讲了的基本操作,这一章我们将学习使用访问,并通过完成简单操作。这里有一个问题什么不选用数据库呢答案是目前支持。突出点是,即非阻塞的。二构建项目及配置本章不在讲解如何构建项目了,大家可以参考第一章。 showImg(https://segmentfault.com/img/remote/1460000018819338?w=1024&h=500); 一、本节目标 前两...

    琛h。 评论0 收藏0
  • SpringBoot 2.X Kotlin 系列Hello World

    摘要:二教程环境三创建项目创建项目有两种方式一种是在官网上创建二是在上创建如图所示勾选然后点,然后一直默认最后点击完成即可。我们这里看到和普通的接口没有异同,除了返回类型是用包装之外。与之对应的还有,这个后面我们会讲到。 showImg(https://segmentfault.com/img/remote/1460000018819338?w=1024&h=500); 从去年开始就开始学习...

    warkiz 评论0 收藏0
  • Kotlin + Spring Boot : 下一代 Java 服务端开发 》

    摘要:下一代服务端开发下一代服务端开发第部门快速开始第章快速开始环境准备,,快速上手实现一个第章企业级服务开发从到语言的缺点发展历程的缺点为什么是产生的背景解决了哪些问题为什么是的发展历程容器的配置地狱是什么从到下一代企业级服务开发在移动开发领域 《 Kotlin + Spring Boot : 下一代 Java 服务端开发 》 Kotlin + Spring Boot : 下一代 Java...

    springDevBird 评论0 收藏0
  • SpringBoot 2.X Kotlin系列AOP统一打印日志

    showImg(https://segmentfault.com/img/remote/1460000018819338?w=1024&h=500); 在开发项目中,我们经常会需要打印日志,这样方便开发人员了解接口调用情况及定位错误问题,很多时候对于Controller或者是Service的入参和出参需要打印日志,但是我们又不想重复的在每个方法里去使用logger打印,这个时候希望有一个管理者统一...

    Nino 评论0 收藏0
  • Spring Boot 2 快速教程:WebFlux 集成 Mongodb(四)

    摘要:在配置下上面启动的配置数据库名为账号密码也为。突出点是,即非阻塞的。四对象修改包里面的城市实体对象类。修改城市对象,代码如下城市实体类城市编号省份编号城市名称描述注解标记对应库表的主键或者唯一标识符。 摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 这是泥瓦匠的第104篇原创 文章工程: JDK...

    Corwien 评论0 收藏0

发表评论

0条评论

MSchumi

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<