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 must have a default or
no-arg constructor.
Column values are mapped based on matching the column name (as obtained from result set meta-data) to public setters in the target class for the corresponding properties. The names are matched either directly or by transforming a name separating the parts with underscores to the same name using "camel" case.
Mapping is provided for properties in the target class for many common types —
for example: String, boolean, Boolean, byte, Byte, short, Short, int, Integer,
long, Long, float, Float, double, Double, BigDecimal, java.util.Date, etc.
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.
For a NULL value read from the database, an attempt will be made to
call the corresponding setter method with null, but in the case of
Java primitives this will result in a TypeMismatchException by default.
To ignore NULL database values for all primitive properties in the
target class, set the primitivesDefaultedForNullValue flag to
true. See setPrimitivesDefaultedForNullValue(boolean) for
details.
If you need to map to a target class which has a data class constructor
— for example, a Java record or a Kotlin data class —
use DataClassRowMapper instead.
Please note that this class is designed to provide convenience rather than
high performance. For best performance, consider using a custom RowMapper
implementation.
DataClassRowMapper, SimplePropertyRowMapper
| Modifier and Type | Field and Description |
|---|---|
| private boolean | checkFullyPopulated
Whether we're strictly validating. |
| private ConversionService | conversionService
ConversionService for binding JDBC values to bean properties. |
| protected final Log | logger
Logger available to subclasses. |
| private Class | mappedClass
The class we are mapping to. |
| private Map | mappedProperties
Map of the properties we provide mapping for. |
| private Set | mappedPropertyNames
Set of bean property names we provide mapping for. |
| private boolean | primitivesDefaultedForNullValue
Whether |
| Access | Constructor and Description |
|---|---|
| public | |
| public | BeanPropertyRowMapper(Class<T>
the class that each row should be mapped to mappedClass)Create a new |
| public | BeanPropertyRowMapper(Class<T>
the class that each row should be mapped to mappedClass, boolean whether we're strictly validating that
all bean properties have been mapped from corresponding database columns checkFullyPopulated)Create a new |
| Modifier and Type | Method and Description |
|---|---|
| protected T | Returns: a corresponding instance of the mapped classthe ResultSet to map (pre-initialized for the current row) rs, TypeConverter a TypeConverter with this RowMapper's conversion service tc)Construct an instance of the mapped class for the current row. |
| protected Object | Returns: the Object valueis the ResultSet holding the data rs, int is the column index index, PropertyDescriptor the bean property that each result object is expected to match pd)Retrieve a JDBC object value for the specified column. |
| protected Object | Returns: the Object valueis the ResultSet holding the data rs, int is the column index index, Class<?> the target parameter type paramType)Retrieve a JDBC object value for the specified column. |
| public ConversionService | getConversionService()
Return a |
| public final Class | |
| protected void | initBeanWrapper(BeanWrapper
the BeanWrapper to initialize bw)Initialize the given BeanWrapper to be used for row mapping. |
| protected void | initialize(Class<T>
the mapped class mappedClass)Initialize the mapping meta-data for the given class. |
| public boolean | isCheckFullyPopulated()
Return whether we're strictly validating that all bean properties have been mapped from corresponding database columns. |
| public boolean | |
| protected String | Returns: the converted namethe original name name)Convert the given name to lower case. |
| protected Set | Returns: a set of mapped namesthe property descriptor discovered on initialization pd)Determine the mapped names for the given property. |
| public T | mapRow(ResultSet
the rs, int ResultSet to map (pre-initialized for the current row)the number of the current row rowNumber)Implements org. |
| public static <T> BeanPropertyRowMapper | newInstance(Class<T>
the class that each row should be mapped to mappedClass)Static factory method to create a new |
| public static <T> BeanPropertyRowMapper | newInstance(Class<T>
the class that each row should be mapped to mappedClass, ConversionService the conversionService)ConversionService for binding
JDBC values to bean properties, or null for noneStatic factory method to create a new |
| public void | setCheckFullyPopulated(boolean checkFullyPopulated)
Set whether we're strictly validating that all bean properties have been mapped from corresponding database columns. |
| public void | setConversionService(ConversionService conversionService)
Set a |
| public void | |
| public void | setPrimitivesDefaultedForNullValue(boolean primitivesDefaultedForNullValue)
Set whether a |
| protected void | suppressProperty(String
the property name (as used by property descriptors) propertyName)Remove the specified property from the mapped properties. |
| protected String | Returns: the converted namethe original name name)Convert a name in camelCase to an underscored name in lower case. |
| checkFullyPopulated | back to summary |
|---|---|
| private boolean checkFullyPopulated Whether we're strictly validating. | |
| conversionService | back to summary |
|---|---|
| private ConversionService conversionService ConversionService for binding JDBC values to bean properties. | |
| logger | back to summary |
|---|---|
| protected final Log logger Logger available to subclasses. | |
| mappedClass | back to summary |
|---|---|
| private Class<T> mappedClass The class we are mapping to. | |
| mappedProperties | back to summary |
|---|---|
| private Map<String, PropertyDescriptor> mappedProperties Map of the properties we provide mapping for. | |
| mappedPropertyNames | back to summary |
|---|---|
| private Set<String> mappedPropertyNames Set of bean property names we provide mapping for. | |
| primitivesDefaultedForNullValue | back to summary |
|---|---|
| private boolean primitivesDefaultedForNullValue Whether | |
| BeanPropertyRowMapper | back to summary |
|---|---|
| public BeanPropertyRowMapper() Create a new
| |
| BeanPropertyRowMapper | back to summary |
|---|---|
| public BeanPropertyRowMapper(Class<T> mappedClass) Create a new
| |
| BeanPropertyRowMapper | back to summary |
|---|---|
| public BeanPropertyRowMapper(Class<T> mappedClass, boolean checkFullyPopulated) Create a new
| |
| constructMappedInstance | back to summary |
|---|---|
| protected T constructMappedInstance(ResultSet rs, TypeConverter tc) throws SQLException Construct an instance of the mapped class for the current row.
| |
| getColumnValue | back to summary |
|---|---|
| protected Object getColumnValue(ResultSet rs, int index, PropertyDescriptor pd) throws SQLException Retrieve a JDBC object value for the specified column. The default implementation calls
Subclasses may override this to check specific value types upfront,
or to post-process values returned from
| |
| getColumnValue | back to summary |
|---|---|
| protected Object getColumnValue(ResultSet rs, int index, Class<?> paramType) throws SQLException Retrieve a JDBC object value for the specified column. The default implementation calls
Subclasses may override this to check specific value types upfront,
or to post-process values returned from
| |
| getConversionService | back to summary |
|---|---|
| public ConversionService getConversionService() Return a
| |
| getMappedClass | back to summary |
|---|---|
| public final Class Get the class that we are mapping to.
| |
| initBeanWrapper | back to summary |
|---|---|
| protected void initBeanWrapper(BeanWrapper bw) Initialize the given BeanWrapper to be used for row mapping. To be called for each row. The default implementation applies the configured
| |
| initialize | back to summary |
|---|---|
| protected void initialize(Class<T> mappedClass) Initialize the mapping meta-data for the given class.
| |
| isCheckFullyPopulated | back to summary |
|---|---|
| public boolean isCheckFullyPopulated() Return whether we're strictly validating that all bean properties have been mapped from corresponding database columns. | |
| isPrimitivesDefaultedForNullValue | back to summary |
|---|---|
| public boolean isPrimitivesDefaultedForNullValue() Get the value of the | |
| lowerCaseName | back to summary |
|---|---|
| protected String lowerCaseName(String name) Convert the given name to lower case. By default, conversions will happen within the US locale.
| |
| mappedNames | back to summary |
|---|---|
| protected Set Determine the mapped names for the given property. Subclasses may override this method to customize the mapped names, adding to or removing from the set determined by this base method (which returns the property name in lower-case and underscore-based form), or replacing the set completely.
| |
| mapRow | back to summary |
|---|---|
| public T mapRow(ResultSet rs, int rowNumber) throws SQLException Implements org. Extract the values for all columns in the current row. Utilizes public setters and result set meta-data.
| |
| newInstance | back to summary |
|---|---|
| public static <T> BeanPropertyRowMapper Static factory method to create a new
| |
| newInstance | back to summary |
|---|---|
| public static <T> BeanPropertyRowMapper Static factory method to create a new
| |
| setCheckFullyPopulated | back to summary |
|---|---|
| public void setCheckFullyPopulated(boolean checkFullyPopulated) Set whether we're strictly validating that all bean properties have been mapped from corresponding database columns. Default is | |
| setConversionService | back to summary |
|---|---|
| public void setConversionService(ConversionService conversionService) Set a Default is a
| |
| setMappedClass | back to summary |
|---|---|
| public void setMappedClass(Class<T> mappedClass) Set the class that each row should be mapped to. | |
| setPrimitivesDefaultedForNullValue | back to summary |
|---|---|
| public void setPrimitivesDefaultedForNullValue(boolean primitivesDefaultedForNullValue) Set whether a Default is If this flag is set to | |
| suppressProperty | back to summary |
|---|---|
| protected void suppressProperty(String propertyName) Remove the specified property from the mapped properties.
| |
| underscoreName | back to summary |
|---|---|
| protected String underscoreName(String name) Convert a name in camelCase to an underscored name in lower case. Any upper case letters are converted to lower case with a preceding underscore.
| |