Class BaseMapper<E extends BaseEntity>

java.lang.Object
com.github.collinalpert.java2db.mappers.BaseMapper<E>
All Implemented Interfaces:
Mappable<E>

public class BaseMapper<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
    BaseMapper​(java.lang.Class<E> clazz)  
  • Method Summary

    Modifier and Type Method Description
    java.util.Optional<E> map​(java.sql.ResultSet set, java.util.Map<java.lang.String,​java.lang.String> aliases)
    Maps a ResultSet with a single row to a Java entity.
    E[] mapToArray​(java.sql.ResultSet set, java.util.Map<java.lang.String,​java.lang.String> aliases)
    Maps a ResultSet with multiple rows to an array of Java entities.
    java.util.List<E> mapToList​(java.sql.ResultSet set, java.util.Map<java.lang.String,​java.lang.String> aliases)
    Maps a ResultSet with 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, java.util.Map<java.lang.String,​java.lang.String> aliases)
    Maps a ResultSet to a Map.
    java.util.stream.Stream<E> mapToStream​(java.sql.ResultSet set, java.util.Map<java.lang.String,​java.lang.String> aliases)
    Maps a ResultSet with multiple rows to a Stream of Java entities.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BaseMapper

      public BaseMapper​(java.lang.Class<E> clazz)
  • Method Details

    • map

      public java.util.Optional<E> map​(java.sql.ResultSet set, java.util.Map<java.lang.String,​java.lang.String> aliases) throws java.sql.SQLException
      Maps a ResultSet with a single row to a Java entity.
      Specified by:
      map in interface Mappable<E extends BaseEntity>
      Parameters:
      set - The ResultSet to map.
      aliases - A map of column aliases needed to retrieve column data from the ResultSet.
      Returns:
      An Optional which contains the Java entity if the query was successful.
      Throws:
      java.sql.SQLException - if the ResultSet.next() call does not work as expected or if the entity fields cannot be set.
    • mapToList

      public java.util.List<E> mapToList​(java.sql.ResultSet set, java.util.Map<java.lang.String,​java.lang.String> aliases) throws java.sql.SQLException
      Maps a ResultSet with multiple rows to a list of Java entities.
      Specified by:
      mapToList in interface Mappable<E extends BaseEntity>
      Parameters:
      set - The ResultSet to map.
      aliases - A map of column aliases needed to retrieve column data from the ResultSet.
      Returns:
      A list of Java entities.
      Throws:
      java.sql.SQLException - if the ResultSet.next() call does not work as expected or if the entity fields cannot be set.
    • mapToStream

      public java.util.stream.Stream<E> mapToStream​(java.sql.ResultSet set, java.util.Map<java.lang.String,​java.lang.String> aliases) throws java.sql.SQLException
      Maps a ResultSet with multiple rows to a Stream of Java entities.
      Specified by:
      mapToStream in interface Mappable<E extends BaseEntity>
      Parameters:
      set - The ResultSet to map.
      aliases - A map of column aliases needed to retrieve column data from the ResultSet.
      Returns:
      A Stream of Java entities.
      Throws:
      java.sql.SQLException - if the ResultSet.next() call does not work as expected or if the entity fields cannot be set.
    • mapToArray

      public E[] mapToArray​(java.sql.ResultSet set, java.util.Map<java.lang.String,​java.lang.String> aliases) throws java.sql.SQLException
      Maps a ResultSet with multiple rows to an array of Java entities.
      Specified by:
      mapToArray in interface Mappable<E extends BaseEntity>
      Parameters:
      set - The ResultSet to map.
      aliases - A map of column aliases needed to retrieve column data from the ResultSet.
      Returns:
      An array of Java entities.
      Throws:
      java.sql.SQLException - if the ResultSet.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, java.util.Map<java.lang.String,​java.lang.String> aliases) throws java.sql.SQLException
      Maps a ResultSet to a Map.
      Specified by:
      mapToMap in interface Mappable<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 - The ResultSet to get the data from.
      keyMapping - The key function of the map.
      valueMapping - The value function of the map.
      aliases - A map of column aliases needed to retrieve column data from the ResultSet.
      Returns:
      A Map containing the ResultSets data.
      Throws:
      java.sql.SQLException - In case the ResultSet can't be read.