Getting Started JDBI

Maven Central JavaDoc

Setting up the environment

Add the Dependency to your build.

for Maven:

Java 8

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

Java 6, 7

<dependency>
    <groupId>org.simpleflatmapper</groupId>
    <artifactId>sfm-jdbi</artifactId>
    <version>3.14.2</version>
    <classifier>jdk16</classifier>
</dependency>

Java 9/10

<dependency>
    <groupId>org.simpleflatmapper</groupId>
    <artifactId>sfm-jdbi</artifactId>
    <version>3.14.2</version>
    <classifier>jdk9</classifier>
</dependency>

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);
}