Package org.jdbi.v3.core.mapper.reflect
Class FieldMapper<T>
- java.lang.Object
-
- org.jdbi.v3.core.mapper.reflect.FieldMapper<T>
-
- All Implemented Interfaces:
RowMapper<T>
public final class FieldMapper<T> extends java.lang.Object implements RowMapper<T>
A row mapper which maps the columns in a statement into an object, using reflection to set fields on the object. All declared fields of the class and its superclasses may be set. Nested properties are supported via theNestedannotation. The mapped class must have a default constructor.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classFieldMapper.BoundFieldMapperprivate static classFieldMapper.FieldData
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringDEFAULT_PREFIXprivate java.util.Map<java.lang.reflect.Field,FieldMapper<?>>nestedMappersprivate java.lang.Stringprefixprivate java.lang.Class<T>type
-
Constructor Summary
Constructors Modifier Constructor Description privateFieldMapper(java.lang.Class<T> type, java.lang.String prefix)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static booleancheckPropagateNullAnnotation(java.lang.reflect.Field field)private java.util.Optional<RowMapper<T>>createSpecializedRowMapper(StatementContext ctx, java.util.List<java.lang.String> columnNames, java.util.List<ColumnNameMatcher> columnNameMatchers, java.util.List<java.lang.String> unmatchedColumns)private java.lang.StringdebugName(java.lang.reflect.Field field)static RowMapperFactoryfactory(java.lang.Class<?> type)Returns a mapper factory that maps to the given bean classstatic RowMapperFactoryfactory(java.lang.Class<?> type, java.lang.String prefix)Returns a mapper factory that maps to the given bean classprivate java.util.OptionalIntlocatePropagateNullColumnIndex(java.util.List<java.lang.String> columnNames, java.util.List<ColumnNameMatcher> columnNameMatchers)Tmap(java.sql.ResultSet rs, StatementContext ctx)Map the current row of the result set.static <T> RowMapper<T>of(java.lang.Class<T> type)Returns a mapper for the given bean classstatic <T> RowMapper<T>of(java.lang.Class<T> type, java.lang.String prefix)Returns a mapper for the given bean classprivate static java.lang.StringparamName(java.lang.reflect.Field field)RowMapper<T>specialize(java.sql.ResultSet rs, StatementContext ctx)Returns a specialized row mapper, optimized for the given result set.
-
-
-
Field Detail
-
DEFAULT_PREFIX
private static final java.lang.String DEFAULT_PREFIX
- See Also:
- Constant Field Values
-
type
private final java.lang.Class<T> type
-
prefix
private final java.lang.String prefix
-
nestedMappers
private final java.util.Map<java.lang.reflect.Field,FieldMapper<?>> nestedMappers
-
-
Constructor Detail
-
FieldMapper
private FieldMapper(java.lang.Class<T> type, java.lang.String prefix)
-
-
Method Detail
-
factory
public static RowMapperFactory factory(java.lang.Class<?> type)
Returns a mapper factory that maps to the given bean class- Parameters:
type- the mapped class- Returns:
- a mapper factory that maps to the given bean class
-
factory
public static RowMapperFactory factory(java.lang.Class<?> type, java.lang.String prefix)
Returns a mapper factory that maps to the given bean class- Parameters:
type- the mapped classprefix- the column name prefix for each mapped field- Returns:
- a mapper factory that maps to the given bean class
-
of
public static <T> RowMapper<T> of(java.lang.Class<T> type)
Returns a mapper for the given bean class- Type Parameters:
T- the type to map- Parameters:
type- the mapped class- Returns:
- a mapper for the given bean class
-
of
public static <T> RowMapper<T> of(java.lang.Class<T> type, java.lang.String prefix)
Returns a mapper for the given bean class- Type Parameters:
T- the type to map- Parameters:
type- the mapped classprefix- the column name prefix for each mapped field- Returns:
- a mapper for the given bean class
-
map
public T map(java.sql.ResultSet rs, StatementContext ctx) throws java.sql.SQLException
Description copied from interface:RowMapperMap the current row of the result set. This method should not cause the result set to advance; allow Jdbi to do that, please.
-
specialize
public RowMapper<T> specialize(java.sql.ResultSet rs, StatementContext ctx) throws java.sql.SQLException
Description copied from interface:RowMapperReturns a specialized row mapper, optimized for the given result set.Before mapping the result set from a SQL statement; Jdbi will first call this method to obtain a specialized instance. The returned mapper will then be used to map the result set rows, and discarded.
Implementing this method is optional; the default implementation returns
this. Implementors might choose to override this method to improve performance, e.g. by matching up column names to properties once for the entire result set, rather than repeating the process for every row.- Specified by:
specializein interfaceRowMapper<T>- Parameters:
rs- the result set to specialize overctx- the statement context to specialize over- Returns:
- a row mapper equivalent to this one, possibly specialized.
- Throws:
java.sql.SQLException- if anything goes wrong go ahead and let this percolate; Jdbi will handle it- See Also:
for an example of specialization.
-
createSpecializedRowMapper
private java.util.Optional<RowMapper<T>> createSpecializedRowMapper(StatementContext ctx, java.util.List<java.lang.String> columnNames, java.util.List<ColumnNameMatcher> columnNameMatchers, java.util.List<java.lang.String> unmatchedColumns)
-
locatePropagateNullColumnIndex
private java.util.OptionalInt locatePropagateNullColumnIndex(java.util.List<java.lang.String> columnNames, java.util.List<ColumnNameMatcher> columnNameMatchers)
-
paramName
private static java.lang.String paramName(java.lang.reflect.Field field)
-
debugName
private java.lang.String debugName(java.lang.reflect.Field field)
-
checkPropagateNullAnnotation
public static boolean checkPropagateNullAnnotation(java.lang.reflect.Field field)
-
-