《Springboot极简教程》 Springboot plus Kotlin :Hello,WorldKotlin, Console: Hello,WorldSpringBoot Kotlin JP

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

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏猿天地

高性能NIO框架Netty-对象传输

上篇文章高性能NIO框架Netty入门篇我们对Netty做了一个简单的介绍,并且写了一个入门的Demo,客户端往服务端发送一个字符串的消息,服务端回复一个字符串...

3088
来自专栏菩提树下的杨过

java调用.net asmx / wcf

一、先用asmx与wcf写二个.net web service: 1.1 asmx web服务:asmx-service.asmx.cs 1 using Sy...

2265
来自专栏精讲JAVA

接口方法上的注解无法被 @Aspect 声明的切面拦截的原因分析

在Spring中使用MyBatis的Mapper接口自动生成时,用一个自定义的注解标记在Mapper接口的方法中,再利用@Aspect定义一个切面,拦截这个注解...

2412
来自专栏小灰灰

Spring之RestTemplate使用小结一

作为一个Java后端,需要通过HTTP请求其他的网络资源可以说是一个比较常见的case了;一般怎么做呢?

1.2K3
来自专栏张善友的专栏

Disruptor-NET和内存栅栏

Disruptor-NET算法(是一种无锁算法)需要我们自己实现某一种特定的内存操作的语义以保证算法的正确性。这时我们就需要显式的使用一些指令来控制内存操作指令...

2176
来自专栏Java Web

JavaWeb中使用JSON

2664
来自专栏菩提树下的杨过

spring mvc4的日期/数字格式化、枚举转换

日期、数字格式化显示,是web开发中的常见需求,spring mvc采用XXXFormatter来处理,先看一个最基本的单元测试:

1433
来自专栏Java Web

JavaWeb中使用JSON

JSON 使用 JavaScript 语法来描述数据对象,但是 JSON 仍然独立于语言和平台。JSON 解析器和 JSON 库支持许多不同的编程语言。

2354
来自专栏微服务那些事儿

微服务的异常处理

不加班的周末,整理了一下项目上的异常处理方案,和小伙伴们共享,里面不成熟的代码或解决方式.QAQ,评论区走起

1.1K6
来自专栏一个会写诗的程序员的博客

8.4 Spring Boot集成Kotlin混合Java开发

本章介绍Spring Boot集成Kotlin混合Java开发一个完整的spring boot应用:Restfeel,一个企业级的Rest API接口测试平台(...

2642

扫码关注云+社区