Class SingleColumnRowMapper<T>
- Type Parameters:
T- the result type
- All Implemented Interfaces:
RowMapper<T>
RowMapper implementation that converts a single column into a single
result value per row. Expects to operate on a java.sql.ResultSet
that just contains a single column.
The type of the result value for each row can be specified. The value
for the single column will be extracted from the ResultSet
and converted into the specified target type.
- Since:
- 1.2
- Author:
- Juergen Hoeller, Kazuki Shimizu
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a newSingleColumnRowMapperfor bean-style configuration.SingleColumnRowMapper(Class<T> requiredType) Create a newSingleColumnRowMapper. -
Method Summary
Modifier and TypeMethodDescriptionconvertValueToRequiredType(Object value, Class<?> requiredType) Convert the given column value to the specified required type.getColumnValue(ResultSet rs, int index) Retrieve a JDBC object value for the specified column, using the most appropriate value type.getColumnValue(ResultSet rs, int index, @Nullable Class<?> requiredType) Retrieve a JDBC object value for the specified column.Extract a value for the single column in the current row.static <T> SingleColumnRowMapper<T>newInstance(Class<T> requiredType) Static factory method to create a newSingleColumnRowMapper.static <T> SingleColumnRowMapper<T>newInstance(Class<T> requiredType, @Nullable ConversionService conversionService) Static factory method to create a newSingleColumnRowMapper.voidsetConversionService(@Nullable ConversionService conversionService) Set aConversionServicefor converting a fetched value.voidsetRequiredType(Class<T> requiredType) Set the type that each result object is expected to match.
-
Constructor Details
-
SingleColumnRowMapper
public SingleColumnRowMapper()Create a newSingleColumnRowMapperfor bean-style configuration.- See Also:
-
SingleColumnRowMapper
Create a newSingleColumnRowMapper.- Parameters:
requiredType- the type that each result object is expected to match
-
-
Method Details
-
setRequiredType
Set the type that each result object is expected to match.If not specified, the column value will be exposed as returned by the JDBC driver.
-
setConversionService
Set aConversionServicefor converting a fetched value.Default is the
DefaultConversionService.- Since:
- 5.0.4
- See Also:
-
mapRow
Extract a value for the single column in the current row.Validates that there is only one column selected, then delegates to
getColumnValue()and alsoconvertValueToRequiredType, if necessary.- Specified by:
mapRowin interfaceRowMapper<T>- Parameters:
rs- theResultSetto map (pre-initialized for the current row)rowNum- the number of the current row- Returns:
- the result object for the current row (may be
null) - Throws:
SQLException- if an SQLException is encountered while getting column values (that is, there's no need to catch SQLException)- See Also:
-
getColumnValue
protected @Nullable Object getColumnValue(ResultSet rs, int index, @Nullable Class<?> requiredType) throws SQLException Retrieve a JDBC object value for the specified column.The default implementation calls
JdbcUtils.getResultSetValue(java.sql.ResultSet, int, Class). If no required type has been specified, this method delegates togetColumnValue(rs, index), which basically callsResultSet.getObject(index)but applies some additional default conversion to appropriate value types.- Parameters:
rs- is the ResultSet holding the dataindex- is the column indexrequiredType- the type that each result object is expected to match (ornullif none specified)- Returns:
- the Object value
- Throws:
SQLException- in case of extraction failure- See Also:
-
getColumnValue
Retrieve a JDBC object value for the specified column, using the most appropriate value type. Called if no required type has been specified.The default implementation delegates to
JdbcUtils.getResultSetValue(), which uses theResultSet.getObject(index)method. Additionally, it includes a "hack" to get around Oracle returning a non-standard object for their TIMESTAMP datatype. See theJdbcUtils#getResultSetValue()javadoc for details.- Parameters:
rs- is the ResultSet holding the dataindex- is the column index- Returns:
- the Object value
- Throws:
SQLException- in case of extraction failure- See Also:
-
convertValueToRequiredType
Convert the given column value to the specified required type. Only called if the extracted column value does not match already.If the required type is String, the value will simply get stringified via
toString(). In case of a Number, the value will be converted into a Number, either through number conversion or through String parsing (depending on the value type). Otherwise, the value will be converted to a required type using theConversionService.- Parameters:
value- the column value as extracted fromgetColumnValue()(nevernull)requiredType- the type that each result object is expected to match (nevernull)- Returns:
- the converted value
- See Also:
-
newInstance
Static factory method to create a newSingleColumnRowMapper.- Parameters:
requiredType- the type that each result object is expected to match- Since:
- 4.1
- See Also:
-
newInstance
public static <T> SingleColumnRowMapper<T> newInstance(Class<T> requiredType, @Nullable ConversionService conversionService) Static factory method to create a newSingleColumnRowMapper.- Parameters:
requiredType- the type that each result object is expected to matchconversionService- theConversionServicefor converting a fetched value, ornullfor none- Since:
- 5.0.4
- See Also:
-