《Kotlin极简教程》正式上架:
点击这里 > 去京东商城购买阅读
点击这里 > 去天猫商城购买阅读
大家请多支持 !!
只是用这两个插件:
apply plugin: 'org.springframework.boot'
apply plugin: 'kotlin'
完整配置:
buildscript {
ext.kotlin_version = '1.1.2' // Required for Kotlin integration
ext.spring_boot_version = '1.5.3.RELEASE'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // Required for Kotlin integration
classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
}
}
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'kotlin' // Required for Kotlin integration
apply plugin: 'org.springframework.boot'
apply plugin: 'application'
写出来的Kotlin调用Java的SpringBoot代码会比较丑:
package org.jetbrains.kotlin.demo
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
open class Application {
companion object {
@JvmStatic fun main(args: Array<String>) {
SpringApplication.run(Application::class.java, *args)
}
}
}
每个类,函数上都得标个open。如果要使用Java默认无参构造函数的功能,就要加上
"org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
我们要写兼容Java的Kotlin代码,当然不会每个类都再手动写个无参构造函数。Kotlin的构造函数跟Java的语法上以及使用方式还是有些不同的。
使用下面的这个配置,代码写起来就自然许多了:
group 'com.easy.kotlin'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1.2-2'
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")
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
//Kotlin整合SpringBoot需要的spring,jpa,org.springframework.boot插件
apply plugin: 'kotlin-spring'
apply plugin: 'kotlin-jpa'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
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')
}
代码就可以这样写了:
package com.easy.kotlin
import com.easy.kotlin.entity.People
import com.easy.kotlin.service.PeopleService
import org.slf4j.LoggerFactory
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.context.annotation.Bean
import java.util.*
/**
* Created by jack on 2017/6/7.
*/
@SpringBootApplication
class Application {
private val log = LoggerFactory.getLogger(Application::class.java)
@Bean
fun init(repository: PeopleService) = CommandLineRunner {
val now = Date();
repository.save(People(null, "Jason", "Chen", "Male", 28, now, now))
repository.save(People(null, "Bluce", "Li", "Male", 32, now, now))
repository.save(People(null, "Corey", "Chen", "Female", 20, now, now))
for (people in repository.findAll()!!) {
log.info(people.toString())
}
val customer = repository.findOne(1L)
log.info(customer.toString())
for (chen in repository.findByLastName("Chen")!!) {
log.info(chen.toString())
}
}
}
fun main(args: Array<String>) {
SpringApplication.run(Application::class.java, *args)
}
示例工程源码:
https://github.com/EasyKotlin/easy_kotlin_chapter2_hello_world_springboot_restful
网友评论