public class ColumnMapRowMapper extends Object implements RowMapper<Map<String,Object>>
RowMapper
implementation that creates a java.util.Map
for each row, representing all columns as key-value pairs: one
entry for each column, with the column name as key.
The Map implementation to use and the key to use for each column
in the column Map can be customized by overriding createColumnMap(int)
and getColumnKey(java.lang.String)
, respectively.
Note: By default, ColumnMapRowMapper
will try to build a linked Map
with case-insensitive keys, to preserve column order as well as allow any
casing to be used for column names.
JdbcTemplate.queryForList(String)
,
JdbcTemplate.queryForMap(String)
Constructor and Description |
---|
ColumnMapRowMapper() |
Modifier and Type | Method and Description |
---|---|
protected Map<String,Object> |
createColumnMap(int columnCount)
Create a Map instance to be used as column map.
|
protected String |
getColumnKey(String columnName)
Determine the key to use for the given column in the column Map.
|
protected Object |
getColumnValue(ResultSet rs,
int index)
Retrieve a JDBC object value for the specified column.
|
Map<String,Object> |
mapRow(ResultSet rs,
int rowNum)
Implementations must implement this method to map each row of data in the
ResultSet . |
public Map<String,Object> mapRow(ResultSet rs, int rowNum) throws SQLException
RowMapper
ResultSet
. This method should not call next()
on the
ResultSet
; it is only supposed to map values of the current row.mapRow
in interface RowMapper<Map<String,Object>>
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)protected Map<String,Object> createColumnMap(int columnCount)
By default, a linked case-insensitive Map will be created.
columnCount
- the column count, to be used as initial
capacity for the MapLinkedCaseInsensitiveMap
protected String getColumnKey(String columnName)
By default, the supplied column name will be returned unmodified.
columnName
- the column name as returned by the ResultSetResultSetMetaData.getColumnName(int)
@Nullable protected Object getColumnValue(ResultSet rs, int index) throws SQLException
The default implementation uses the getObject
method.
Additionally, this implementation includes a "hack" to get around Oracle
returning a non-standard object for their TIMESTAMP data type.
rs
- the ResultSet holding the dataindex
- the column indexSQLException
JdbcUtils.getResultSetValue(java.sql.ResultSet, int, java.lang.Class<?>)