Class BeanMapHandler<K,V>
- java.lang.Object
-
- org.apache.commons.dbutils.handlers.AbstractKeyedHandler<K,V>
-
- org.apache.commons.dbutils.handlers.BeanMapHandler<K,V>
-
- Type Parameters:
K
- the type of keys maintained by the returned mapV
- the type of the bean
- All Implemented Interfaces:
ResultSetHandler<java.util.Map<K,V>>
public 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>
- Since:
- DbUtils 1.5
- See Also:
ResultSetHandler
-
-
Field Summary
Fields Modifier and Type Field Description private int
columnIndex
The column index to retrieve key values from.private java.lang.String
columnName
The column name to retrieve key values from.private RowProcessor
convert
The RowProcessor implementation to use when converting rows into Objects.private java.lang.Class<V>
type
The Class of beans produced by this handler.
-
Constructor Summary
Constructors Modifier Constructor Description BeanMapHandler(java.lang.Class<V> type)
Creates a new instance of BeanMapHandler.BeanMapHandler(java.lang.Class<V> type, int columnIndex)
Creates a new instance of BeanMapHandler.BeanMapHandler(java.lang.Class<V> type, java.lang.String columnName)
Creates a new instance of BeanMapHandler.BeanMapHandler(java.lang.Class<V> type, RowProcessor convert)
Creates a new instance of BeanMapHandler.private
BeanMapHandler(java.lang.Class<V> type, RowProcessor convert, int columnIndex, java.lang.String columnName)
Private Helper
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected K
createKey(java.sql.ResultSet rs)
This factory method is called byhandle()
to retrieve the key value from the currentResultSet
row.protected V
createRow(java.sql.ResultSet rs)
This factory method is called byhandle()
to store the currentResultSet
row in some object.-
Methods inherited from class org.apache.commons.dbutils.handlers.AbstractKeyedHandler
createMap, handle
-
-
-
-
Field Detail
-
type
private final java.lang.Class<V> type
The Class of beans produced by this handler.
-
convert
private final RowProcessor convert
The RowProcessor implementation to use when converting rows into Objects.
-
columnIndex
private final int columnIndex
The column index to retrieve key values from. Defaults to 1.
-
columnName
private final java.lang.String columnName
The column name to retrieve key values from. Either columnName or columnIndex will be used but never both.
-
-
Constructor Detail
-
BeanMapHandler
public BeanMapHandler(java.lang.Class<V> type)
Creates a new instance of BeanMapHandler. The value of the first column of each row will be a key in the Map.- Parameters:
type
- The Class that objects returned fromcreateRow()
are created from.
-
BeanMapHandler
public BeanMapHandler(java.lang.Class<V> type, RowProcessor convert)
Creates a new instance of BeanMapHandler. The value of the first column of each row will be a key in the Map.- Parameters:
type
- The Class that objects returned fromcreateRow()
are created from.convert
- TheRowProcessor
implementation to use when converting rows into Beans
-
BeanMapHandler
public BeanMapHandler(java.lang.Class<V> type, int columnIndex)
Creates a new instance of BeanMapHandler.- Parameters:
type
- The Class that objects returned fromcreateRow()
are created from.columnIndex
- The values to use as keys in the Map are retrieved from the column at this index.
-
BeanMapHandler
public BeanMapHandler(java.lang.Class<V> type, java.lang.String columnName)
Creates a new instance of BeanMapHandler.- Parameters:
type
- The Class that objects returned fromcreateRow()
are created from.columnName
- The values to use as keys in the Map are retrieved from the column with this name.
-
BeanMapHandler
private BeanMapHandler(java.lang.Class<V> type, RowProcessor convert, int columnIndex, java.lang.String columnName)
Private Helper- Parameters:
convert
- TheRowProcessor
implementation to use when converting rows into BeanscolumnIndex
- The values to use as keys in the Map are retrieved from the column at this index.columnName
- The values to use as keys in the Map are retrieved from the column with this name.
-
-
Method Detail
-
createKey
protected K createKey(java.sql.ResultSet rs) throws java.sql.SQLException
This factory method is called byhandle()
to retrieve the key value from the currentResultSet
row.- Specified by:
createKey
in classAbstractKeyedHandler<K,V>
- Parameters:
rs
- ResultSet to create a key from- Returns:
- K from the configured key column name/index
- Throws:
java.sql.SQLException
- if a database access error occursjava.lang.ClassCastException
- if the class datatype does not match the column type- See Also:
AbstractKeyedHandler.createKey(ResultSet)
-
createRow
protected V createRow(java.sql.ResultSet rs) throws java.sql.SQLException
Description copied from class:AbstractKeyedHandler
This factory method is called byhandle()
to store the currentResultSet
row in some object.- Specified by:
createRow
in classAbstractKeyedHandler<K,V>
- Parameters:
rs
- ResultSet to create a row from- Returns:
- V object created from the current row
- Throws:
java.sql.SQLException
- if a database access error occurs
-
-