前两章主要讲了SpringBoot Kotlin的基本操作,这一章我们将学习使用Kotlin访问MongoDB,并通过JPA完成(Create,Read,Update,Delete)简单操作。这里有一个问题什么不选用MySQL数据库呢?
Spring Data Reactive Repositories 突出点是 Reactive,即非阻塞的。区别如下:
本章不在讲解如何构建项目了,大家可以参考第一章。这里我们主要引入了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
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这里接不用做过多的介绍了,不懂可以自行百度。
控制台打印如下:
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"}
我们看一下数据库是否存储了
其他接口测试情况
如果大家觉得文章有用麻烦点一下赞,有问题的地方欢迎大家指出来。