SpringBoot 2.X Kotlin 系列之Reactive Mongodb 与 JPA
一、本节目标
前两章主要讲了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文件加入下列内容即可。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
如何jar包后我们需要配置一下MongoDB数据库,在application.properties文件中加入一下配置即可,密码和用户名需要替换自己的,不然会报错的。
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.password=student2018.Docker_
spring.data.mongodb.database=student
spring.data.mongodb.username=student
三、创建实体及具体实现
3.1实体创建
package io.intodream.kotlin03.entity
import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.Document
/**
* @description
*
* @author Jwenk
* @copyright intoDream.io 筑梦科技
* @email xmsjgzs@163.com
* @date 2019-03-25,18:05
*/
@Document
class Student {
@Id
var id :String? = null
var name :String? = null
var age :Int? = 0
var gender :String? = null
var sClass :String ?= null
override fun toString(): String {
return ObjectMapper().writeValueAsString(this)
}
}
3.2Repository
package io.intodream.kotlin03.dao
import io.intodream.kotlin03.entity.Student
import org.springframework.data.mongodb.repository.ReactiveMongoRepository
/**
* @description
*
* @author Jwenk
* @copyright intoDream.io 筑梦科技
* @email xmsjgzs@163.com
* @date 2019-03-25,18:04
*/
interface StudentRepository : ReactiveMongoRepository<Student, String>{
}
3.3Service
package io.intodream.kotlin03.service
import io.intodream.kotlin03.entity.Student
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
/**
* @description
*
* @author Jwenk
* @copyright intoDream.io 筑梦科技
* @email xmsjgzs@163.com
* @date 2019-03-25,18:04
*/
interface StudentService {
/**
* 通过学生编号获取学生信息
*/
fun find(id : String): Mono<Student>
/**
* 查找所有学生信息
*/
fun list(): Flux<Student>
/**
* 创建一个学生信息
*/
fun create(student: Student): Mono<Student>
/**
* 通过学生编号删除学生信息
*/
fun delete(id: String): Mono<Void>
}
// 接口实现类
package io.intodream.kotlin03.service.impl
import io.intodream.kotlin03.dao.StudentRepository
import io.intodream.kotlin03.entity.Student
import io.intodream.kotlin03.service.StudentService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
/**
* @description
*
* @author Jwenk
* @copyright intoDream.io 筑梦科技
* @email xmsjgzs@163.com
* @date 2019-03-25,18:23
*/
@Service
class StudentServiceImpl : StudentService{
@Autowired lateinit var studentRepository: StudentRepository
override fun find(id: String): Mono<Student> {
return studentRepository.findById(id)
}
override fun list(): Flux<Student> {
return studentRepository.findAll()
}
override fun create(student: Student): Mono<Student> {
return studentRepository.save(student)
}
override fun delete(id: String): Mono<Void> {
return studentRepository.deleteById(id)
}
}
3.4 Controller实现
package io.intodream.kotlin03.web
import io.intodream.kotlin03.entity.Student
import io.intodream.kotlin03.service.StudentService
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
/**
* @description
*
* @author Jwenk
* @copyright intoDream.io 筑梦科技
* @email xmsjgzs@163.com
* @date 2019-03-25,18:03
*/
@RestController
@RequestMapping("/api/student")
class StudentController {
@Autowired lateinit var studentService: StudentService
val logger = LoggerFactory.getLogger(this.javaClass)
/**
* 保存或新增学生信息
*/
@PostMapping("/")
fun create(@RequestBody student: Student): Mono<Student> {
logger.info("【保存学生信息】请求参数:{}", student)
return studentService.create(student)
}
/**
* 更新学生信息
*/
@PutMapping("/")
fun update(@RequestBody student: Student): Mono<Student> {
logger.info("【更新学生信息】请求参数:{}", student)
return studentService.create(student)
}
/**
* 查找所有学生信息
*/
@GetMapping("/list")
fun listStudent(): Flux<Student> {
return studentService.list()
}
/**
* 通过学生编号查找学生信息
*/
@GetMapping("/id")
fun student(@RequestParam id : String): Mono<Student> {
logger.info("查询学生编号:{}", id)
return studentService.find(id)
}
/**
* 通过学生编号删除学生信息
*/
@DeleteMapping("/")
fun delete(@RequestParam id: String): Mono<Void> {
logger.info("删除学生编号:{}", id)
return studentService.delete(id)
}
}
四、接口测试
这里我们使用Postman来对接口进行测试,关于Postman这里接不用做过多的介绍了,不懂可以自行百度。
控制台打印如下:
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"}
我们看一下数据库是否存储了
其他接口测试情况
如果大家觉得文章有用麻烦点一下赞,有问题的地方欢迎大家指出来。
13 声望
2 粉丝
推荐阅读
SpringBoot 2.X Kotlin系列之数据校验和异常处理
在开发项目时,我们经常需要在前后端都校验用户提交的数据,判断提交的数据是否符合我们的标准,包括字符串长度,是否为数字,或者是否为手机号码等;这样做的目的主要是为了减少SQL注入攻击的风险以及脏数据的插...
惜鱼赞 1阅读 2.7k
PHP转Go实践:xjson解析神器「开源工具集」
我和劲仔都是PHP转Go,身边越来越多做PHP的朋友也逐渐在用Go进行重构,重构过程中,会发现php的json解析操作(系列化与反序列化)是真的香,弱类型语言的各种隐式类型转换,很大程度的减低了程序的复杂度。
王中阳Go赞 10阅读 1.7k评论 2
万字详解,吃透 MongoDB!
MongoDB 是一个基于 分布式文件存储 的开源 NoSQL 数据库系统,由 C++ 编写的。MongoDB 提供了 面向文档 的存储方式,操作起来比较简单和容易,支持“无模式”的数据建模,可以存储比较复杂的数据类型,是一款非常...
JavaGuide赞 5阅读 725
与RabbitMQ有关的一些知识
工作中用过一段时间的Kafka,不过主要还是RabbitMQ用的多一些。今天主要来讲讲与RabbitMQ相关的一些知识。一些基本概念,以及实际使用场景及一些注意事项。
lpe234赞 8阅读 1.9k
计算机网络连环炮40问
本文已经收录到Github仓库,该仓库包含计算机基础、Java基础、多线程、JVM、数据库、Redis、Spring、Mybatis、SpringMVC、SpringBoot、分布式、微服务、设计模式、架构、校招社招分享等核心知识点,欢迎star~
程序员大彬赞 7阅读 1k
spring boot 锁
由于当前的项目中由于多线程操作同一个实体,会出现数据覆盖的问题,后保存的实体把先保存的实体的数据给覆盖了。于是查找了锁的实现的几种方式。但写到最后发现,其实自己可以写sql 更新需要更新的字段即可,这...
weiewiyi赞 3阅读 9.2k
Git操作不规范,战友提刀来相见!
年终奖都没了,还要扣我绩效,门都没有,哈哈。这波骚Git操作我也是第一次用,担心闪了腰,所以不仅做了备份,也做了笔记,分享给大家。问题描述小A和我在同时开发一个功能模块,他在优化之前的代码逻辑,我在开...
王中阳Go赞 5阅读 2.2k评论 2
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。