Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit 0eab3e3

Browse files
committed
KClasssから初期化する場合について追記
1 parent 8df7360 commit 0eab3e3

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,34 @@ val dst: Dst = jdbcTemplate.query(query, KRowMapper(::Dst))
3030
```
3131

3232
## Usage
33+
### Initialization
34+
`KRowMapper` can be initialized from a `method reference` or an initialization function obtained from `KClass`.
35+
36+
#### Initialization from KClass
37+
When initializing from `KClass`, the `primary constructor` is used by default.
38+
39+
```kotlin
40+
val rowMapper = KRowMapper(Dst::class)
41+
```
42+
43+
By assigning the `KConstructor` `annotation` to the `secondary constructor` or `factory method`, you can also specify the `KFunction` to be used when initializing from the `KClass`.
44+
45+
```kotlin
46+
class SecondaryConstructorDst(val argument: Int) {
47+
@KConstructor
48+
constructor(argument: Number) : this(argument.toInt())
49+
}
50+
51+
class CompanionFactoryDst(val argument: IntArray) {
52+
companion object {
53+
@KConstructor
54+
fun factory(csv: String): CompanionFactoryDst {
55+
return csv.split(",").map { it.toInt() }.toIntArray().let { CompanionFactoryDst(it) }
56+
}
57+
}
58+
}
59+
```
60+
3361
### Convert Naming conventions
3462
`KRowMapper` searches columns by default in camel case.
3563
If the DB is named in snake case, mapping can be done by passing a conversion function(e.g. defined in `JackSon`, `Guava`) to `KRowMapper`.

0 commit comments

Comments
 (0)