《Springboot极简教程》 Springboot plus Kotlin :Hello,World

我们的理念是用极简的招数,打败绝顶"高手"。

Kotlin, Console: Hello,World

<h5> Step1. 新建gradle,kotlin工程:

螢幕快照 2017-03-11 12.40.05.png

<h5>Step2. 配置build.gradle

group 'com.jason.chen.mini_springboot'
version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.1.0'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'kotlin'
apply plugin: 'application'

mainClassName = 'jason.chen.mini_springboot.HelloWorldKt'

defaultTasks 'run'

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

<h5>Step3.写HelloWorld Kotlin class

package jason.chen.mini_springboot

/**
 * Created by jack on 2017/3/11.
 * @author jack
 * @date 2017/03/11
 */


fun helloWorld():String {
    val words = mutableListOf<String>()
    words.add("Hello")
    words.add("World!")
    return words.joinToString(separator=" ")
}

fun main(args: Array<String>){
    println(helloWorld())
}

<h5>Step4. Run

螢幕快照 2017-03-11 12.57.23.png

源码:

https://github.com/MiniSpringBootTutorial/mini_springboot/blob/master/src/main/kotlin/jason/chen/mini_springboot/console/HelloWorld.kt

SpringBoot Kotlin JPA Myql, RestAPI : Hello,World

<h5>Step1.配置build.gradle

version = "0.0.1-SNAPSHOT"

buildscript {

    ext{
        springBootVersion = "1.5.2.RELEASE"
        kotlinVersion = "1.1.0"
    }

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
        classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlinVersion")
        classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
    }
}

apply {
    plugin("kotlin")
    plugin("kotlin-spring")
    plugin("kotlin-jpa")
    plugin("org.springframework.boot")
    plugin 'java'
    plugin 'eclipse'
    plugin 'idea'
//    plugin: 'spring-boot'
}

repositories {
    mavenCentral()
}

jar {
    baseName = 'mini_springboot'
    version = '0.0.1'
}


sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
//    compile("com.h2database:h2")
    compile("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion")
    compile("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
    compile("com.fasterxml.jackson.module:jackson-module-kotlin:2.8.4")
    testCompile("org.springframework.boot:spring-boot-starter-test")

    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude module: "spring-boot-starter-tomcat"
    }
    compile("org.springframework.boot:spring-boot-starter-jetty")
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile('mysql:mysql-connector-java:5.1.13')

    testCompile("junit:junit")
}



<h5>Step2. 配置application.properties


spring.datasource.url = jdbc:mysql://localhost:3306/mini_springboot
spring.datasource.username = root
spring.datasource.password = root
#spring.datasource.driverClassName = com.mysql.jdbc.Driver

# Specify the DBMS
spring.jpa.database = MYSQL

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

server.port=9891

<h5>Step3.工程架构

记住,就连UNIX这样的操作系统,“一切都是文件”。so, 你的所有的代码,工程,一切都是文件。

目录

.
├── README_.md
├── build
│   ├── classes
│   │   └── main
│   │       ├── META-INF
│   │       │   └── mini_springboot_main.kotlin_module
│   │       └── jason
│   │           └── chen
│   │               └── mini_springboot
│   │                   ├── console
│   │                   │   └── HelloWorldKt.class
│   │                   ├── rest
│   │                   │   ├── biz
│   │                   │   ├── controller
│   │                   │   └── entity
│   │                   └── restful
│   │                       ├── Application$init$1.class
│   │                       ├── Application.class
│   │                       ├── ApplicationKt.class
│   │                       ├── biz
│   │                       │   └── CustomerRepository.class
│   │                       ├── controller
│   │                       │   └── CustomerController.class
│   │                       ├── entity
│   │                       │   └── Customer.class
│   │                       └── utils
│   │                           ├── DateOperator.class
│   │                           ├── DateOptUnit$WhenMappings.class
│   │                           ├── DateOptUnit.class
│   │                           └── DateUtilsKt.class
│   ├── kotlin
│   │   ├── compileKotlin
│   │   │   └── sync
│   │   │       └── kotlin-files-in-java-timestamps.bin
│   │   └── daemon-is-alive
│   ├── kotlin-build
│   │   └── caches
│   │       └── version.txt
│   ├── kotlin-classes
│   │   └── main
│   │       ├── META-INF
│   │       │   └── mini_springboot_main.kotlin_module
│   │       └── jason
│   │           └── chen
│   │               └── mini_springboot
│   │                   ├── console
│   │                   │   └── HelloWorldKt.class
│   │                   └── restful
│   │                       ├── Application$init$1.class
│   │                       ├── Application.class
│   │                       ├── ApplicationKt.class
│   │                       ├── biz
│   │                       │   └── CustomerRepository.class
│   │                       ├── controller
│   │                       │   └── CustomerController.class
│   │                       ├── entity
│   │                       │   └── Customer.class
│   │                       └── utils
│   │                           ├── DateOperator.class
│   │                           ├── DateOptUnit$WhenMappings.class
│   │                           ├── DateOptUnit.class
│   │                           └── DateUtilsKt.class
│   └── resources
│       └── main
│           ├── application.properties
│           └── application.yml
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── run.bat
├── run.sh
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   ├── kotlin
    │   │   └── jason
    │   │       └── chen
    │   │           └── mini_springboot
    │   │               ├── console
    │   │               │   └── HelloWorld.kt
    │   │               └── restful
    │   │                   ├── Application.kt
    │   │                   ├── biz
    │   │                   │   └── CustomerRepository.kt
    │   │                   ├── controller
    │   │                   │   └── CustomerController.kt
    │   │                   ├── entity
    │   │                   │   └── Customer.kt
    │   │                   └── utils
    │   │                       └── DateUtils.kt
    │   └── resources
    │       ├── application.properties
    │       └── application.yml
    └── test
        ├── java
        ├── kotlin
        └── resources

56 directories, 46 files

一切尽在不言中,静静地看工程文件结构。

<h4>Step4.测试

请求:http://127.0.0.1:9891
响应:

curl http://127.0.0.1:9891
[{"firstName":"Jason","lastName":"Chen","gmtCreated":null,"gmtModified":null,"deletedDate":null,"id":1},{"firstName":"Bluce","lastName":"Li","gmtCreated":null,"gmtModified":null,"deletedDate":null,"id":2},{"firstName":"Michelle","lastName":"Chen","gmtCreated":null,"gmtModified":null,"deletedDate":null,"id":3},{"firstName":"Jason","lastName":"Chen","gmtCreated":1489214640000,"gmtModified":1489214640000,"deletedDate":1489214640000,"id":4},{"firstName":"Bluce","lastName":"Li","gmtCreated":1489214640000,"gmtModified":1489214640000,"deletedDate":1489214640000,"id":5},{"firstName":"Michelle","lastName":"Chen","gmtCreated":1489214640000,"gmtModified":1489214640000,"deletedDate":1489214640000,"id":6}]



curl http://127.0.0.1:9891/Chen
[{"firstName":"Jason","lastName":"Chen","gmtCreated":null,"gmtModified":null,"deletedDate":null,"id":1},{"firstName":"Michelle","lastName":"Chen","gmtCreated":null,"gmtModified":null,"deletedDate":null,"id":3},{"firstName":"Jason","lastName":"Chen","gmtCreated":1489214640000,"gmtModified":1489214640000,"deletedDate":1489214640000,"id":4},{"firstName":"Michelle","lastName":"Chen","gmtCreated":1489214640000,"gmtModified":1489214640000,"deletedDate":1489214640000,"id":6}]

源代码:

https://github.com/MiniSpringBootTutorial/mini_springboot

Spring家族大观园

访问,http://start.spring.io/ , 你将进入丰富多彩的Spring世界:

螢幕快照 2017-03-11 11.11.19.png
螢幕快照 2017-03-11 11.12.20.png

OK,言归正传,我们开篇第一回,Hello,World

螢幕快照 2017-03-11 11.56.45.png

https://start.spring.io中生成项目骨架:

GroupId:  com.light.sword.mini_springboot
Artifact:  mini_springboot

生成工程mini_springboot.

工程目录如下:

.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── light
    │   │           └── sword
    │   │               └── mini_springboot
    │   │                   └── MiniSpringbootApplication.java
    │   └── resources
    │       └── application.properties
    └── test
        └── java
            └── com
                └── light
                    └── sword
                        └── mini_springboot
                            └── MiniSpringbootApplicationTests.java

16 directories, 8 files


参考示例工程:

https://github.com/sdeleuze/spring-boot-kotlin-demo

参考文章:

Developing Spring Boot applications with Kotlin:
https://spring.io/blog/2016/02/15/developing-spring-boot-applications-with-kotlin

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,302评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,563评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,433评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,628评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,467评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,354评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,777评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,419评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,725评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,768评论 2 314
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,543评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,387评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,794评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,032评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,305评论 1 252
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,741评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,946评论 2 336

推荐阅读更多精彩内容