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

Commit dda8e86

Browse files
committed
Deserialization(冒頭)
ついでに古い内容は削除
1 parent 2fe8e86 commit dda8e86

File tree

1 file changed

+6
-177
lines changed

1 file changed

+6
-177
lines changed

README.md

Lines changed: 6 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -220,184 +220,13 @@ val mapper: KRowMapper<Dst> = KRowMapper(::Dst, parameterNameConverter)
220220
By using the contents described so far, you can perform more flexible and safe mapping compared to `BeanPropertyRowMapper`.
221221
In addition, by making full use of the abundant functions provided by `KRowMapper`, further labor saving is possible.
222222

223-
### Deserialize column
224-
`KRowMapper` provides a deserialization function for the acquisition results of three patterns.
223+
### Deserialization
224+
Since `KRowMapper` gets the value from `java.sql.ResultSet`, by default it is not possible to get the type which is not supported by this implementation.
225+
To deal with this problem, `KRowMapper` provides the following three types of deserialization methods in addition to the default conversion function.
225226

226-
- Deserialize at initialization using the `factory method` or deserialize on initialize.
227-
- Define a deserializer for the `class`.
228-
- Define custom deserializer `annotation`.
229-
230-
#### Deserialize at initialization using the factory method or deserialize on initialize
231-
Deserialization within a `factory method` or initialization is the simplest method.
232-
Also, deserialization from multiple columns to one argument or from one column to multiple arguments cannot be realized other than this method.
233-
234-
```kotlin
235-
// example of deserialize on factory method
236-
data class Dst(
237-
foo: Foo,
238-
bar: Bar,
239-
baz: Baz?,
240-
...
241-
) {
242-
companion object {
243-
fun factory(
244-
foo: String,
245-
bar: String,
246-
baz: Int?,
247-
...
248-
): Dst {
249-
return Dst(
250-
Foo(foo),
251-
Bar.fromString(bar),
252-
baz?.let { Baz(it) },
253-
...
254-
)
255-
}
256-
}
257-
}
258-
259-
val dst: Dst = jdbcTemplate.query(query, KRowMapper((Dst)::factory))
260-
```
261-
262-
#### Define a deserializer for the class
263-
By assigning the `KColumnDeserializer` `annotation` to the `constructor` or `factory method`, deserialization by the `KFunction` can be performed.
264-
The `method` that assigns this `annotation` must have one argument.
265-
266-
When the deserializer is defined in this way, the destination will be as follows.
267-
268-
```kotlin
269-
data class Dst(
270-
val foo: ByConstructor,
271-
val bar: BySecondaryConstructor,
272-
val baz: ByCompanionObject,
273-
val qux: ByStaticMethod
274-
)
275-
```
276-
277-
##### constructor
278-
```kotlin
279-
data class ByConstructor @KColumnDeserializer constructor(val fooString: String)
280-
```
281-
282-
##### secondary constructor
283-
```kotlin
284-
data class BySecondaryConstructor(val barShort: Short) {
285-
@KColumnDeserializer
286-
constructor(bar: String) : this(bar.toShort())
287-
}
288-
```
289-
290-
##### factory method
291-
```kotlin
292-
data class ByCompanionObject(val bazInt: Int) {
293-
companion object {
294-
@KColumnDeserializer
295-
fun factory(baz: String) = ByCompanionObject(baz.toInt())
296-
}
297-
}
298-
```
299-
300-
##### (static method)
301-
`Java` `static method` is also supported.
302-
303-
```java
304-
public class ByStaticMethod {
305-
private final String quxString;
306-
307-
public ByStaticMethod(String quxString) {
308-
this.quxString = quxString;
309-
}
310-
311-
public String getQuxString() {
312-
return quxString;
313-
}
314-
315-
@KColumnDeserializer
316-
public static ByStaticMethod factory(Integer quxArg) {
317-
return new ByStaticMethod(quxArg.toString());
318-
}
319-
}
320-
```
321-
322-
#### Define custom deserializer annotation
323-
`KRowMapper` supports complex deserialization by defining custom deserializer `annotations`.
324-
As an example, a custom deserializer `annotation` that performs deserialization from a `String` to `LocalDateTime` is shown.
325-
326-
```kotlin
327-
// annotation
328-
@Target(AnnotationTarget.VALUE_PARAMETER)
329-
@Retention(AnnotationRetention.RUNTIME)
330-
@MustBeDocumented
331-
@KColumnDeserializeBy(LocalDateTimeDeserializerImpl::class)
332-
annotation class LocalDateTimeDeserializer(val pattern: String = "yyyy-MM-dd'T'HH:mm:ss")
333-
334-
// deserializer
335-
class LocalDateTimeDeserializerImpl(
336-
annotation: LocalDateTimeDeserializer
337-
) : AbstractKColumnDeserializer<LocalDateTimeDeserializer, String, LocalDateTime>(annotation) {
338-
private val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern(annotation.pattern)
339-
340-
override val srcClass: Class<String> = String::class.javaObjectType
341-
342-
override fun deserialize(source: String?): LocalDateTime? {
343-
return source?.let {
344-
LocalDateTime.parse(it, formatter)
345-
}
346-
}
347-
}
348-
```
349-
350-
```kotlin
351-
// usage
352-
data class Dst(@LocalDateTimeDeserializer val dateTime: LocalDateTime)
353-
```
354-
355-
##### annotation
356-
For the `annotation class`, specify the deserializer `class` with the `KColumnDeserializeBy` `annotation`.
357-
Also, the fields prepared for this `annotation class` can be used from the deserializer.
358-
359-
```kotlin
360-
@Target(AnnotationTarget.VALUE_PARAMETER)
361-
@Retention(AnnotationRetention.RUNTIME)
362-
@MustBeDocumented
363-
// LocalDateTimeDeserializerImpl is deserializer class
364-
@KColumnDeserializeBy(LocalDateTimeDeserializerImpl::class)
365-
annotation class LocalDateTimeDeserializer(val pattern: String = "yyyy-MM-dd'T'HH:mm:ss")
366-
```
367-
368-
##### deserializer
369-
Deserializer is created by inheriting `AbstractKColumnDeserializer`.
370-
The meaning of each type parameter is as follows.
371-
372-
- `A`: `Annotation class` (`LocalDateTimeDeserializer` in this example)
373-
- `S`: `Java class` of argument required at deserialization
374-
- `D`: `Class` returned after deserialization
375-
376-
```kotlin
377-
abstract class AbstractKColumnDeserializer<A : Annotation, S : Any, D : Any>(protected val annotation: A) {
378-
abstract val srcClass: Class<S>
379-
abstract fun deserialize(source: S?): D?
380-
}
381-
```
382-
383-
In the example, deserialization from a `String` to `LocalDateTime` is performed based on the `pattern` obtained from the
384-
`LocalDateTimeDeserializer`.
385-
386-
```kotlin
387-
class LocalDateTimeDeserializerImpl(
388-
annotation: LocalDateTimeDeserializer
389-
) : AbstractKColumnDeserializer<LocalDateTimeDeserializer, String, LocalDateTime>(annotation) {
390-
private val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern(annotation.pattern)
391-
392-
override val srcClass: Class<String> = String::class.javaObjectType
393-
394-
override fun deserialize(source: String?): LocalDateTime? {
395-
return source?.let {
396-
LocalDateTime.parse(it, formatter)
397-
}
398-
}
399-
}
400-
```
227+
1. Deserialization by using the `KColumnDeserializer` annotation.
228+
2. Deserialization by creating your own custom deserialization annotations.
229+
3. Deserialization from multiple arguments.
401230

402231
### Use default arguments
403232
`KRowMapper` supports `default arguments`.

0 commit comments

Comments
 (0)