类 SingleColumnRowMapper<T>
- java.lang.Object
- org.springframework.jdbc.core.SingleColumnRowMapper<T>
- 所有已实现的接口:
RowMapper<T>
public 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 ajava.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.- 从以下版本开始:
- 1.2
- 作者:
- Juergen Hoeller
- 另请参阅:
JdbcTemplate.queryForList(String, Class)
,JdbcTemplate.queryForObject(String, Class)
构造器概要
构造器 构造器 说明 SingleColumnRowMapper()
Create a newSingleColumnRowMapper
for bean-style configuration.SingleColumnRowMapper(Class<T> requiredType)
Create a newSingleColumnRowMapper
.
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 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 newSingleColumnRowMapper
(with the required type specified only once).void
setRequiredType(Class<T> requiredType)
Set the type that each result object is expected to match.
构造器详细资料
SingleColumnRowMapper
public SingleColumnRowMapper()
Create a newSingleColumnRowMapper
for bean-style configuration.
SingleColumnRowMapper
public SingleColumnRowMapper(Class<T> requiredType)
Create a newSingleColumnRowMapper
.Consider using the
newInstance(java.lang.Class<T>)
factory method instead, which allows for specifying the required type once only.- 参数:
requiredType
- the type that each result object is expected to match
方法详细资料
setRequiredType
public void setRequiredType(Class<T> requiredType)
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.
mapRow
public T mapRow(ResultSet rs, int rowNum) throws SQLException
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.- 指定者:
mapRow
在接口中RowMapper<T>
- 参数:
rs
- the ResultSet to map (pre-initialized for the current row)rowNum
- the number of the current row- 返回:
- the result object for the current row (may be
null
) - 抛出:
SQLException
- if a SQLException is encountered getting column values (that is, there's no need to catch SQLException)- 另请参阅:
ResultSetMetaData.getColumnCount()
,getColumnValue(java.sql.ResultSet, int, Class)
,convertValueToRequiredType(Object, Class)
getColumnValue
protected Object getColumnValue(ResultSet rs, int index, 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.- 参数:
rs
- is the ResultSet holding the dataindex
- is the column indexrequiredType
- the type that each result object is expected to match (ornull
if none specified)- 返回:
- the Object value
- 抛出:
SQLException
- in case of extraction failure- 另请参阅:
JdbcUtils.getResultSetValue(java.sql.ResultSet, int, Class)
,getColumnValue(java.sql.ResultSet, int)
getColumnValue
protected Object getColumnValue(ResultSet rs, int index) throws SQLException
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.- 参数:
rs
- is the ResultSet holding the dataindex
- is the column index- 返回:
- the Object value
- 抛出:
SQLException
- in case of extraction failure- 另请参阅:
JdbcUtils.getResultSetValue(java.sql.ResultSet, int)
convertValueToRequiredType
protected Object convertValueToRequiredType(Object value, Class<?> requiredType)
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).- 参数:
value
- the column value as extracted fromgetColumnValue()
(nevernull
)requiredType
- the type that each result object is expected to match (nevernull
)- 返回:
- the converted value
- 另请参阅:
getColumnValue(java.sql.ResultSet, int, Class)
newInstance
public static <T> SingleColumnRowMapper<T> newInstance(Class<T> requiredType)
Static factory method to create a newSingleColumnRowMapper
(with the required type specified only once).- 参数:
requiredType
- the type that each result object is expected to match- 从以下版本开始:
- 4.1