|
1 | | -================== |
2 | | -Kotlin サポート |
3 | | -================== |
| 1 | +============== |
| 2 | +Kotlin support |
| 3 | +============== |
4 | 4 |
|
5 | | -.. contents:: 目次 |
| 5 | +.. contents:: |
6 | 6 | :depth: 3 |
7 | 7 |
|
8 | | -Doma は `Kotlin <https://kotlinlang.org/>`_ 1.1.2を実験的にサポートしています。 |
| 8 | +Doma supports `Kotlin <https://kotlinlang.org/>`_ 1.3.11 or above **experimentally**. |
9 | 9 |
|
10 | | -Kotlin利用のベストプラクティス |
11 | | -================================ |
| 10 | +Best practices |
| 11 | +============== |
12 | 12 |
|
13 | | -クラスの定義やビルドに関する事柄について推奨する方法を記載します。 |
| 13 | +We show you recommended ways to define classes and build them with Kotlin. |
14 | 14 |
|
15 | | -エンティティクラス |
16 | | -------------------- |
| 15 | +Entity classes |
| 16 | +-------------- |
17 | 17 |
|
18 | | -* Data Classで定義する |
19 | | -* イミュータブルで定義する( `@Entity` の `immutable` 要素に `true` を設定する) |
20 | | -* コンストラクタは1つだけ定義する |
21 | | -* コンストラクタ以外でプロパティを定義しない |
22 | | -* コンストラクタで定義するプロパティには `val` を使用する |
| 18 | +* Define as a data class |
| 19 | +* Specify ``true`` to the ``immutable`` element of ``@Entity`` |
| 20 | +* Define only one constructor |
| 21 | +* Define properties only in the constructor |
| 22 | +* Use `val` for the property definitions |
23 | 23 |
|
24 | 24 | .. code-block:: java |
25 | 25 |
|
26 | 26 | @Entity(immutable = true) |
27 | 27 | data class Person( |
28 | 28 | @Id |
29 | | - @GeneratedValue(strategy = org.seasar.doma.GenerationType.IDENTITY) |
| 29 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
30 | 30 | val id: Int? = null, |
31 | 31 | val name: Name, |
32 | 32 | val address: Address) |
33 | 33 |
|
34 | | -ドメインクラス |
35 | | -------------------- |
| 34 | +Domain classes |
| 35 | +-------------- |
36 | 36 |
|
37 | | -* Data Classで定義する |
38 | | -* コンストラクタは1つだけ定義する |
39 | | -* コンストラクタで定義するプロパティの名前は `value` にする |
40 | | -* コンストラクタで定義するプロパティには `val` を使用する |
| 37 | +* Define as a data class |
| 38 | +* Define only one constructor |
| 39 | +* Define only one property whose name is ``value`` in the constructor |
| 40 | +* Use `val` for the property definition |
41 | 41 |
|
42 | 42 | .. code-block:: java |
43 | 43 |
|
44 | 44 | @Domain(valueType = String::class) |
45 | 45 | data class Name(val value: String) |
46 | 46 |
|
47 | | -エンベッダブルクラス |
48 | | ----------------------- |
| 47 | +Embeddable classes |
| 48 | +------------------ |
49 | 49 |
|
50 | | -* Data Classで定義する |
51 | | -* コンストラクタは1つだけ定義する |
52 | | -* コンストラクタ以外でプロパティを定義しない |
53 | | -* コンストラクタで定義するプロパティには `val` を使用する |
| 50 | +* Define as a data class |
| 51 | +* Define only one constructor |
| 52 | +* Define properties only in the constructor |
| 53 | +* Use `val` for the property definitions |
54 | 54 |
|
55 | 55 | .. code-block:: java |
56 | 56 |
|
57 | 57 | @Embeddable |
58 | 58 | data class Address(val city: String, val street: String) |
59 | 59 |
|
60 | | -Daoインタフェース |
61 | | -------------------- |
| 60 | +Dao interfaces |
| 61 | +-------------- |
62 | 62 |
|
63 | | -* KotlinではなくJavaで定義する |
64 | | -* 更新処理の戻り値の型は `org.seasar.doma.jdbc.Result` や `org.seasar.doma.jdbc.BatchResult` を使う |
| 63 | +* Specify a SQL template to ``@org.seasar.doma.experimental.Sql`` |
| 64 | +* Use ``org.seasar.doma.jdbc.Result`` as the return type of ``@Delete``, ``@Insert`` and ``@Update`` |
| 65 | +* Use ``org.seasar.doma.jdbc.BatchResult`` as the return type of |
| 66 | + ``@BatchDelete``, ``@BatchInsert`` and ``@BatchUpdate`` |
65 | 67 |
|
66 | 68 | .. code-block:: java |
67 | 69 |
|
68 | | - @Dao(config = AppConfig.class) |
69 | | - public interface PersonDao { |
| 70 | + @Dao(config = AppConfig::class) |
| 71 | + interface PersonDao { |
| 72 | + @Sql(""" |
| 73 | + select * from person where id = /*id*/0 |
| 74 | + """) |
70 | 75 | @Select |
71 | | - Person selectById(Integer id); |
| 76 | + fun selectById(id: Int): Person |
| 77 | +
|
72 | 78 | @Insert |
73 | 79 | Result<Person> insert(Person person); |
74 | 80 | } |
75 | 81 |
|
76 | | -* 更新処理の戻り値を扱う際は `Destructuring Declarations <https://kotlinlang.org/docs/reference/multi-declarations.html>`_ を使う |
| 82 | +* Use `Destructuring Declarations <https://kotlinlang.org/docs/reference/multi-declarations.html>`_ |
| 83 | + for ``org.seasar.doma.jdbc.Result`` and ``org.seasar.doma.jdbc.BatchResult`` |
77 | 84 |
|
78 | 85 | .. code-block:: java |
79 | 86 |
|
80 | 87 | val dao: PersonDao = ... |
81 | 88 | val person = Person(name = Name("Jhon"), address = Address(city = "Tokyo", street = "Yaesu")) |
82 | 89 | val (newPerson, count) = dao.insert(person) |
83 | 90 |
|
| 91 | +Using kapt in Gradle |
| 92 | +-------------------- |
84 | 93 |
|
85 | | -kaptによるビルド |
86 | | -------------------- |
87 | | - |
88 | | -Kotlinで記述されたクラスやインタフェースに対して注釈処理をするには `kapt <https://blog.jetbrains.com/kotlin/2016/12/kotlin-1-0-6-is-here/>`_ を実行する必要があります。 |
89 | | -kaptは実験的な位置付けにありドキュメントがありません。 |
90 | | -Gradleでビルドする際は、確実な注釈処理が行われるように常に `clean build` を実行することを推奨します。 |
| 94 | +Annotation processors are supported in Kotlin with the |
| 95 | +`kapt <https://kotlinlang.org/docs/reference/kapt.html>`_ compiler plugin. |
91 | 96 |
|
92 | | -.. code-block:: sh |
| 97 | +Add the dependencies using the `kapt` and `implementation` configuration in your dependencies block: |
93 | 98 |
|
94 | | - ./gradlew clean build |
| 99 | +.. code-block:: groovy |
95 | 100 |
|
96 | | -Eclispeを利用する場合設定を適切に行えばJavaの注釈処理は自動で行われますが、kapt(Kotlinの注釈処理)はGradleを実行しない限り行われないことに注意してください。 |
| 101 | + dependencies { |
| 102 | + kapt "org.seasar.doma:doma:2.21.1-SNAPSHOT" |
| 103 | + implementation "org.seasar.doma:doma:2.21.1-SNAPSHOT" |
| 104 | + } |
97 | 105 |
|
98 | | -下記はbuild.gradleの抜粋です。コンパイル時にSQLファイルを参照するために下記の設定に特に注意してください。 |
| 106 | +If you use resource files such as SQL files, make the kapt find them: |
99 | 107 |
|
100 | 108 | .. code-block:: groovy |
101 | 109 |
|
102 | | - // コンパイルより前にSQLファイルを出力先ディレクトリにコピーするために依存関係を逆転する |
103 | | - compileJava.dependsOn processResources |
104 | | - |
105 | | - // SQLファイルなどリソースファイルの出力先ディレクトリをkaptに伝える |
106 | | - kapt { |
107 | | - arguments { |
108 | | - arg("doma.resources.dir", processResources.destinationDir) |
109 | | - } |
110 | | - } |
| 110 | + kapt { |
| 111 | + arguments { |
| 112 | + arg("doma.resources.dir", compileKotlin.destinationDir) |
| 113 | + } |
| 114 | + } |
| 115 | +
|
| 116 | + task copyDomaResources(type: Sync) { |
| 117 | + from sourceSets.main.resources.srcDirs |
| 118 | + into compileKotlin.destinationDir |
| 119 | + include 'doma.compile.config' |
| 120 | + include 'META-INF/**/*.sql' |
| 121 | + include 'META-INF/**/*.script' |
| 122 | + } |
| 123 | +
|
| 124 | + compileKotlin { |
| 125 | + dependsOn copyDomaResources |
| 126 | + } |
| 127 | +
|
111 | 128 |
|
| 129 | +.. note:: |
112 | 130 |
|
113 | | -JavaとKotlinの混在 |
114 | | -------------------------- |
| 131 | + Remember that you always have options as follows: |
115 | 132 |
|
116 | | -kaptの不確実な挙動を避けるため、Domaに関するコードの全てをJavaで書くことは検討に値します。 |
117 | | -Domaの利用において、JavaとKotlinの混在は問題ありません。 |
| 133 | + - Write all codes with Kotlin |
| 134 | + - Write all codes with Java |
| 135 | + - Write codes annotated with Doma's annotations in Java and others in Kotlin |
118 | 136 |
|
119 | | -サンプルプロジェクト |
120 | | -===================== |
| 137 | + The third option is worth considering, because it can avoid some troubles with the kapt. |
121 | 138 |
|
122 | | -サンプルコードについては下記のプロジェクトを参照ください。 |
| 139 | +Sample project |
| 140 | +============== |
123 | 141 |
|
124 | 142 | * `kotlin-sample <https://github.com/domaframework/kotlin-sample>`_ |
0 commit comments