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

public Class DataClassRowMapper<T>

extends BeanPropertyRowMapper<T>
Class Inheritance
Type Parameters
<T>
the result type
Imports
java.lang.reflect.Constructor, java.sql.ResultSet, .SQLException, org.springframework.beans.BeanUtils, .TypeConverter, org.springframework.core.MethodParameter, org.springframework.core.convert.ConversionService, .TypeDescriptor, org.springframework.lang.Nullable, org.springframework.util.Assert

RowMapper implementation that converts a row 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 RowMapper implementation.

Authors
Juergen Hoeller, Sam Brannen
Since
5.3
See Also
SimplePropertyRowMapper

Field Summary

Modifier and TypeField and Description
private String[]
private TypeDescriptor[]
private Constructor<T>
Inherited from org.springframework.jdbc.core.BeanPropertyRowMapper:
logger

Constructor Summary

AccessConstructor and Description
public
DataClassRowMapper()

Create a new DataClassRowMapper for bean-style configuration.

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

Create a new DataClassRowMapper.

Method Summary

Modifier and TypeMethod and Description
protected T
constructMappedInstance(ResultSet
the ResultSet to map (pre-initialized for the current row)
rs
,
TypeConverter
a TypeConverter with this RowMapper's conversion service
tc
)

Overrides org.springframework.jdbc.core.BeanPropertyRowMapper.constructMappedInstance.

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

protected void
initialize(Class<T>
the mapped class
mappedClass
)

Overrides org.springframework.jdbc.core.BeanPropertyRowMapper.initialize.

Initialize the mapping meta-data for the given class.

public static <T> DataClassRowMapper<T>
newInstance(Class<T>
the class that each row should be mapped to
mappedClass
)

Hides org.springframework.jdbc.core.BeanPropertyRowMapper.newInstance.

Static factory method to create a new DataClassRowMapper.

public static <T> DataClassRowMapper<T>
newInstance(Class<T>
the class that each row should be mapped to
mappedClass
,
ConversionService
the ConversionService for binding JDBC values to bean properties, or null for none
conversionService
)

Hides org.springframework.jdbc.core.BeanPropertyRowMapper.newInstance.

Static factory method to create a new DataClassRowMapper.

Inherited from org.springframework.jdbc.core.BeanPropertyRowMapper:
getColumnValuegetColumnValuegetConversionServicegetMappedClassinitBeanWrapperisCheckFullyPopulatedisPrimitivesDefaultedForNullValuelowerCaseNamemappedNamesmapRowsetCheckFullyPopulatedsetConversionServicesetMappedClasssetPrimitivesDefaultedForNullValuesuppressPropertyunderscoreName

Field Detail

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

Constructor Detail

DataClassRowMapperback to summary
public DataClassRowMapper()

Create a new DataClassRowMapper for bean-style configuration.

See Also
setMappedClass, setConversionService
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

Method Detail

constructMappedInstanceback to summary
protected T constructMappedInstance(ResultSet rs, TypeConverter tc) throws SQLException

Overrides org.springframework.jdbc.core.BeanPropertyRowMapper.constructMappedInstance.

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

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

Parameters
rs:ResultSet

the ResultSet to map (pre-initialized for the current row)

tc:TypeConverter

a TypeConverter with this RowMapper's conversion service

Returns:T

a corresponding instance of the mapped class

Annotations
@Override
Exceptions
SQLException:
if an SQLException is encountered
initializeback to summary
protected void initialize(Class<T> mappedClass)

Overrides org.springframework.jdbc.core.BeanPropertyRowMapper.initialize.

Doc from org.springframework.jdbc.core.BeanPropertyRowMapper.initialize.

Initialize the mapping meta-data for the given class.

Parameters
mappedClass:Class<T>

the mapped class

Annotations
@Override
newInstanceback to summary
public static <T> DataClassRowMapper<T> newInstance(Class<T> mappedClass)

Hides org.springframework.jdbc.core.BeanPropertyRowMapper.newInstance.

Static factory method to create a new DataClassRowMapper.

Parameters
mappedClass:Class<T>

the class that each row should be mapped to

See Also
newInstance(Class, ConversionService)
newInstanceback to summary
public static <T> DataClassRowMapper<T> newInstance(Class<T> mappedClass, ConversionService conversionService)

Hides org.springframework.jdbc.core.BeanPropertyRowMapper.newInstance.

Static factory method to create a new DataClassRowMapper.

Parameters
mappedClass:Class<T>

the class that each row should be mapped to

conversionService:ConversionService

the ConversionService for binding JDBC values to bean properties, or null for none

See Also
newInstance(Class), setConversionService