Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spring and Kotlin #ca_kt

Taro Nagasawa
November 15, 2017

Spring and Kotlin #ca_kt

CA.kt #4 (Kotlin Conf報告会) ( https://cyberagent.connpass.com/event/70423/ )で発表した資料です。

Taro Nagasawa

November 15, 2017
Tweet

More Decks by Taro Nagasawa

Other Decks in Programming

Transcript

  1. 自己紹介 • 長澤 太郎(たろーって呼んでね) • @ngsw_taro • エムスリー株式会社 • ディズニーが大好き!

    エムスリーは 国内最大規模の 医療情報プラット フォームを開発・ 運営しています。 エンジニア募 集中!
  2. サーバサイドKotlin キテます! 44セッション中... • サーバサイドKotlin: 5個(内、Spring関連 3個) • Android: 4個

    • Native(iOS含む): 3個 • JS: 2個 • その他プラットフォーム: 3個 • その他(言語機能、ベストプラクティスなど): 28個
  3. サーバサイドKotlin キテます! 44セッション中... • サーバサイドKotlin: 5個(内、Spring関連 3個) • Android: 4個

    • Native(iOS含む): 3個 • JS: 2個 • その他プラットフォーム: 3個 • その他(言語機能、ベストプラクティスなど): 28個
  4. Spring Framework 5.0 リリース! • 2017/9/28にSpring Framework 5.0がリリース • Spring

    WebFluxの追加 ◦ ノンブロッキングでリアクティブなWebフレームワーク ◦ 従来のアノテーションによるハンドラの宣言に加え ◦ ラムダを使用したルーティング • ラムダを使用したBean登録 • KotlinフレンドリなAPI群の追加
  5. 基本 data class User(val firstName: String, val lastName: String) @SpringBootApplication

    class DemoApplication { @Bean fun objectMapperBuilder() = Jackson2ObjectMapperBuilder() .propertyNamingStrategy(SNAKE_CASE) } fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } @RestController @RequestMapping("users") class UserController { @GetMapping fun index() = listOf(User("Taro", "Nagasawa")) }
  6. 基本 data class User(val firstName: String, val lastName: String) @SpringBootApplication

    class DemoApplication { @Bean fun objectMapperBuilder() = Jackson2ObjectMapperBuilder() .propertyNamingStrategy(SNAKE_CASE) } fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } @RestController @RequestMapping("users") class UserController { @GetMapping fun index() = listOf(User("Taro", "Nagasawa")) }
  7. 基本 data class User(val firstName: String, val lastName: String) @SpringBootApplication

    class DemoApplication { @Bean fun objectMapperBuilder() = Jackson2ObjectMapperBuilder() .propertyNamingStrategy(SNAKE_CASE) } fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } @RestController @RequestMapping("users") class UserController { @GetMapping fun index() = listOf(User("Taro", "Nagasawa")) }
  8. 基本 data class User(val firstName: String, val lastName: String) @SpringBootApplication

    class DemoApplication { @Bean fun objectMapperBuilder() = Jackson2ObjectMapperBuilder() .propertyNamingStrategy(SNAKE_CASE) } fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } @RestController @RequestMapping("users") class UserController { @GetMapping fun index() = listOf(User("Taro", "Nagasawa")) }
  9. 基本 data class User(val firstName: String, val lastName: String) @SpringBootApplication

    class DemoApplication { @Bean fun objectMapperBuilder() = Jackson2ObjectMapperBuilder() .propertyNamingStrategy(SNAKE_CASE) } fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } @RestController @RequestMapping("users") class UserController { @GetMapping fun index() = listOf(User("Taro", "Nagasawa")) }
  10. 基本 data class User(val firstName: String, val lastName: String) @SpringBootApplication

    class DemoApplication { @Bean fun objectMapperBuilder() = Jackson2ObjectMapperBuilder() .propertyNamingStrategy(SNAKE_CASE) } fun main(args: Array<String>) { runApplication<DemoApplication>(*args) } @RestController @RequestMapping("users") class UserController { @GetMapping fun index() = listOf(User("Taro", "Nagasawa")) }
  11. ラムダを使用したbean登録 (1) @SpringBootApplication class DemoApplication fun main(args: Array<String>) { SpringApplicationBuilder()

    .sources(DemoApplication::class.java) .initializers(beans { bean { Jackson2ObjectMapperBuilder() .propertyNamingStrategy(SNAKE_CASE) } }) .run(*args) }
  12. ラムダを使用したbean登録 (1) @SpringBootApplication class DemoApplication fun main(args: Array<String>) { SpringApplicationBuilder()

    .sources(DemoApplication::class.java) .initializers(beans { bean { Jackson2ObjectMapperBuilder() .propertyNamingStrategy(SNAKE_CASE) } }) .run(*args) }
  13. ラムダを使用したbean登録 (1) @SpringBootApplication class DemoApplication fun main(args: Array<String>) { SpringApplicationBuilder()

    .sources(DemoApplication::class.java) .initializers(beans { bean { Jackson2ObjectMapperBuilder() .propertyNamingStrategy(SNAKE_CASE) } }) .run(*args) }
  14. ラムダを使用したbean登録 (1) @SpringBootApplication class DemoApplication fun main(args: Array<String>) { SpringApplicationBuilder()

    .sources(DemoApplication::class.java) .initializers(beans { bean { Jackson2ObjectMapperBuilder() .propertyNamingStrategy(SNAKE_CASE) } }) .run(*args) }
  15. ラムダを使用したbean登録 (2) beans { bean { Foo() } bean {

    Bar(ref<Foo>()) } } @Bean fun foo(): Foo = Foo() @Bean fun bar(foo: Foo): Bar = Bar(foo)
  16. ラムダを使用したbean登録 (2) beans { bean { Foo() } bean {

    Bar(ref<Foo>()) } } @Bean fun foo(): Foo = Foo() @Bean fun bar(foo: Foo): Bar = Bar(foo) 推論できれば省略可
  17. WebFlux @RestController @RequestMapping("users") class UserController { @GetMapping fun index(): Mono<List<User>>

    = Mono.just(listOf(User("Taro", "Nagasawa"))) } @RestController @RequestMapping("users") class UserController { @GetMapping fun index():List<User> = listOf(User("Taro", "Nagasawa")) }
  18. WebFlux @RestController @RequestMapping("users") class UserController { @GetMapping fun index(): Mono<List<User>>

    = Mono.just(listOf(User("Taro", "Nagasawa"))) } @RestController @RequestMapping("users") class UserController { @GetMapping fun index():List<User> = listOf(User("Taro", "Nagasawa")) } MonoやFluxを返すだけ
  19. ラムダによるルーティング data class User(val firstName: String, val lastName: String) @SpringBootApplication

    class DemoApplication fun main(args: Array<String>) { SpringApplicationBuilder() .sources(DemoApplication::class.java) .initializers(beans { bean { /* Jacksonの設定 */ } bean { router { GET("/users") { val users = listOf(User("Taro", "Nagasawa")) ServerResponse.ok().body(Mono.just(users)) } } } }) .run(*args) }
  20. ラムダによるルーティング data class User(val firstName: String, val lastName: String) @SpringBootApplication

    class DemoApplication fun main(args: Array<String>) { SpringApplicationBuilder() .sources(DemoApplication::class.java) .initializers(beans { bean { /* Jacksonの設定 */ } bean { router { GET("/users") { val users = listOf(User("Taro", "Nagasawa")) ServerResponse.ok().body(Mono.just(users)) } } } }) .run(*args) }
  21. おまけ: Exposed • Kotlin SQLライブラリ ◦ SQL DSL ◦ Data

    Access Object • JetBrainsが開発 https://github.com/JetBrains/Exposed • ver. 0.8.9 • 有名なDBMSに対応: ポスグレ, MySQL, Oracle, ...
  22. SQL DSL object Teams: Table() { val id = long("id").autoIncrement().primaryKey()

    val name = varchar("name", length = 100) } fun main(args: Array<String>) { Database.connect("jdbc:h2:mem:test", "org.h2.Driver") transaction { SchemaUtils.create(Teams) Teams.insert { it[name] = "Kotlin" } Teams.selectAll().forEach { row -> println("id=${row[Teams.id]}: ${row[Teams.name]}") // id=1: Kotlin } SchemaUtils.drop(Teams) } }
  23. SQL DSL object Teams: Table() { val id = long("id").autoIncrement().primaryKey()

    val name = varchar("name", length = 100) } fun main(args: Array<String>) { Database.connect("jdbc:h2:mem:test", "org.h2.Driver") transaction { SchemaUtils.create(Teams) Teams.insert { it[name] = "Kotlin" } Teams.selectAll().forEach { row -> println("id=${row[Teams.id]}: ${row[Teams.name]}") // id=1: Kotlin } SchemaUtils.drop(Teams) } } テーブル定義
  24. SQL DSL object Teams: Table() { val id = long("id").autoIncrement().primaryKey()

    val name = varchar("name", length = 100) } fun main(args: Array<String>) { Database.connect("jdbc:h2:mem:test", "org.h2.Driver") transaction { SchemaUtils.create(Teams) Teams.insert { it[name] = "Kotlin" } Teams.selectAll().forEach { row -> println("id=${row[Teams.id]}: ${row[Teams.name]}") // id=1: Kotlin } SchemaUtils.drop(Teams) } } DBに接続
  25. SQL DSL object Teams: Table() { val id = long("id").autoIncrement().primaryKey()

    val name = varchar("name", length = 100) } fun main(args: Array<String>) { Database.connect("jdbc:h2:mem:test", "org.h2.Driver") transaction { SchemaUtils.create(Teams) Teams.insert { it[name] = "Kotlin" } Teams.selectAll().forEach { row -> println("id=${row[Teams.id]}: ${row[Teams.name]}") // id=1: Kotlin } SchemaUtils.drop(Teams) } } トランザクション
  26. SQL DSL object Teams: Table() { val id = long("id").autoIncrement().primaryKey()

    val name = varchar("name", length = 100) } fun main(args: Array<String>) { Database.connect("jdbc:h2:mem:test", "org.h2.Driver") transaction { SchemaUtils.create(Teams) Teams.insert { it[name] = "Kotlin" } Teams.selectAll().forEach { row -> println("id=${row[Teams.id]}: ${row[Teams.name]}") // id=1: Kotlin } SchemaUtils.drop(Teams) } } テーブル作成 テーブル削除
  27. SQL DSL object Teams: Table() { val id = long("id").autoIncrement().primaryKey()

    val name = varchar("name", length = 100) } fun main(args: Array<String>) { Database.connect("jdbc:h2:mem:test", "org.h2.Driver") transaction { SchemaUtils.create(Teams) Teams.insert { it[name] = "Kotlin" } Teams.selectAll().forEach { row -> println("id=${row[Teams.id]}: ${row[Teams.name]}") // id=1: Kotlin } SchemaUtils.drop(Teams) } } インサート
  28. SQL DSL object Teams: Table() { val id = long("id").autoIncrement().primaryKey()

    val name = varchar("name", length = 100) } fun main(args: Array<String>) { Database.connect("jdbc:h2:mem:test", "org.h2.Driver") transaction { SchemaUtils.create(Teams) Teams.insert { it[name] = "Kotlin" } Teams.selectAll().forEach { row -> println("id=${row[Teams.id]}: ${row[Teams.name]}") // id=1: Kotlin } SchemaUtils.drop(Teams) } } 取得
  29. 一対多 関連 object Teams: Table() { val id = long("id").autoIncrement().primaryKey()

    val name = varchar("name", length = 100) } object Tasks: Table() { val id = long("id").autoIncrement().primaryKey() val content = text("content") val completedAt = datetime("completed_at").nullable() val teamId = long("team_id") references Teams.id }
  30. 一対多 関連 object Teams: Table() { val id = long("id").autoIncrement().primaryKey()

    val name = varchar("name", length = 100) } object Tasks: Table() { val id = long("id").autoIncrement().primaryKey() val content = text("content") val completedAt = datetime("completed_at").nullable() val teamId = long("team_id") references Teams.id } 外部キー設定
  31. joinとprojectionとwhere Tasks.innerJoin(Teams) .slice(Team.name, Tasks.content) .select { Tasks.completedAt.isNull() } .forEach {

    row -> println("${row[Teams.name]}: ${row[Tasks.content]}") } id name 1 Kotlin id content completed_at team_id 1 release 1.1.60 2017-11-14T03:00+09:00 1 2 release 1.2.0 (null) 1 Teams Tasks Teams.name Tasks.content Kotlin release 1.2.0
  32. Data Access Object object Teams: LongIdTable() { val name =

    varchar("name", length = 100) } class Team(id: EntityID<Long>): LongEntity(id) { companion object: LongEntityClass<Team>(Teams) var name by Teams.name } fun main(args: Array<String>) { Database.connect("jdbc:h2:mem:test", "org.h2.Driver") transaction { SchemaUtils.create(Teams) Team.new { name = "Kotlin" } Team.all().forEach { team: Team -> println("id=${team.id}, name=${team.name}") } } }
  33. Data Access Object object Teams: LongIdTable() { val name =

    varchar("name", length = 100) } class Team(id: EntityID<Long>): LongEntity(id) { companion object: LongEntityClass<Team>(Teams) var name by Teams.name } fun main(args: Array<String>) { Database.connect("jdbc:h2:mem:test", "org.h2.Driver") transaction { SchemaUtils.create(Teams) Team.new { name = "Kotlin" } Team.all().forEach { team: Team -> println("id=${team.id}, name=${team.name}") } } }
  34. Data Access Object object Teams: LongIdTable() { val name =

    varchar("name", length = 100) } class Team(id: EntityID<Long>): LongEntity(id) { companion object: LongEntityClass<Team>(Teams) var name by Teams.name } fun main(args: Array<String>) { Database.connect("jdbc:h2:mem:test", "org.h2.Driver") transaction { SchemaUtils.create(Teams) Team.new { name = "Kotlin" } Team.all().forEach { team: Team -> println("id=${team.id}, name=${team.name}") } } }
  35. 一対多 関連 object Teams: LongIdTable() { val name = varchar("name",

    length = 100) } object Tasks: LongIdTable() { val content: text("content") val completedAt: datetime("completed_at").nullable() val team = reference("team", Teams) } class Team(id: EntityID<Long>): LongEntity(id) { companion object: LongEntityClass<Team>(Teams) var name by Teams.name val tasks by Tasks.referrersOn(Tasks.team) } class Task(id: EntityID<Long>): LongEntity(id) { companion object: LongEntityClass<Task>(Tasks) var content by Tasks.content var completedAt by Tasks.completedAt var team by Team.referencedOn(Tasks.team) }
  36. 一対多 関連 object Teams: LongIdTable() { val name = varchar("name",

    length = 100) } object Tasks: LongIdTable() { val content: text("content") val completedAt: datetime("completed_at").nullable() val team = reference("team", Teams) } class Team(id: EntityID<Long>): LongEntity(id) { companion object: LongEntityClass<Team>(Teams) var name by Teams.name val tasks by Tasks.referrersOn(Tasks.team) } class Task(id: EntityID<Long>): LongEntity(id) { companion object: LongEntityClass<Task>(Tasks) var content by Tasks.content var completedAt by Tasks.completedAt var team by Team.referencedOn(Tasks.team) } 関連を定義
  37. 取得 Task .find { Tasks.completedAt.isNull() } .forEach { task: Task

    -> val team: Team = task.team println("${team.name}: ${task.content}") }
  38. 取得 Task .find { Tasks.completedAt.isNull() } .forEach { task: Task

    -> val team: Team = task.team println("${team.name}: ${task.content}") } N + 1 のおそれ