Configuration
Mappers live in a RowMapperRegistry. Create one, register your mappers and pass it to your
query configuration:
RowMapperRegistry registry = new RowMapperRegistry()
.register(PostgresqlMapper.getDefaultMapper())
.register(userMapper);
QueryConfiguration config = QueryConfiguration.builder(dataSource)
.setRowMapperRegistry(registry)
.build();
register takes one mapper, several mappers or a list. A class can have only one mapper per column set. Registering
a second one with the same columns throws a MappingAlreadyRegisteredException.
register(Class) registers the @MappingProvider mappers of a class right away, see
Custom mappers. It throws an InvalidMappingException if the class has none.
Default mappers
Each database module has mappers for simple types. They read the first column whose type fits, so they are made for results with one column:
long users = config.query("SELECT count(*) FROM users")
.single()
.mapAs(Long.class)
.first()
.orElse(0L);
| Class | Maps to |
|---|---|
PostgresqlMapper | Boolean, Short, Integer, Long, Float, Double, BigDecimal, String, Byte[], UUID |
MariaDbMapper | Boolean, Short, Integer, Long, Float, Double, BigDecimal, String, Byte[], UUID |
MySqlMapper | Boolean, Integer, Long, Float, Double, String, Byte[], UUID |
SqLiteMapper | Boolean, Integer, Long, Float, Double, String, Byte[], UUID |
Register them with getDefaultMapper(), or pick single ones like PostgresqlMapper.LONG_MAPPER.
The default mappers find a column by the type name the driver reports. On PostgreSQL the Short, Integer and
Long mappers read every integer column (smallint, integer, bigint and their serial variants), so count(*),
which is a bigint, can be read as an Integer. A value that does not fit fails with an SQLException.
UUID is read from a text column, or from a binary column as 16 bytes. On PostgreSQL it is also read from a uuid
column.
On MariaDB and MySQL, Byte[] is read from a binary column (BINARY, VARBINARY or a BLOB type), and also from
a text column as the bytes of the text.