Getting Started JDBI

Maven Central JavaDoc

Setting up the environment

Add the Dependency to your build.

for Maven:

Java 8, 9, 10 , 11 no module-info

<dependency>
    <groupId>org.simpleflatmapper</groupId>
    <artifactId>sfm-jdbi</artifactId>
    <version>8.2.3</version>
</dependency>

Java 6, 7

<dependency>
    <groupId>org.simpleflatmapper</groupId>
    <artifactId>sfm-jdbi-jre6</artifactId>
    <version>8.2.3</version>
</dependency>

Java 9, 10 , 11 with module-info

<dependency>
    <groupId>org.simpleflatmapper</groupId>
    <artifactId>sfm-jdbi-jre9</artifactId>
    <version>8.2.3</version>
</dependency>

From 7.0.0 you will need explicitly include the jdbi dependency in your pom.

Register SfmResultSetMapperFactory

You can register sfm as a ResultSetMapperFactory, then use the mapTo - in place of map - it will JDBI will look for a registered Factory and is the SFM one is the only or first one it will use it to map the object.

DBI dbi = new DBI(datasource);
dbi.registerMapper(new SfmResultSetMapperFactory());
Handle handle = dbi.open();
try {
    DbObject dbObject = handle.createQuery("SELECT * FROM T1").mapTo(DbObject.class).first();
} finally {
    handle.close();
}

Annotation @RegisterMapperFactory

You can also annotate your method or dao interface with

@RegisterMapperFactory(value = {SfmResultSetMapperFactory.class})
public interface MyDao
{
    @SqlQuery("SELECT * FROM TEST_DB_OBJECT where id = :id")
    DbObject selectOne(@Bind("id") long id);

}

Annotation @SfmBind

SFM also provide a binding annotation equivalent of @BindBean but using the SFM mapping.

public interface MyDao
{
    @SqlUpdate("insert into TEST_DB_OBJECT (id, name, email, creation_time, type_ordinal, type_name) values (:id, :name, :email, :creation_time, :type_ordinal, :type_name)")
    void insert(@SfmBind(sqlTypes = {@SqlType(name ="type_ordinal", type=Types.NUMERIC)}) DbObject s);
}