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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 我们的理念是用极简的招数,打败绝顶"高手"。Kotlin, Console: Hello,World Step1. 新建gradle,kotlin工程:螢幕快照 2017-03-11 12.

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

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

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
前端开发 Java Linux
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(三)
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(三)
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(三)
|
NoSQL 数据可视化 关系型数据库
SpringBoot 多模块项目打包部署保姆级教程
SpringBoot 多模块项目打包部署保姆级教程
990 0
SpringBoot 多模块项目打包部署保姆级教程
|
SQL Java 数据库连接
SpringBoot2.x系列教程31--SpringBoot整合H2内存数据库实现CRUD操作
前言 在上一章节中,我给大家详细讲解了H2数据库的安装配置,接下来我就带大家在Spring Boot中整合H2数据库,实现代码操作。 一. 以内嵌模式整合H2数据库 1. 创建Web项目 我们按照之前的经验,创建一个SpringBoot的Web程序,具体过程略,请参考下图创建。 2. 添加依赖包 然后在pom.xml文件中添加如下依赖包。 <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <de
1127 1
|
Java 开发工具 Spring
springBoot搭建自己的第一个接口(kotlin+gradle)
springBoot搭建自己的第一个接口(kotlin+gradle)
207 0
springBoot搭建自己的第一个接口(kotlin+gradle)
|
Java Kotlin
springBoot引入kotlin模块
当时创建springboot的时候没有选择kotlin部分,后面想要运行kotlin文件显示,不能编译,所以需要手动引入一些依赖,idea当时确实有提示过需要引入kotlin‘模块,不过由于不能下载最新版本1.6.0-M1,卡了好久,所以换成低一点的版本
142 0
|
存储 安全 Java
SpringBoot整合SpringSecurity完整教程
SpringBoot整合SpringSecurity完整教程
SpringBoot整合SpringSecurity完整教程
|
缓存 Java 开发者
《SpringBoot篇》18.SpringBoot整合Memcached缓存超详细教程(二)
《SpringBoot篇》18.SpringBoot整合Memcached缓存超详细教程(二)
《SpringBoot篇》18.SpringBoot整合Memcached缓存超详细教程(二)
|
存储 缓存 NoSQL
《SpringBoot篇》18.SpringBoot整合Memcached缓存超详细教程(一)
《SpringBoot篇》18.SpringBoot整合Memcached缓存超详细教程(一)
《SpringBoot篇》18.SpringBoot整合Memcached缓存超详细教程(一)
|
Java 索引 容器
《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(三)
《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(三)
《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(三)
|
NoSQL Java MongoDB
《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(二)
《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(二)
《SpringBoot篇》16.SpringBoot整合Elasticsearch超详细教程(二)