Interface ResultSetMapper
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Contract for mapping a
ResultSet row to the specified type.
A production-ready concrete implementation is available via the following static methods:
withDefaultConfiguration()withPlanCachingEnabled(Boolean)(builder)withCustomColumnMappers(List)(builder)withNormalizationLocale(Locale)(builder)
How to acquire an instance:
// With out-of-the-box defaults
ResultSetMapper default = ResultSetMapper.withDefaultConfiguration();
// Customized
ResultSetMapper custom = ResultSetMapper.withPlanCachingEnabled(false)
.customColumnMappers(List.of(...))
.normalizationLocale(Locale.forLanguageTag("pt-BR"))
.build(); Or, implement your own: ResultSetMapper myImpl = new ResultSetMapper() {
@NonNull
@Override
public <T> Optional<T> map(
@NonNull StatementContext<T> statementContext,
@NonNull ResultSet resultSet,
@NonNull Class<T> resultSetRowType,
@NonNull InstanceProvider instanceProvider
) throws SQLException {
// Pull data from resultSet and apply to a new instance of T
return Optional.empty();
}
};- Since:
- 1.0.0
- Author:
- Mark Allen
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classBuilder used to construct a standard implementation ofResultSetMapper. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault maximum number of cached row-mapping plans per result class.static final intDefault maximum number of cached preferred custom column mappers per source class. -
Method Summary
Modifier and TypeMethodDescriptionmap(@NonNull StatementContext<T> statementContext, @NonNull ResultSet resultSet, @NonNull Class<T> resultSetRowType, @NonNull InstanceProvider instanceProvider) Maps the current row ofresultSetto the result class indicated bystatementContext.static @NonNull ResultSetMapper.BuilderwithCustomColumnMappers(@NonNull List<@NonNull CustomColumnMapper> customColumnMappers) Acquires a builder for a concrete implementation of this interface, specifying aListof custom column-specific mapping logic to apply, in priority order.static @NonNull ResultSetMapperAcquires a concrete implementation of this interface with out-of-the-box defaults.static @NonNull ResultSetMapper.BuilderwithNormalizationLocale(@NonNull Locale normalizationLocale) Acquires a builder for a concrete implementation of this interface, specifying the locale to use when massaging JDBC column names for matching against Java property names.static @NonNull ResultSetMapper.BuilderwithPlanCachingEnabled(@NonNull Boolean planCachingEnabled) Acquires a builder for a concrete implementation of this interface, specifying whether an internal "mapping plan" cache should be used to speed upResultSetmapping.
-
Field Details
-
DEFAULT_PLAN_CACHE_CAPACITY
Default maximum number of cached row-mapping plans per result class.- See Also:
-
DEFAULT_PREFERRED_COLUMN_MAPPER_CACHE_CAPACITY
Default maximum number of cached preferred custom column mappers per source class.- See Also:
-
-
Method Details
-
map
<T> @NonNull Optional<T> map(@NonNull StatementContext<T> statementContext, @NonNull ResultSet resultSet, @NonNull Class<T> resultSetRowType, @NonNull InstanceProvider instanceProvider) throws SQLException Maps the current row ofresultSetto the result class indicated bystatementContext.- Type Parameters:
T- result instance type token- Parameters:
statementContext- current SQL contextresultSet- provides raw row data to pull fromresultSetRowType- the type to which theResultSetrow should be marshaledinstanceProvider- instance-creation factory, used to instantiateresultSetRowTyperow objects- Returns:
- an
Optionalcontaining an instance of the givenresultClass, orOptional.empty()to indicate anullvalue - Throws:
SQLException- if an error occurs during mapping
-
withNormalizationLocale
static @NonNull ResultSetMapper.Builder withNormalizationLocale(@NonNull Locale normalizationLocale) Acquires a builder for a concrete implementation of this interface, specifying the locale to use when massaging JDBC column names for matching against Java property names.- Parameters:
normalizationLocale- the locale to use when massaging JDBC column names for matching against Java property names- Returns:
- a
Builderfor a concrete implementation
-
withCustomColumnMappers
static @NonNull ResultSetMapper.Builder withCustomColumnMappers(@NonNull List<@NonNull CustomColumnMapper> customColumnMappers) Acquires a builder for a concrete implementation of this interface, specifying aListof custom column-specific mapping logic to apply, in priority order.- Parameters:
customColumnMappers- aListof custom column-specific mapping logic to apply, in priority order- Returns:
- a
Builderfor a concrete implementation
-
withPlanCachingEnabled
Acquires a builder for a concrete implementation of this interface, specifying whether an internal "mapping plan" cache should be used to speed upResultSetmapping.Disabling plan caching is primarily useful for highly dynamic schemas; the non-planned path allocates per-row maps and does more reflection.
- Parameters:
planCachingEnabled- whether an internal "mapping plan" cache should be used to speed upResultSetmapping- Returns:
- a
Builderfor a concrete implementation
-
withDefaultConfiguration
Acquires a concrete implementation of this interface with out-of-the-box defaults.The returned instance is thread-safe.
- Returns:
- a concrete implementation of this interface with out-of-the-box defaults
-