Class EntityMapper<E extends BaseEntity>
java.lang.Object
com.github.collinalpert.java2db.mappers.EntityMapper<E>
- All Implemented Interfaces:
Mappable<E>
public class EntityMapper<E extends BaseEntity> extends java.lang.Object implements Mappable<E>
Default mapper for converting a
ResultSet to the respective Java entity.- Author:
- Collin Alpert
-
Constructor Summary
Constructors Constructor Description EntityMapper(java.lang.Class<E> clazz) -
Method Summary
Modifier and Type Method Description java.util.Optional<E>map(java.sql.ResultSet set)Maps aResultSetwith a single row to a Java entity.E[]mapToArray(java.sql.ResultSet set)Maps aResultSetwith multiple rows to an array of Java entities.java.util.List<E>mapToList(java.sql.ResultSet set)Maps aResultSetwith multiple rows to a list of Java entities.<K, V> java.util.Map<K,V>mapToMap(java.sql.ResultSet set, java.util.function.Function<E,K> keyMapping, java.util.function.Function<E,V> valueMapping)Maps aResultSetto aMap.java.util.stream.Stream<E>mapToStream(java.sql.ResultSet set)Maps aResultSetwith multiple rows to aStreamof Java entities.
-
Constructor Details
-
Method Details
-
map
Maps aResultSetwith a single row to a Java entity.- Specified by:
mapin interfaceMappable<E extends BaseEntity>- Parameters:
set- TheResultSetto map.- Returns:
- An Optional which contains the Java entity if the query was successful.
- Throws:
java.sql.SQLException- if theResultSet.next()call does not work as expected or if the entity fields cannot be set.
-
mapToList
Maps aResultSetwith multiple rows to a list of Java entities.- Specified by:
mapToListin interfaceMappable<E extends BaseEntity>- Parameters:
set- TheResultSetto map.- Returns:
- A list of Java entities.
- Throws:
java.sql.SQLException- if theResultSet.next()call does not work as expected or if the entity fields cannot be set.
-
mapToStream
Maps aResultSetwith multiple rows to aStreamof Java entities.- Specified by:
mapToStreamin interfaceMappable<E extends BaseEntity>- Parameters:
set- TheResultSetto map.- Returns:
- A
Streamof Java entities. - Throws:
java.sql.SQLException- if theResultSet.next()call does not work as expected or if the entity fields cannot be set.
-
mapToArray
Maps aResultSetwith multiple rows to an array of Java entities.- Specified by:
mapToArrayin interfaceMappable<E extends BaseEntity>- Parameters:
set- TheResultSetto map.- Returns:
- An array of Java entities.
- Throws:
java.sql.SQLException- if theResultSet.next()call does not work as expected or if the entity fields cannot be set.
-
mapToMap
public <K, V> java.util.Map<K,V> mapToMap(java.sql.ResultSet set, java.util.function.Function<E,K> keyMapping, java.util.function.Function<E,V> valueMapping) throws java.sql.SQLExceptionMaps aResultSetto aMap.- Specified by:
mapToMapin interfaceMappable<E extends BaseEntity>- Type Parameters:
K- The type of the keys in the map.V- The type of the values in the map.- Parameters:
set- TheResultSetto get the data from.keyMapping- The key function of the map.valueMapping- The value function of the map.- Returns:
- A
Mapcontaining theResultSets data. - Throws:
java.sql.SQLException- In case theResultSetcan't be read.
-