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

Commit 485fc0d

Browse files
authored
Merge pull request #11 from k163377/add_readme
Add readme.
2 parents 9424d5f + ec316a9 commit 485fc0d

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

README.md

Lines changed: 63 additions & 2 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`.
@@ -174,7 +202,7 @@ data class Dst(@LocalDateTimeDeserializer val dateTime: LocalDateTime)
174202
```
175203

176204
##### annotation
177-
For the `annotation class`, specify the deserializer `class` with the `KColumnDeserializeBy` `annotation`.
205+
For the `annotation class`, specify the deserializer `class` with the `KColumnDeserializeBy` `annotation`.
178206
Also, the fields prepared for this `annotation class` can be used from the deserializer.
179207

180208
```kotlin
@@ -187,7 +215,7 @@ annotation class LocalDateTimeDeserializer(val pattern: String = "yyyy-MM-dd'T'H
187215
```
188216

189217
##### deserializer
190-
Deserializer is created by inheriting `AbstractKColumnDeserializer`.
218+
Deserializer is created by inheriting `AbstractKColumnDeserializer`.
191219
The meaning of each type parameter is as follows.
192220

193221
- `A`: `Annotation class` (`LocalDateTimeDeserializer` in this example)
@@ -220,6 +248,39 @@ class LocalDateTimeDeserializerImpl(
220248
}
221249
```
222250

251+
### Use default arguments
252+
`KRowMapper` supports `default arguments`.
253+
`Default arguments` are available in the following situations:
254+
255+
- When not referring to the acquisition result
256+
- When the acquisition result is `null`
257+
258+
As of `KRowMapper` 0.8, it does not support the use of `default argument` when columns cannot be obtained.
259+
260+
#### When not referring to the acquisition result
261+
When the `KUseDefaultArgument` `annotation` is added to the parameter,
262+
the `default argument` can be used forcibly without referring to the obtained result.
263+
264+
```kotlin
265+
data class Dst(val fooId: Int, @param:KUseDefaultArgument val barValue: String = "default")
266+
```
267+
268+
#### When the acquisition result is null
269+
When `KParameterRequireNonNull` `annotation` is given to a parameter,
270+
the default argument can be used if the obtained result is `null`.
271+
272+
```kotlin
273+
data class Dst(val fooId: Int, @param:KParameterRequireNonNull val barValue: String = "default")
274+
```
275+
276+
### Parameter aliasing
277+
In `KRowMapper`, the column name of the acquisition target can be specified by giving the `KParameterAlias` `annotation` to the `parameter`.
278+
The name conversion function is applied to the name specified here.
279+
280+
```kotlin
281+
data class Dst(@param:KParameterAlias("fooId") val barValue: Int)
282+
```
283+
223284
## Installation
224285
Published on JitPack.
225286
You can use this library on `maven`, `gradle` and any other build tools.

0 commit comments

Comments
 (0)