org.skife.jdbi.v2.sqlobject.package.html Maven / Gradle / Ivy
SQL Objects
The sql objects API allows for declarative definition of interfaces which will handle
the generation of sql statements and queries on your behalf when needed. Take the following interface:
public interface TheBasics
{
{@literal @}SqlUpdate("insert into something (id, name) values (:id, :name)")
int insert({@literal @}BindBean Something something);
{@literal @}SqlQuery("select id, name from something where id = :id")
Something findById({@literal @}Bind("id") long id);
}
You obtain an instance of TheBasics
via one of three means, the first opens a new
handle against a DBI instance, the second attaches the object to an already open handle, and the third
will obtain and release connections on demand against a DBI instance.
DBI dbi = new DBI(dataSource);
Handle handle = dbi.open();
TheBasics via_open = dbi.open(TheBasics.class);
TheBasics attached = handle.attach(TheBasics.class);
TheBasics on_demand = dbi.onDemand(TheBasics.class);
Closing Objects
It is important to ensure you close the underlying handle on a sql object if it was opened via the DBI#open in
particular.
If the sql object defines a close() method, or includes it via another interface (such as the
included CloseMe, or java.lang.AutoCloseable) calling that method will close the handle.
On-demand sql objects should not be explicitly closed.
Mixin Interfaces
A number of mixin interfaces are included in the org.skife.jdbi.v2.sqlobject package. These provide additional
functionality on your sql object, such as transactions or access to the underlying Handle instance. To make
use of them just have your sql object interface extend the mixin interface.