Package org.springframework.jdbc.core

# Class DataClassRowMapper<T>

java.lang.Object
org.springframework.jdbc.core.BeanPropertyRowMapper<T>
org.springframework.jdbc.core.DataClassRowMapper<T>

Type Parameters:
  `T` - the result type

All Implemented Interfaces:
  `RowMapper<T>`

public class DataClassRowMapper<T> extends BeanPropertyRowMapper<T>
`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.

Since:
  5.3
Author:
  Juergen Hoeller, Sam Brannen
See Also:
  - `SimplePropertyRowMapper`

## Field Summary

### Fields inherited from class org.springframework.jdbc.core.BeanPropertyRowMapper

`logger`

## Constructor Summary

Constructors
Constructor
Description
`DataClassRowMapper()`
Create a new `DataClassRowMapper` for bean-style configuration.
`DataClassRowMapper(Class<T> mappedClass)`
Create a new `DataClassRowMapper`.

## Method Summary

Modifier and Type
Method
Description
`protected T`
`constructMappedInstance(ResultSet rs, org.springframework.beans.TypeConverter tc)`
Construct an instance of the mapped class for the current row.
`protected void`
`initialize(Class<T> mappedClass)`
Initialize the mapping meta-data for the given class.
`static <T> DataClassRowMapper<T>`
`newInstance(Class<T> mappedClass)`
Static factory method to create a new `DataClassRowMapper`.
`static <T> DataClassRowMapper<T>`
`newInstance(Class<T> mappedClass, org.springframework.core.convert.ConversionService conversionService)`
Static factory method to create a new `DataClassRowMapper`.

### Methods inherited from class org.springframework.jdbc.core.BeanPropertyRowMapper

`getColumnValue, getColumnValue, getConversionService, getMappedClass, initBeanWrapper, isCheckFullyPopulated, isPrimitivesDefaultedForNullValue, lowerCaseName, mappedNames, mapRow, setCheckFullyPopulated, setConversionService, setMappedClass, setPrimitivesDefaultedForNullValue, suppressProperty, underscoreName`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### DataClassRowMapper

public DataClassRowMapper()
Create a new `DataClassRowMapper` for bean-style configuration.

See Also:
  - `BeanPropertyRowMapper.setMappedClass(java.lang.Class<T>)`
  - `BeanPropertyRowMapper.setConversionService(org.springframework.core.convert.ConversionService)`

### DataClassRowMapper

public DataClassRowMapper(Class<T> mappedClass)
Create a new `DataClassRowMapper`.

Parameters:
  `mappedClass` - the class that each row should be mapped to

## Method Details

### initialize

protected void initialize(Class<T> mappedClass)
Description copied from class: `BeanPropertyRowMapper`
Initialize the mapping meta-data for the given class.

Overrides:
  `initialize` in class `BeanPropertyRowMapper<T>`
Parameters:
  `mappedClass` - the mapped class
See Also:
  - `BeanPropertyRowMapper.setMappedClass(java.lang.Class<T>)`
  - `BeanUtils.getPropertyDescriptors(java.lang.Class<?>)`
  - `BeanPropertyRowMapper.mappedNames(PropertyDescriptor)`

### constructMappedInstance

protected T constructMappedInstance(ResultSet rs, org.springframework.beans.TypeConverter tc) throws SQLException
Description copied from class: `BeanPropertyRowMapper`
Construct an instance of the mapped class for the current row.

Overrides:
  `constructMappedInstance` in class `BeanPropertyRowMapper<T>`
Parameters:
  `rs` - the ResultSet to map (pre-initialized for the current row)
  `tc` - a TypeConverter with this RowMapper's conversion service
Returns:
  a corresponding instance of the mapped class
Throws:
  `SQLException` - if an SQLException is encountered

### newInstance

public static <T> DataClassRowMapper<T> newInstance(Class<T> mappedClass)
Static factory method to create a new `DataClassRowMapper`.

Parameters:
  `mappedClass` - the class that each row should be mapped to
See Also:
  - `newInstance(Class, ConversionService)`

### newInstance

public static <T> DataClassRowMapper<T> newInstance(Class<T> mappedClass, @Nullable org.springframework.core.convert.ConversionService conversionService)
Static factory method to create a new `DataClassRowMapper`.

Parameters:
  `mappedClass` - the class that each row should be mapped to
  `conversionService` - the `ConversionService` for binding JDBC values to bean properties, or `null` for none
See Also:
  - `newInstance(Class)`
  - `BeanPropertyRowMapper.setConversionService(org.springframework.core.convert.ConversionService)`