K
- the type of keys maintained by the returned mapV
- the type of the beanpublic class BeanMapHandler<K,V> extends AbstractKeyedHandler<K,V>
ResultSetHandler
implementation that returns a Map of Beans.
ResultSet
rows are converted into Beans which are then stored in
a Map under the given key.
If you had a Person table with a primary key column called ID, you could retrieve rows from the table like this:
ResultSetHandler<Map<Long, Person>> h = new BeanMapHandler<Long, Person>(Person.class, "id"); Map<Long, Person> found = queryRunner.query("select id, name, age from person", h); Person jane = found.get(1L); // jane's id is 1 String janesName = jane.getName(); Integer janesAge = jane.getAge();Note that the "id" passed to BeanMapHandler can be in any case. The data type returned for id is dependent upon how your JDBC driver converts SQL column types from the Person table into Java types. The "name" and "age" columns are converted according to their property descriptors by DbUtils. </p>
This class is thread safe. </p>
ResultSetHandler
Constructor and Description |
---|
BeanMapHandler(Class<V> type)
Creates a new instance of BeanMapHandler.
|
BeanMapHandler(Class<V> type,
int columnIndex)
Creates a new instance of BeanMapHandler.
|
BeanMapHandler(Class<V> type,
RowProcessor convert)
Creates a new instance of BeanMapHandler.
|
BeanMapHandler(Class<V> type,
String columnName)
Creates a new instance of BeanMapHandler.
|
Modifier and Type | Method and Description |
---|---|
protected K |
createKey(ResultSet resultSet)
This factory method is called by
handle() to retrieve the
key value from the current ResultSet row. |
protected V |
createRow(ResultSet resultSet)
This factory method is called by
handle() to store the
current ResultSet row in some object. |
createMap, handle
public BeanMapHandler(Class<V> type)
type
- The Class that objects returned from createRow()
are created from.public BeanMapHandler(Class<V> type, int columnIndex)
type
- The Class that objects returned from createRow()
are created from.columnIndex
- The values to use as keys in the Map are retrieved from the
column at this index.public BeanMapHandler(Class<V> type, RowProcessor convert)
type
- The Class that objects returned from createRow()
are created from.convert
- The RowProcessor
implementation to use when
converting rows into Beanspublic BeanMapHandler(Class<V> type, String columnName)
type
- The Class that objects returned from createRow()
are created from.columnName
- The values to use as keys in the Map are retrieved from the
column with this name.protected K createKey(ResultSet resultSet) throws SQLException
handle()
to retrieve the
key value from the current ResultSet
row.createKey
in class AbstractKeyedHandler<K,V>
resultSet
- ResultSet to create a key fromSQLException
- if a database access error occursClassCastException
- if the class datatype does not match the column typeAbstractKeyedHandler.createKey(ResultSet)
protected V createRow(ResultSet resultSet) throws SQLException
AbstractKeyedHandler
handle()
to store the
current ResultSet
row in some object.createRow
in class AbstractKeyedHandler<K,V>
resultSet
- ResultSet to create a row fromSQLException
- if a database access error occursCopyright © 2002–2023 The Apache Software Foundation. All rights reserved.