摘要:是一门最近比较流行的静态类型编程语言,而且和一样同属系。这个生成的构造函数是合成的,因此不能从或中直接调用,但可以使用反射调用。
Kotlin是一门最近比较流行的静态类型编程语言,而且和Groovy、Scala一样同属Java系。Kotlin具有的很多静态语言特性诸如:类型判断、多范式、扩展函数、模式匹配等等让我无法只作为一个吃瓜群众了,所以稍微花了点时间了解了一下该语言。
本文主要介绍一下如何使用Kotlin结合SpringBt开发一个带有数据库交互的REST风格基本程序
注: 本文首发于 My 公众号 CodeSheep ,可 长按 或 扫描 下面的 小心心 来订阅 ↓ ↓ ↓实验环境
JDK不用说了,Kotlin毕竟是运行在JVM环境下的语言,所以JDK必须,我这里用的JDK1.8
数据库:MySQL
数据库访问组件:Spring data jpa
J2EE框架:SpringBt 1.5.2.RELEASE
构建工具:Gradle
工程创建没啥好说的,我这里创建的是基于Gradle的Kotlin工程:
创建完成后的基本工程样式和SpringBt的工程几乎没任何区别,给张图示意一下好了:
好啦,接下来我们就来写代码完善这个工程即可
完善build.gradle配置我们需要在build.gradle中引入SpringBt依赖,除此之外还要引入一些特定的插件方便我们向写Java代码一样来写Kotlin程序!
在dependencies中加入如下依赖:
dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version" testCompile group: "junit", name: "junit", version: "4.12" compile("org.springframework.boot:spring-boot-starter-web") testCompile("org.springframework.boot:spring-boot-starter-test") compile("org.springframework.boot:spring-boot-starter-data-jpa") compile("mysql:mysql-connector-java:5.1.13") }
这样SpringBt相关的依赖就配置上了!
接下来我们配置两个非常关键的插件依赖:
无参(no-arg)插件
全开放(allopen)插件
我们先配上,等下解释:
buildscript { ext.kotlin_version = "1.1.1" ext.springboot_version = "1.5.2.RELEASE" repositories { mavenCentral() } dependencies { // Kotlin Gradle插件 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // SpringBoot Gradle插件 classpath("org.springframework.boot:spring-boot-gradle-plugin:$springboot_version") // Kotlin整合SpringBoot的默认无参构造函数,默认把所有的类设置open类插件 classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlin_version") // 无参插件 classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version") // 全开放插件 } }
其中(以下解释源自《Kotlin极简教程》):
org.jetbrains.kotlin:kotlin-noarg是无参(no-arg)编译器插件,它为具有特定注解的类生成一个额外的零参数构造函数。 这个生成的构造函数是合成的,因此不能从 Java 或 Kotlin 中直接调用,但可以使用反射调用。 这样我们就可以使用 Java Persistence API(JPA)实例化 data 类。
org.jetbrains.kotlin:kotlin-allopen 是全开放编译器插件。我们使用Kotlin 调用Java的Spring AOP框架和库,需要类为 open(可被继承实现),而Kotlin 类和函数都是默认 final 的,这样我们需要为每个类和函数前面加上open修饰符。这样的代码写起来很费事。还好,我们有all-open 编译器插件。它会适配 Kotlin 以满足这些框架的需求,并使用指定的注解标注类而其成员无需显式使用 open 关键字打开。 例如,当我们使用 Spring 时,就不需要打开所有的类,跟我们在Java中写代码一样,只需要用相应的注解标注即可,如 @Configuration 或 @Service。
讲白了,引入这两个特定的插件的目的就是为了方便我们向写SpringBt代码一样来写Kotlin程序!
配置application.properties这里面主要是跟Mysql数据库相关的一些配置:
spring.datasource.url = jdbc:mysql://localhost:3306/easykotlin spring.datasource.username = root spring.datasource.password = 你的Mysql密码 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.database = MYSQL spring.datasource.testWhileIdle = true spring.datasource.validationQuery = SELECT 1 spring.jpa.show-sql = true spring.jpa.hibernate.ddl-auto = update spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect server.port=7000正式编写工程
我们需要去数据库中查询东西,所以二话不说,写个访问数据库的标准代码层:
controller
entity
repository
service
各部分代码如下:
People.kt
@Entity class People( @Id @GeneratedValue(strategy = GenerationType.AUTO) val id: Long?, val firstName: String?, val lastName: String?, val gender: String?, val age: Int?, val gmtCreated: Date, val gmtModified: Date ) { override fun toString(): String { return "People(id=$id, firstName="$firstName", lastName="$lastName", gender="$gender", age=$age, gmtCreated=$gmtCreated, gmtModified=$gmtModified)" } }
PeopleRepository.kt
interface PeopleRepository : CrudRepository{ fun findByLastName(lastName: String): List ? }
PeopleService.kt
@Service class PeopleService : PeopleRepository { @Autowired val peopleRepository: PeopleRepository? = null override fun findByLastName(lastName: String): List? { return peopleRepository?.findByLastName(lastName) } override fun save(entity: S): S? { return peopleRepository?.save(entity) } override funsave(entities: MutableIterable?): MutableIterable? { return peopleRepository?.save(entities) } override fun delete(entities: MutableIterable?) { } override fun delete(entity: People?) { } override fun delete(id: Long?) { } override fun findAll(ids: MutableIterable ?): MutableIterable ? { return peopleRepository?.findAll(ids) } override fun findAll(): MutableIterable ? { return peopleRepository?.findAll() } override fun exists(id: Long?): Boolean { return peopleRepository?.exists(id)!! } override fun count(): Long { return peopleRepository?.count()!! } override fun findOne(id: Long?): People? { return peopleRepository?.findOne(id) } override fun deleteAll() { } }
PeopleController.kt
@Controller class PeopleController { @Autowired val peopleService: PeopleService? = null @GetMapping(value = "/hello") @ResponseBody fun hello(@RequestParam(value = "lastName") lastName: String): Any { val peoples = peopleService?.findByLastName(lastName) val map = HashMap() map.put("hello", peoples!!) return map } }
可见有了无参、全开放组件加持后,写代码和写Java的代码基本没区别了
实际实验首先需要去Mysql中建好数据库,并插入一些数据:
然后启动工程,访问:
http://localhost:7000/hello?lastName=wang
可以看到数据成功被取回:
参考文献《Kotlin极简教程》
后记作者更多的原创文章在此,欢迎观赏
My Personal Blog
作者更多的SpringBt实践文章在此:
Spring Boot应用监控实战
SpringBoot应用部署于外置Tomcat容器
ElasticSearch搜索引擎在SpringBt中的实践
初探Kotlin+SpringBoot联合编程
Spring Boot日志框架实践
SpringBoot优雅编码之:Lombok加持
如果有兴趣,也可以抽点时间看看作者一些关于容器化、微服务化方面的文章:
利用K8S技术栈打造个人私有云 连载文章
从一份配置清单详解Nginx服务器配置
Docker容器可视化监控中心搭建
利用ELK搭建Docker容器化应用日志中心
RPC框架实践之:Apache Thrift
RPC框架实践之:Google gRPC
微服务调用链追踪中心搭建
Docker容器跨主机通信
Docker Swarm集群初探
高效编写Dockerfile的几条准则
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/68333.html
摘要:概述进行的开发过程中,我们很多时候经常需要重启服务器才能保证修改的源代码文件或者一些诸如的配置文件以及一些静态文件生效,这样耗时又低效。 showImg(https://segmentfault.com/img/remote/1460000015363888); 概述 进行SpringBoot的Web开发过程中,我们很多时候经常需要重启Web服务器才能保证修改的 源代码文件、或者一些...
摘要:但考虑到实际的情形中,我们的服务器一般是另外部署好了的,有专门的维护方式。此时我们需要剥离掉应用内置的服务器,进而将应用发布并部署到外置的容器之中,本文就实践一下这个。 showImg(https://segmentfault.com/img/remote/1460000015173574); 0x01. 概述 SpringBoot平时我们用的爽歪歪,爽到它自己连Tomcat都自集成...
摘要:微服务的基本思想在于考虑围绕着业务领域组件来创建应用,这些应用可独立地进行开发管理和加速。在分散的组件中使用微服务云架构和平台,使部署管理和服务功能交付变得更加简单。 showImg(https://segmentfault.com/img/remote/1460000014332184); 概述 当下web服务端开发中最火的名词中绝对有微服务的一席之地,其也成为当下互联网后端服务架...
摘要:概述应用一旦容器化以后,需要考虑的就是如何采集位于容器中的应用程序的打印日志供运维分析。 showImg(https://segmentfault.com/img/remote/1460000014146680); 概述 应用一旦容器化以后,需要考虑的就是如何采集位于Docker容器中的应用程序的打印日志供运维分析。典型的比如 SpringBoot应用的日志 收集。本文即将阐述如何利...
阅读 2508·2023-04-25 19:24
阅读 1727·2021-11-11 16:54
阅读 2849·2021-11-08 13:19
阅读 3568·2021-10-25 09:45
阅读 2577·2021-09-13 10:24
阅读 3302·2021-09-07 10:15
阅读 4091·2021-09-07 10:14
阅读 2974·2019-08-30 15:56