前兩章主要講了SpringBoot Kotlin的基本操做,這一章咱們將學習使用Kotlin訪問MongoDB,並經過JPA完成(Create,Read,Update,Delete)簡單操做。這裏有一個問題什麼不選用MySQL數據庫呢?java
Spring Data Reactive Repositories 突出點是 Reactive,即非阻塞的。區別以下:react
本章不在講解如何構建項目了,你們能夠參考第一章。這裏咱們主要引入了mongodb-reactive框架,在pom文件加入下列內容便可。web
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId> </dependency>
如何jar包後咱們須要配置一下MongoDB數據庫,在application.properties文件中加入一下配置便可,密碼和用戶名須要替換本身的,否則會報錯的。spring
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
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) } }
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>{ }
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) } }
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這裏接不用作過多的介紹了,不懂能夠自行百度。
控制檯打印以下:sql
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"}
咱們看一下數據庫是否存儲了
mongodb
其餘接口測試狀況
若是你們以爲文章有用麻煩點一下贊,有問題的地方歡迎你們指出來。數據庫