├── .gitignore
├── README.md
├── pom.xml
└── src
├── main
├── kotlin
│ └── com
│ │ └── lianggzone
│ │ └── demo
│ │ └── kotlin
│ │ ├── SpringKotlinApplication.kt
│ │ ├── config
│ │ └── BeanConfig.kt
│ │ ├── controller
│ │ ├── AuthorController.kt
│ │ └── DemoController.kt
│ │ ├── dao
│ │ ├── AuthorDao.kt
│ │ └── AuthorDaoImpl.kt
│ │ ├── entity
│ │ └── Author.kt
│ │ └── service
│ │ ├── AuthorService.kt
│ │ └── AuthorServiceImpl.kt
└── resources
│ ├── application.properties
│ └── config
│ └── source.properties
└── test
├── kotlin
└── com
│ └── lianggzone
│ └── demo
│ └── kotlin
│ └── SpringKotlinApplicationTests.kt
└── resources
└── api.http
/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 本项目内容为 《Spring Boot 揭秘与实战》系列,汇总文集。如您觉得该项目对您有用,欢迎点击右上方的 Star 按钮,给予支持!!
2 |
3 | - 博客:http://blog.720ui.com
4 | - GitHub:https://github.com/lianggzone/spring-kotlin-samples
5 | - GitHub:https://github.com/lianggzone/springboot-action
6 |
7 |
8 |
9 | 
10 |
11 | ## 文章
12 |
13 | > SpringBoot 与 Kotlin 完美交融 : [SpringBoot 与 Kotlin 完美交融](http://blog.720ui.com/2018/springboot2_kotlin_prime/)
14 | >
15 | > Spring Boot 揭秘与实战 系列 : [Spring Boot 揭秘与实战 系列](http://blog.720ui.com/columns/springboot_all/)
16 |
17 | ## 源代码
18 |
19 | > 相关示例完整代码 : [spring-kotlin-samples](https://github.com/lianggzone/spring-kotlin-samples.git)
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.lianggonde.demo
7 | spring-kotlin-samples
8 | 0.0.1-SNAPSHOT
9 | jar
10 | spring-kotlin-samples
11 |
12 |
13 |
14 | lianggzone
15 | 梁桂钊
16 | lianggzone@163.com
17 |
18 | Architect
19 | Developer
20 |
21 |
22 |
23 |
24 |
25 | org.springframework.boot
26 | spring-boot-starter-parent
27 | 2.0.2.RELEASE
28 |
29 |
30 |
31 |
32 | UTF-8
33 | UTF-8
34 | 1.8
35 | 1.2.41
36 |
37 |
38 |
39 |
40 | org.springframework.boot
41 | spring-boot-starter
42 |
43 |
44 | org.springframework.boot
45 | spring-boot-starter-web
46 |
47 |
48 | org.springframework.boot
49 | spring-boot-starter-test
50 | test
51 |
52 |
53 |
54 | org.springframework.boot
55 | spring-boot-starter-jdbc
56 |
57 |
58 | mysql
59 | mysql-connector-java
60 | 5.1.35
61 |
62 |
63 | com.alibaba
64 | druid
65 | 1.0.14
66 |
67 |
68 |
69 | org.jetbrains.kotlin
70 | kotlin-stdlib-jdk8
71 |
72 |
73 | org.jetbrains.kotlin
74 | kotlin-reflect
75 |
76 |
77 | org.jetbrains.kotlin
78 | kotlin-stdlib
79 |
80 |
81 |
82 | com.alibaba
83 | fastjson
84 | 1.2.6
85 |
86 |
87 |
88 |
89 |
90 | ${project.basedir}/src/main/kotlin
91 | ${project.basedir}/src/test/kotlin
92 |
93 |
94 | org.springframework.boot
95 | spring-boot-maven-plugin
96 |
97 |
98 | kotlin-maven-plugin
99 | org.jetbrains.kotlin
100 | ${kotlin.version}
101 |
102 |
103 | compile
104 | compile
105 |
106 |
107 | test-compile
108 | test-compile
109 |
110 |
111 |
112 |
113 | org.jetbrains.kotlin
114 | kotlin-maven-noarg
115 | ${kotlin.version}
116 |
117 |
118 | org.jetbrains.kotlin
119 | kotlin-maven-allopen
120 | ${kotlin.version}
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
--------------------------------------------------------------------------------
/src/main/kotlin/com/lianggzone/demo/kotlin/SpringKotlinApplication.kt:
--------------------------------------------------------------------------------
1 | package com.lianggzone.demo.kotlin
2 |
3 | import org.springframework.boot.SpringApplication
4 | import org.springframework.boot.autoconfigure.SpringBootApplication
5 | import org.springframework.boot.runApplication
6 |
7 | @SpringBootApplication(scanBasePackages = ["com.lianggzone.demo.kotlin"])
8 | open class SpringKotlinApplication{
9 | fun main(args: Array) {
10 | SpringApplication.run(SpringKotlinApplication::class.java, *args)
11 | }
12 | }
13 |
14 |
15 |
--------------------------------------------------------------------------------
/src/main/kotlin/com/lianggzone/demo/kotlin/config/BeanConfig.kt:
--------------------------------------------------------------------------------
1 | package com.lianggzone.demo.kotlin.config
2 |
3 | import com.alibaba.druid.pool.DruidDataSource
4 | import org.springframework.beans.factory.annotation.Autowired
5 | import org.springframework.context.annotation.Bean
6 | import org.springframework.context.annotation.Configuration
7 | import org.springframework.context.annotation.PropertySource
8 | import org.springframework.core.env.Environment
9 | import org.springframework.jdbc.core.JdbcTemplate
10 | import org.springframework.transaction.annotation.EnableTransactionManagement
11 | import javax.sql.DataSource
12 |
13 | @Configuration
14 | @EnableTransactionManagement
15 | @PropertySource(value = *arrayOf("classpath:config/source.properties"))
16 | open class BeanConfig {
17 |
18 | @Autowired
19 | private lateinit var env: Environment
20 |
21 | @Bean
22 | open fun dataSource(): DataSource {
23 | val dataSource = DruidDataSource()
24 | dataSource.driverClassName = env!!.getProperty("source.driverClassName").trim()
25 | dataSource.url = env.getProperty("source.url").trim()
26 | dataSource.username = env.getProperty("source.username").trim()
27 | dataSource.password = env.getProperty("source.password").trim()
28 | return dataSource
29 | }
30 |
31 | @Bean
32 | open fun jdbcTemplate(): JdbcTemplate {
33 | val jdbcTemplate = JdbcTemplate()
34 | jdbcTemplate.dataSource = dataSource()
35 | return jdbcTemplate
36 | }
37 | }
--------------------------------------------------------------------------------
/src/main/kotlin/com/lianggzone/demo/kotlin/controller/AuthorController.kt:
--------------------------------------------------------------------------------
1 | package com.lianggzone.demo.kotlin.controller
2 |
3 | import com.alibaba.fastjson.JSONObject
4 | import com.lianggzone.demo.kotlin.entity.Author
5 | import com.lianggzone.demo.kotlin.service.AuthorService
6 | import org.springframework.beans.factory.annotation.Autowired
7 | import org.springframework.web.bind.annotation.*
8 | import java.util.*
9 | import javax.servlet.http.HttpServletRequest
10 |
11 | @RestController
12 | @RequestMapping(value = "/authors")
13 | class AuthorController {
14 |
15 | @Autowired
16 | private lateinit var authorService: AuthorService
17 |
18 | /**
19 | * 查询用户列表
20 | */
21 | @RequestMapping(method = [RequestMethod.GET])
22 | fun getAuthorList(request: HttpServletRequest): Map {
23 | val authorList = this.authorService.findAuthorList()
24 | val param = HashMap()
25 | param["total"] = authorList.size
26 | param["rows"] = authorList
27 | return param
28 | }
29 |
30 | /**
31 | * 查询用户信息
32 | */
33 | @RequestMapping(value = "/{userId:\\d+}", method = [RequestMethod.GET])
34 | fun getAuthor(@PathVariable userId: Long, request: HttpServletRequest): Author {
35 | return authorService.findAuthor(userId) ?: throw RuntimeException("查询错误")
36 | }
37 |
38 | /**
39 | * 新增方法
40 | */
41 | @RequestMapping(method = [RequestMethod.POST])
42 | fun add(@RequestBody jsonObject: JSONObject) {
43 | val userId = jsonObject.getString("user_id")
44 | val realName = jsonObject.getString("real_name")
45 | val nickName = jsonObject.getString("nick_name")
46 |
47 | val author = Author()
48 | author.id = java.lang.Long.valueOf(userId)
49 | author.realName = realName
50 | author.nickName = nickName
51 | try {
52 | this.authorService.add(author)
53 | } catch (e: Exception) {
54 | throw RuntimeException("新增错误")
55 | }
56 | }
57 |
58 | /**
59 | * 更新方法
60 | */
61 | @RequestMapping(value = "/{userId:\\d+}", method = [RequestMethod.PUT])
62 | fun update(@PathVariable userId: Long, @RequestBody jsonObject: JSONObject) {
63 | var author = this.authorService.findAuthor(userId)
64 | val realName = jsonObject.getString("real_name")
65 | val nickName = jsonObject.getString("nick_name")
66 | try {
67 | if (author != null) {
68 | author.realName = realName
69 | author.nickName = nickName
70 | this.authorService.update(author)
71 | }
72 | } catch (e: Exception) {
73 | throw RuntimeException("更新错误")
74 | }
75 |
76 | }
77 |
78 | /**
79 | * 删除方法
80 | */
81 | @RequestMapping(value = "/{userId:\\d+}", method = [RequestMethod.DELETE])
82 | fun delete(@PathVariable userId: Long) {
83 | try {
84 | this.authorService.delete(userId)
85 | } catch (e: Exception) {
86 | throw RuntimeException("删除错误")
87 | }
88 | }
89 | }
--------------------------------------------------------------------------------
/src/main/kotlin/com/lianggzone/demo/kotlin/controller/DemoController.kt:
--------------------------------------------------------------------------------
1 | package com.lianggzone.demo.kotlin.controller
2 |
3 | import org.springframework.boot.SpringApplication
4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration
5 | import org.springframework.web.bind.annotation.RequestMapping
6 | import org.springframework.web.bind.annotation.RestController
7 |
8 | @RestController
9 | @EnableAutoConfiguration
10 | class DemoController {
11 |
12 | @RequestMapping("/")
13 | fun home(): String {
14 | return "Hello World!"
15 | }
16 | }
--------------------------------------------------------------------------------
/src/main/kotlin/com/lianggzone/demo/kotlin/dao/AuthorDao.kt:
--------------------------------------------------------------------------------
1 | package com.lianggzone.demo.kotlin.dao
2 |
3 | import com.lianggzone.demo.kotlin.entity.Author
4 |
5 | interface AuthorDao {
6 | fun add(author: Author): Int
7 | fun update(author: Author): Int
8 | fun delete(id: Long): Int
9 | fun findAuthor(id: Long): Author?
10 | fun findAuthorList(): List
11 | }
--------------------------------------------------------------------------------
/src/main/kotlin/com/lianggzone/demo/kotlin/dao/AuthorDaoImpl.kt:
--------------------------------------------------------------------------------
1 | package com.lianggzone.demo.kotlin.dao
2 |
3 | import com.lianggzone.demo.kotlin.entity.Author
4 | import org.springframework.jdbc.core.BeanPropertyRowMapper
5 | import org.springframework.jdbc.core.JdbcTemplate
6 | import org.springframework.beans.factory.annotation.Autowired
7 | import org.springframework.stereotype.Repository
8 |
9 |
10 | @Repository
11 | open class AuthorDaoImpl : AuthorDao {
12 |
13 | @Autowired
14 | private lateinit var jdbcTemplate: JdbcTemplate
15 |
16 | override fun add(author: Author): Int {
17 | return jdbcTemplate.update("insert into t_author(real_name, nick_name) values(?, ?)",
18 | author.realName, author.nickName)
19 | }
20 |
21 | override fun update(author: Author): Int {
22 | return jdbcTemplate.update("update t_author set real_name = ?, nick_name = ? where id = ?",
23 | *arrayOf(author.realName, author.nickName, author.id))
24 | }
25 |
26 | override fun delete(id: Long): Int {
27 | return jdbcTemplate.update("delete from t_author where id = ?", id)
28 | }
29 |
30 | override fun findAuthor(id: Long): Author? {
31 | val list = jdbcTemplate.query("select * from t_author where id = ?",
32 | arrayOf(id), BeanPropertyRowMapper(Author::class.java))
33 | return list?.get(0);
34 | }
35 |
36 | override fun findAuthorList(): List {
37 | return jdbcTemplate.query("select * from t_author", arrayOf(), BeanPropertyRowMapper(Author::class.java))
38 | }
39 | }
--------------------------------------------------------------------------------
/src/main/kotlin/com/lianggzone/demo/kotlin/entity/Author.kt:
--------------------------------------------------------------------------------
1 | package com.lianggzone.demo.kotlin.entity
2 |
3 | class Author {
4 | var id: Long? = null
5 | var realName: String? = null
6 | var nickName: String? = null
7 | }
--------------------------------------------------------------------------------
/src/main/kotlin/com/lianggzone/demo/kotlin/service/AuthorService.kt:
--------------------------------------------------------------------------------
1 | package com.lianggzone.demo.kotlin.service
2 |
3 | import com.lianggzone.demo.kotlin.entity.Author
4 |
5 | interface AuthorService {
6 | fun add(author: Author): Int
7 | fun update(author: Author): Int
8 | fun delete(id: Long): Int
9 | fun findAuthor(id: Long): Author?
10 | fun findAuthorList(): List
11 | }
--------------------------------------------------------------------------------
/src/main/kotlin/com/lianggzone/demo/kotlin/service/AuthorServiceImpl.kt:
--------------------------------------------------------------------------------
1 | package com.lianggzone.demo.kotlin.service
2 |
3 | import com.lianggzone.demo.kotlin.entity.Author
4 | import com.oracle.util.Checksums.update
5 | import com.lianggzone.demo.kotlin.dao.AuthorDao
6 | import org.springframework.beans.factory.annotation.Autowired
7 | import org.springframework.stereotype.Service
8 |
9 | @Service("authorService")
10 | open class AuthorServiceImpl : AuthorService {
11 |
12 | @Autowired
13 | private lateinit var authorDao: AuthorDao
14 |
15 | override fun update(author: Author): Int {
16 | return this.authorDao.update(author)
17 | }
18 |
19 | override fun add(author: Author): Int {
20 | return this.authorDao.add(author)
21 | }
22 |
23 | override fun delete(id: Long): Int {
24 | return this.authorDao.delete(id)
25 | }
26 |
27 | override fun findAuthor(id: Long): Author? {
28 | return this.authorDao.findAuthor(id)
29 | }
30 |
31 | override fun findAuthorList(): List {
32 | return this.authorDao.findAuthorList()
33 | }
34 | }
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | #spring.datasource.driver-class-name=com.mysql.jdbc.Driver
2 | #spring.datasource.url=jdbc:mysql://localhost:3307/springboot_db
3 | #spring.datasource.username=root
4 | #spring.datasource.password=root
--------------------------------------------------------------------------------
/src/main/resources/config/source.properties:
--------------------------------------------------------------------------------
1 | # mysql
2 | source.driverClassName = com.mysql.jdbc.Driver
3 | source.url = jdbc:mysql://localhost:3306/springboot_db?useUnicode = true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
4 | source.username = root
5 | source.password = root
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/src/test/kotlin/com/lianggzone/demo/kotlin/SpringKotlinApplicationTests.kt:
--------------------------------------------------------------------------------
1 | package com.lianggzone.demo.kotlin
2 |
3 | import org.junit.Test
4 | import org.junit.runner.RunWith
5 | import org.springframework.boot.test.context.SpringBootTest
6 | import org.springframework.test.context.junit4.SpringRunner
7 |
8 | @RunWith(SpringRunner::class)
9 | @SpringBootTest
10 | class SpringKotlinApplicationTests {
11 |
12 | @Test
13 | fun contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/src/test/resources/api.http:
--------------------------------------------------------------------------------
1 | ### 查询用户列表
2 | GET http://localhost:8080/authors
3 | Accept : application/json
4 | Content-Type : application/json;charset=UTF-8
5 |
6 | ### 查询用户信息
7 | GET http://localhost:8080/authors/15
8 | Accept : application/json
9 | Content-Type : application/json;charset=UTF-8
10 |
11 | ### 新增方法
12 | POST http://localhost:8080/authors
13 | Content-Type: application/json
14 |
15 | {
16 | "user_id": "21",
17 | "real_name": "梁桂钊",
18 | "nick_name": "梁桂钊"
19 | }
20 |
21 | ### 更新方法
22 | PUT http://localhost:8080/authors/21
23 | Content-Type: application/json
24 |
25 | {
26 | "real_name" : "lianggzone",
27 | "nick_name": "lianggzone"
28 | }
29 |
30 |
31 | ### 删除方法
32 | DELETE http://localhost:8080/authors/21
33 | Accept : application/json
34 | Content-Type : application/json;charset=UTF-8
35 |
36 | ###
--------------------------------------------------------------------------------