db-utils 


This library contains useful utility classes to work with the database and to reduce boilerplate code.
Installation
Releases are available in Maven Central.
List of version changes. List of version migrations.
Maven
Add this snippet to the pom.xml dependencies
section:
<dependency>
<groupId>ru.spb.devclub.utils</groupId>
<artifactId>db-utils</artifactId>
<version>0.3.2</version>
</dependency>
Gradle
Add this snippet to the build.gradle dependencies
section:
implementation 'ru.spb.devclub.utils:db-utils:0.3.2'
Others
Others snippets are available in The Central Repository.
Usage
ExtendedResultSet
The java.sql.ResultSet
returns the default value for primitive types if the value is SQL NULL
.
ExtendedResultSet adds:
- boxed primitive types usage methods:
void example(ExtendedResultSet rs) {
Boolean booleanValue = rs.getBooleanOrNull(0);
Integer intValue = rs.getIntOrNull("one");
Long longValue = rs.getLongOrNull(2);
}
- local dates usage methods:
void example(ExtendedResultSet rs) {
LocalDate date = rs.getLocalDate(0);
LocalTime time = rs.getLocalTime("one");
LocalDateTime dateTime = rs.getLocalDateTime(2);
}
- enum usage methods:
void example(ExtendedResultSet rs) {
Thread.State zeroState = rs.getEnumByName(0);
Thread.State firstState = rs.getEnumByOrdinal("one");
}
java.util.Optional
usage methods:
void example(ExtendedResultSet rs){
Optional<Boolean> booleanOptional = rs.getOptionalBoolean(0);
Optional<Integer> intOptional = rs.getOptionalInt("one");
Optional<Long> longOptional = rs.getOptionalLong(2);
}
Also for the all types from ResultSet.
ExtendedRowMapper
The Spring-Jdbc
RowMapper
provides the mapRow(rs:ResultSet, rowNum:int)
method.
ExtendedRowMapper replaces the ResultSet argument type of RowMapper#mapRow with the ExtendedResultSet.
import java.sql.SQLException;
public class ShortRowMapper implements ExtendedRowMapper<Short> {
@Override
public Short mapRow(ExtendedResultSet rs, int rowNum) throws SQLException {
return rs.getShortOrNull(0);
}
}
For use as Spring-Jdbc
RowMapper
, add extends SpringRowMapper<T>
to ExtendedRowMapper implementation.
class EntityRowMapper extends SpringRowMapper<Entity> implements ExtendedRowMapper<Entity> {
@Override
public Entity mapRow(ExtendedResultSet rs, int rowNum) {
Entity entity = new Entity();
// get values from rs and set to entity
return entity;
}
}
License
This project is licensed under MIT License.