Top Description Fields Constructors Methods
org.springframework.r2dbc.core

public Class DataClassRowMapper<T>

extends BeanPropertyRowMapper<T>
Class Inheritance
Type Parameters
<T>
the result type
Imports
java.lang.reflect.Constructor, java.util.List, io.r2dbc.spi.Readable, .ReadableMetadata, org.springframework.beans.BeanUtils, .TypeConverter, org.springframework.core.MethodParameter, org.springframework.core.convert.ConversionService, .TypeDescriptor, org.springframework.core.convert.support.DefaultConversionService, org.springframework.dao.DataRetrievalFailureException

Mapping Function implementation that converts an R2DBC Readable (a Row or OutParameters) into a new instance of the specified mapped target class. The mapped target class must be a top-level class or static nested class, and it may expose either a data class constructor with named parameters corresponding to column names or classic bean property setter methods with property names corresponding to column names (or even a combination of both).

The term "data class" applies to Java records, Kotlin data classes, and any class which has a constructor with named parameters that are intended to be mapped to corresponding column names.

When combining a data class constructor with setter methods, any property mapped successfully via a constructor argument will not be mapped additionally via a corresponding setter method. This means that constructor arguments take precedence over property setter methods.

Note that this class extends BeanPropertyRowMapper and can therefore serve as a common choice for any mapped target class, flexibly adapting to constructor style versus setter methods in the mapped class.

Please note that this class is designed to provide convenience rather than high performance. For best performance, consider using a custom readable mapping Function implementation.

Authors
Simon Baslé, Juergen Hoeller, Sam Brannen
Since
6.1

Field Summary

Modifier and TypeField and Description
private final String[]
private final TypeDescriptor[]
private final Constructor<T>

Constructor Summary

AccessConstructor and Description
public
DataClassRowMapper(Class<T>
the class that each row should be mapped to
mappedClass
)

Create a new DataClassRowMapper.

public
DataClassRowMapper(Class<T> mappedClass, ConversionService conversionService)

Method Summary

Modifier and TypeMethod and Description
protected T
constructMappedInstance(Readable
the Readable being mapped (a Row or OutParameters)
readable
,
List<? extends ReadableMetadata>
the list of item ReadableMetadata (either ColumnMetadata or OutParameterMetadata)
itemMetadatas
,
TypeConverter
a TypeConverter with this row mapper's conversion service
tc
)

Overrides org.springframework.r2dbc.core.BeanPropertyRowMapper.constructMappedInstance.

Construct an instance of the mapped class for the current Readable.

private int
findIndex(List<? extends ReadableMetadata> itemMetadatas, String name)

Inherited from org.springframework.r2dbc.core.BeanPropertyRowMapper:
applygetItemValuelowerCaseNamesuppressPropertyunderscoreName

Field Detail

constructorParameterNamesback to summary
private final String[] constructorParameterNames
constructorParameterTypesback to summary
private final TypeDescriptor[] constructorParameterTypes
mappedConstructorback to summary
private final Constructor<T> mappedConstructor

Constructor Detail

DataClassRowMapperback to summary
public DataClassRowMapper(Class<T> mappedClass)

Create a new DataClassRowMapper.

Parameters
mappedClass:Class<T>

the class that each row should be mapped to

DataClassRowMapperback to summary
public DataClassRowMapper(Class<T> mappedClass, ConversionService conversionService)

Method Detail

constructMappedInstanceback to summary
protected T constructMappedInstance(Readable readable, List<? extends ReadableMetadata> itemMetadatas, TypeConverter tc)

Overrides org.springframework.r2dbc.core.BeanPropertyRowMapper.constructMappedInstance.

Doc from org.springframework.r2dbc.core.BeanPropertyRowMapper.constructMappedInstance.

Construct an instance of the mapped class for the current Readable.

The default implementation simply instantiates the mapped class. Can be overridden in subclasses.

Parameters
readable:Readable

the Readable being mapped (a Row or OutParameters)

itemMetadatas:List<? extends ReadableMetadata>

the list of item ReadableMetadata (either ColumnMetadata or OutParameterMetadata)

tc:TypeConverter

a TypeConverter with this row mapper's conversion service

Returns:T

a corresponding instance of the mapped class

Annotations
@Override
findIndexback to summary
private int findIndex(List<? extends ReadableMetadata> itemMetadatas, String name)