Interface SqlRowMapper<T>
- Type Parameters:
T- The row (bean) type.
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
ResultSet row into a bean, for BeanQuerySession.find(BeanQuery) and
BeanQuerySession.stream(BeanQuery).
The result set is positioned on the row; do not call ResultSet.next(). columns lists the selected
(logical) column names in SELECT order (the query's view, or every column), so column i of the
result set is columns.get(i - 1) and a mapper can skip columns the query did not select.
Two built-in mappers need no dependency beyond this module:
map()— each row as aLinkedHashMapkeyed by column name in view order.bean(Class)— each row as arecord(canonical constructor) or as a class with a public no-arg constructor and public setters (setX/withX, void or fluent) or public non-final fields. Columns are matched to Juneau bean property names by exact name; columns with no property are ignored, and properties with no column keep their defaults.bean(BeanMeta)— the same, but using existing bean metadata, so custom property namers apply and column names match the property names a serializer built from that metadata emits.
When no mapper is set on the SqlBeanQueryContext.Builder, one is chosen from the row type: a Map
type gets map(), and any other type that bean(Class) can plan gets bean(Class).
Object, Map types a LinkedHashMap is not assignable to (such as TreeMap) and types
that cannot be planned get none, and find/stream then throw IllegalStateException.
Example:
- Since:
- 10.0.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> SqlRowMapper<T>A built-in row mapper that maps each row to a new instance oftype: arecord(via its canonical constructor) or a concrete class with a public no-arg constructor and public setters (setX/withXreturningvoidor the bean type, so fluent setters work) or public non-final fields.static <T> SqlRowMapper<T>Same asbean(Class), but follows the bean rules of existing bean metadata, for example the metadata of the serializer that renders the rows, so column names match the property names it emits.static SqlRowMapper<Map<String,Object>> map()A built-in row mapper that maps each row to a newLinkedHashMap, keyed by the logical column name inSELECTorder, with each value read viaResultSet.getObject(int)(the driver's natural type;null for SQLNULL).Maps the current result-set row to a bean.
-
Method Details
-
map
Maps the current result-set row to a bean.- Parameters:
rs- The result set, positioned on the row to map. Must not be advanced.columns- The selected column names inSELECTorder.- Returns:
- The mapped bean.
- Throws:
SQLException- If a column read fails.
-
map
A built-in row mapper that maps each row to a newLinkedHashMap, keyed by the logical column name inSELECTorder, with each value read viaResultSet.getObject(int)(the driver's natural type;null for SQLNULL).Example:
SqlRowMapper<Map<String,Object>>
mapper = SqlRowMapper.map ();- Returns:
- A shared, stateless row mapper. Always the same instance.
- Since:
- 10.0.0
-
bean
A built-in row mapper that maps each row to a new instance oftype: arecord(via its canonical constructor) or a concrete class with a public no-arg constructor and public setters (setX/withXreturningvoidor the bean type, so fluent setters work) or public non-final fields.Planning happens here, once; mapping never re-inspects
type. Columns match Juneau bean property names exactly (setters, public fields and record components, with@BeanIgnore,@BeanPropand the default property namer applied; seeQueryModel). A column with no property is ignored; a record component with no column, or a SQLNULLfor a primitive, gets the type's default. Values are read withResultSet.getObject(i, type), exceptInstant(viaOffsetDateTime),Date(viagetTimestamp) and enums (by constant name). A failure reading or setting a column throws aBeanQueryExecutionExceptionnaming the column. The returned mapper is thread-safe.Example:
record Task(String name,int age) {} SqlRowMapper<Task>mapper = SqlRowMapper.bean (Task.class );- Type Parameters:
T- The bean type.- Parameters:
type- The bean type. Must not benull .- Returns:
- A row mapper for
type. - Throws:
IllegalArgumentException- Iftypecannot be planned: an interface, abstract class, enum, array or primitive type, a class with no public no-arg constructor, or a setter bean with an ambiguous (overloaded) setter.- Since:
- 10.0.0
-
bean
Same asbean(Class), but follows the bean rules of existing bean metadata, for example the metadata of the serializer that renders the rows, so column names match the property names it emits.Example:
BeanConfigContext
config = BeanConfigContext.create ().propertyNamer(PropertyNamerDLC.INSTANCE ).build(); SqlRowMapper<Task>mapper = SqlRowMapper.bean (BeanMeta.of (Task.class ,config ));- Type Parameters:
T- The bean type.- Parameters:
beanMeta- The bean metadata. Must not benull .- Returns:
- A row mapper for the bean type.
- Throws:
IllegalArgumentException- For the same reasons asbean(Class).- Since:
- 10.0.0
-