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

public Class SimplePropertyRowMapper<T>

extends Object
implements RowMapper<T>
Class Inheritance
All Implemented Interfaces
org.springframework.jdbc.core.RowMapper
Type Parameters
<T>
the result type
Imports
java.beans.PropertyDescriptor, java.lang.reflect.Constructor, .Field, .Method, java.sql.ResultSet, .ResultSetMetaData, .SQLException, java.util.HashSet, .Map, .Set, java.util.concurrent.ConcurrentHashMap, org.springframework.beans.BeanUtils, org.springframework.core.MethodParameter, org.springframework.core.convert.ConversionService, .TypeDescriptor, org.springframework.core.convert.support.DefaultConversionService, org.springframework.jdbc.support.JdbcUtils, org.springframework.util.Assert, .ReflectionUtils

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 fields with corresponding field 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 or field mapping. This means that constructor arguments take precedence over property setter methods which in turn take precedence over direct field mappings.

To facilitate mapping between columns and properties that don't have matching names, try using underscore-separated column aliases in the SQL statement like "select fname as first_name from customer", where first_name can be mapped to a setFirstName(String) method in the target class.

This is a flexible alternative to DataClassRowMapper and BeanPropertyRowMapper for scenarios where no specific customization and no pre-defined property mappings are needed.

In terms of its fallback property discovery algorithm, this class is similar to org.springframework.jdbc.core.namedparam.SimplePropertySqlParameterSource and is similarly used for org.springframework.jdbc.core.simple.JdbcClient.

Author
Juergen Hoeller
Since
6.1
See Also
DataClassRowMapper, BeanPropertyRowMapper, org.springframework.jdbc.core.simple.JdbcClient.StatementSpec#query(Class), org.springframework.jdbc.core.namedparam.SimplePropertySqlParameterSource

Field Summary

Modifier and TypeField and Description
private final String[]
private final TypeDescriptor[]
private final ConversionService
private final Class<T>
private final Constructor<T>
private static final Object
private final Map<String, Object>

Constructor Summary

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

Create a new SimplePropertyRowMapper.

public
SimplePropertyRowMapper(Class<T>
the class that each row should be mapped to
mappedClass
,
ConversionService
a ConversionService for binding JDBC values to bean properties
conversionService
)

Create a new SimplePropertyRowMapper.

Method Summary

Modifier and TypeMethod and Description
private Object
public T
mapRow(ResultSet
the ResultSet to map (pre-initialized for the current row)
rs
,
int
the number of the current row
rowNumber
)

Implements org.springframework.jdbc.core.RowMapper.mapRow.

Implementations must implement this method to map each row of data in the ResultSet.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

constructorParameterNamesback to summary
private final String[] constructorParameterNames
constructorParameterTypesback to summary
private final TypeDescriptor[] constructorParameterTypes
conversionServiceback to summary
private final ConversionService conversionService
mappedClassback to summary
private final Class<T> mappedClass
mappedConstructorback to summary
private final Constructor<T> mappedConstructor
NO_DESCRIPTORback to summary
private static final Object NO_DESCRIPTOR
propertyDescriptorsback to summary
private final Map<String, Object> propertyDescriptors

Constructor Detail

SimplePropertyRowMapperback to summary
public SimplePropertyRowMapper(Class<T> mappedClass)

Create a new SimplePropertyRowMapper.

Parameters
mappedClass:Class<T>

the class that each row should be mapped to

SimplePropertyRowMapperback to summary
public SimplePropertyRowMapper(Class<T> mappedClass, ConversionService conversionService)

Create a new SimplePropertyRowMapper.

Parameters
mappedClass:Class<T>

the class that each row should be mapped to

conversionService:ConversionService

a ConversionService for binding JDBC values to bean properties

Method Detail

getDescriptorback to summary
private Object getDescriptor(String column)
mapRowback to summary
public T mapRow(ResultSet rs, int rowNumber) throws SQLException

Implements org.springframework.jdbc.core.RowMapper.mapRow.

Doc from org.springframework.jdbc.core.RowMapper.mapRow.

Implementations must implement this method to map each row of data in the ResultSet. This method should not call next() on the ResultSet; it is only supposed to map values of the current row.

Parameters
rs:ResultSet

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

rowNumber:int

the number of the current row

Returns:T

the result object for the current row (may be null)

Annotations
@Override
Exceptions
SQLException:
if an SQLException is encountered while getting column values (that is, there's no need to catch SQLException)