摘要:一本节目标前两章主要讲了的基本操作,这一章我们将学习使用访问,并通过完成简单操作。这里有一个问题什么不选用数据库呢答案是目前支持。突出点是,即非阻塞的。二构建项目及配置本章不在讲解如何构建项目了,大家可以参考第一章。
一、本节目标
前两章主要讲了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文件加入下列内容即可。
</>复制代码
org.springframework.boot
spring-boot-starter-data-mongodb-reactive
如何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{
}
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
/**
* 查找所有学生信息
*/
fun list(): Flux
/**
* 创建一个学生信息
*/
fun create(student: Student): Mono
/**
* 通过学生编号删除学生信息
*/
fun delete(id: String): Mono
}
// 接口实现类
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 {
return studentRepository.findById(id)
}
override fun list(): Flux {
return studentRepository.findAll()
}
override fun create(student: Student): Mono {
return studentRepository.save(student)
}
override fun delete(id: String): Mono {
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 {
logger.info("【保存学生信息】请求参数:{}", student)
return studentService.create(student)
}
/**
* 更新学生信息
*/
@PutMapping("/")
fun update(@RequestBody student: Student): Mono {
logger.info("【更新学生信息】请求参数:{}", student)
return studentService.create(student)
}
/**
* 查找所有学生信息
*/
@GetMapping("/list")
fun listStudent(): Flux {
return studentService.list()
}
/**
* 通过学生编号查找学生信息
*/
@GetMapping("/id")
fun student(@RequestParam id : String): Mono {
logger.info("查询学生编号:{}", id)
return studentService.find(id)
}
/**
* 通过学生编号删除学生信息
*/
@DeleteMapping("/")
fun delete(@RequestParam id: String): Mono {
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"}
我们看一下数据库是否存储了
其他接口测试情况
如果大家觉得文章有用麻烦点一下赞,有问题的地方欢迎大家指出来。
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/77524.html
摘要:一本节目标前两章主要讲了的基本操作,这一章我们将学习使用访问,并通过完成简单操作。这里有一个问题什么不选用数据库呢答案是目前支持。突出点是,即非阻塞的。二构建项目及配置本章不在讲解如何构建项目了,大家可以参考第一章。 showImg(https://segmentfault.com/img/remote/1460000018819338?w=1024&h=500); 一、本节目标 前两...
摘要:二教程环境三创建项目创建项目有两种方式一种是在官网上创建二是在上创建如图所示勾选然后点,然后一直默认最后点击完成即可。我们这里看到和普通的接口没有异同,除了返回类型是用包装之外。与之对应的还有,这个后面我们会讲到。 showImg(https://segmentfault.com/img/remote/1460000018819338?w=1024&h=500); 从去年开始就开始学习...
摘要:下一代服务端开发下一代服务端开发第部门快速开始第章快速开始环境准备,,快速上手实现一个第章企业级服务开发从到语言的缺点发展历程的缺点为什么是产生的背景解决了哪些问题为什么是的发展历程容器的配置地狱是什么从到下一代企业级服务开发在移动开发领域 《 Kotlin + Spring Boot : 下一代 Java 服务端开发 》 Kotlin + Spring Boot : 下一代 Java...
showImg(https://segmentfault.com/img/remote/1460000018819338?w=1024&h=500); 在开发项目中,我们经常会需要打印日志,这样方便开发人员了解接口调用情况及定位错误问题,很多时候对于Controller或者是Service的入参和出参需要打印日志,但是我们又不想重复的在每个方法里去使用logger打印,这个时候希望有一个管理者统一...
摘要:在配置下上面启动的配置数据库名为账号密码也为。突出点是,即非阻塞的。四对象修改包里面的城市实体对象类。修改城市对象,代码如下城市实体类城市编号省份编号城市名称描述注解标记对应库表的主键或者唯一标识符。 摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 这是泥瓦匠的第104篇原创 文章工程: JDK...
阅读 2063·2021-11-22 14:45
阅读 2744·2021-10-12 10:11
阅读 845·2021-09-22 10:02
阅读 1408·2019-08-30 15:55
阅读 1229·2019-08-30 15:54
阅读 3343·2019-08-30 15:54
阅读 1326·2019-08-29 17:16
阅读 3186·2019-08-28 17:55
极致性价比!云服务器续费无忧!
Tesla A100/A800、Tesla V100S等多种GPU云主机特惠2折起,不限台数,续费同价。
NVIDIA RTX 40系,高性价比推理显卡,满足AI应用场景需要。
乌兰察布+上海青浦,满足东推西训AI场景需要