我们的理念是用极简的招数,打败绝顶"高手"。
Kotlin, Console: Hello,World
<h5> Step1. 新建gradle,kotlin工程:
螢幕快照 2017-03-11 12.40.05.png
<h5>Step2. 配置build.gradle
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制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, 你的所有的代码,工程,一切都是文件。
目录
代码语言:javascript复制.
├── 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 响应:
代码语言:javascript复制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中生成项目骨架:
代码语言:javascript复制GroupId: com.light.sword.mini_springboot
Artifact: mini_springboot
生成工程mini_springboot.
工程目录如下:
代码语言:javascript复制.
├── 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