public class ClassMapper<T> extends Object implements ResultSetRowMapper<T>
The mapped target class must be a top-level class and it must have a default or no-arg constructor. You can find an optimal structure here.
Column values are mapped based on matching the column name as obtained from result set meta-data to public setters for the corresponding properties.
If field names are different from column names, you can annotate the public setters with the Name
annotation, and specify the column name.
Mapping is provided for fields in the target class for many common types, e.g.: String, boolean, Boolean, byte, Byte, short, Short, int, Integer, long, Long, float, Float, double, Double, BigDecimal, java.util.Date, etc.
If you have to convert a type into a more complex one, you can use converters. With the annotation MapWith
, you can annotate the setter parameter and specify the converter you want to use for that type.
Be aware that setters must have 1 and only 1 parameter; they will be ignored otherwise.
If you need your own converter, use the Converter
interface for custom implementations.
For null
values read from the database, ClassMapper will attempt to call the setter, but in the case of Java primitives, this causes a TypeMismatchException.
This class can be configured (using the 'defaultNullValueForPrimitives' property, true by default) to catch this exception and use the primitives default value.
ATTENTION: if you use the values from the generated bean to update the database, the primitive value will have been set to the primitive's default value instead of null.
Please note that this class is designed to provide CONVENIENCE rather than high performance. For best performance, consider using a custom ResultSetRowMapper implementation.
TypeMismatchException
,
ResultSetRowMapper
,
Name
,
MapWith
,
Converter
Modifier and Type | Field and Description |
---|---|
protected static Map<Class<Converter<Object,Object>>,Converter<Object,Object>> |
converterMap |
protected Map<String,PropertyDescriptor> |
descriptorMap |
protected PropertyDescriptor[] |
descriptors |
protected Class<T> |
target |
Constructor and Description |
---|
ClassMapper(@NotNull Class<T> target)
Constructs a new ClassMapper which maps the given bean class.
|
Modifier and Type | Method and Description |
---|---|
<S,R> ClassMapper<T> |
addConverter(Converter<S,R> converter)
Adds a new converter for this (and future ones) ClassMapper instance.
|
protected void |
assignColumnToProperty(ResultSetMetaData metaData,
PropertyDescriptor[] descriptors,
int columnCount) |
protected PropertyDescriptor[] |
getDescriptors(Class<T> target) |
boolean |
isDefaultNullValueForPrimitives()
Return whether we're defaulting Java primitives in the case of mapping a null value from corresponding database fields.
|
T |
map(@NotNull ResultSet resultSet,
int rowNumber)
Implementations will tell how to map EACH row of the ResultSet.
|
protected T |
newInstance(Class<T> target) |
static <S> @NotNull ClassMapper<S> |
of(Class<S> type) |
Map<Class<?>,Object> |
primitives() |
protected @Nullable MapWith |
retrieveAnnotations(Method method) |
protected @Nullable Converter<Object,Object> |
retrieveConverter(Method method) |
ClassMapper<T> |
setDefaultNullValueForPrimitives(boolean defaultNullValueForPrimitives)
Set whether we're defaulting Java primitives in the case of mapping a null value from corresponding database fields.
|
protected void |
setValue(T bean,
PropertyDescriptor descriptor,
Object value) |
protected static final Map<Class<Converter<Object,Object>>,Converter<Object,Object>> converterMap
protected final PropertyDescriptor[] descriptors
protected final Map<String,PropertyDescriptor> descriptorMap
@Nullable public T map(@NotNull @NotNull ResultSet resultSet, int rowNumber) throws SQLException
ResultSetRowMapper
ResultSet.next()
call is not needed: this method should only map values of the current row.map
in interface ResultSetRowMapper<T>
resultSet
- the ResultSet, already initializedrowNumber
- the number of the current rowSQLException
- if the implementation's trying to get column values in the wrong waypublic ClassMapper<T> setDefaultNullValueForPrimitives(boolean defaultNullValueForPrimitives)
public boolean isDefaultNullValueForPrimitives()
public <S,R> ClassMapper<T> addConverter(Converter<S,R> converter)
S
- the type to convertR
- the desired typeconverter
- the converter implementationprotected T newInstance(Class<T> target) throws SQLException
SQLException
protected PropertyDescriptor[] getDescriptors(Class<T> target) throws SQLException
SQLException
protected void setValue(T bean, PropertyDescriptor descriptor, Object value) throws TypeMismatchException
TypeMismatchException
protected void assignColumnToProperty(ResultSetMetaData metaData, PropertyDescriptor[] descriptors, int columnCount) throws SQLException
SQLException
@Nullable protected @Nullable Converter<Object,Object> retrieveConverter(Method method)
@NotNull public static <S> @NotNull ClassMapper<S> of(Class<S> type)
Copyright © 2021. All rights reserved.