T
- the result typepublic class SingleColumnRowMapper<T> extends Object implements 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.
JdbcTemplate.queryForList(String, Class)
,
JdbcTemplate.queryForObject(String, Class)
Constructor and Description |
---|
SingleColumnRowMapper()
Create a new
SingleColumnRowMapper for bean-style configuration. |
SingleColumnRowMapper(Class<T> requiredType)
Create a new
SingleColumnRowMapper . |
Modifier and Type | Method and Description |
---|---|
protected Object |
convertValueToRequiredType(Object value,
Class<?> requiredType)
Convert the given column value to the specified required type.
|
protected Object |
getColumnValue(ResultSet rs,
int index)
Retrieve a JDBC object value for the specified column, using the most
appropriate value type.
|
protected Object |
getColumnValue(ResultSet rs,
int index,
Class<?> requiredType)
Retrieve a JDBC object value for the specified column.
|
T |
mapRow(ResultSet rs,
int rowNum)
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 new
SingleColumnRowMapper . |
static <T> SingleColumnRowMapper<T> |
newInstance(Class<T> requiredType,
ConversionService conversionService)
Static factory method to create a new
SingleColumnRowMapper . |
void |
setConversionService(ConversionService conversionService)
Set a
ConversionService for converting a fetched value. |
void |
setRequiredType(Class<T> requiredType)
Set the type that each result object is expected to match.
|
public SingleColumnRowMapper()
SingleColumnRowMapper
for bean-style configuration.setRequiredType(java.lang.Class<T>)
public void setRequiredType(Class<T> requiredType)
If not specified, the column value will be exposed as returned by the JDBC driver.
public void setConversionService(@Nullable ConversionService conversionService)
ConversionService
for converting a fetched value.
Default is the DefaultConversionService
.
DefaultConversionService.getSharedInstance()
@Nullable public T mapRow(ResultSet rs, int rowNum) throws SQLException
Validates that there is only one column selected,
then delegates to getColumnValue()
and also
convertValueToRequiredType
, if necessary.
mapRow
in interface RowMapper<T>
rs
- the ResultSet
to map (pre-initialized for the current row)rowNum
- the number of the current rownull
)SQLException
- if an SQLException is encountered while getting
column values (that is, there's no need to catch SQLException)ResultSetMetaData.getColumnCount()
,
getColumnValue(java.sql.ResultSet, int, Class)
,
convertValueToRequiredType(Object, Class)
@Nullable protected Object getColumnValue(ResultSet rs, int index, @Nullable Class<?> requiredType) throws SQLException
The default implementation calls
JdbcUtils.getResultSetValue(java.sql.ResultSet, int, Class)
.
If no required type has been specified, this method delegates to
getColumnValue(rs, index)
, which basically calls
ResultSet.getObject(index)
but applies some additional
default conversion to appropriate value types.
rs
- is the ResultSet holding the dataindex
- is the column indexrequiredType
- the type that each result object is expected to match
(or null
if none specified)SQLException
- in case of extraction failureJdbcUtils.getResultSetValue(java.sql.ResultSet, int, Class)
,
getColumnValue(java.sql.ResultSet, int)
@Nullable protected Object getColumnValue(ResultSet rs, int index) throws SQLException
The default implementation delegates to JdbcUtils.getResultSetValue()
,
which uses the ResultSet.getObject(index)
method. Additionally,
it includes a "hack" to get around Oracle returning a non-standard object for
their TIMESTAMP datatype. See the JdbcUtils#getResultSetValue()
javadoc for details.
rs
- is the ResultSet holding the dataindex
- is the column indexSQLException
- in case of extraction failureJdbcUtils.getResultSetValue(java.sql.ResultSet, int)
@Nullable protected Object convertValueToRequiredType(Object value, Class<?> requiredType)
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 the ConversionService
.
value
- the column value as extracted from getColumnValue()
(never null
)requiredType
- the type that each result object is expected to match
(never null
)getColumnValue(java.sql.ResultSet, int, Class)
public static <T> SingleColumnRowMapper<T> newInstance(Class<T> requiredType)
SingleColumnRowMapper
.requiredType
- the type that each result object is expected to matchnewInstance(Class, ConversionService)
public static <T> SingleColumnRowMapper<T> newInstance(Class<T> requiredType, @Nullable ConversionService conversionService)
SingleColumnRowMapper
.requiredType
- the type that each result object is expected to matchconversionService
- the ConversionService
for converting a
fetched value, or null
for nonenewInstance(Class)
,
setConversionService(org.springframework.core.convert.ConversionService)