public class Jdbi extends Object implements org.jooby.Jooby.Module
DBI, Handle and SQL Objects (a.k.a DAO).
This module depends on Jdbc module.
It is pretty straightforward:
{
use(new Jdbc());
use(new Jdbi());
get("/", req -> {
DBI dbi = req.require(DBI.class);
// ... work with dbi
});
get("/handle", req -> {
try (Handle handle = req.require(Handle.class)) {
// ... work with dbi handle
}
});
}
A call to req.require(Handle.class) is the same as:
req.require(DBI.class).open(Handle.class).
public interface MyRepository extends Closeable {
@SqlUpdate("create table something (id int primary key, name varchar(100))")
void createSomethingTable();
@SqlUpdate("insert into something (id, name) values (:id, :name)")
void insert(@ind("id") int id, @Bind("name") String name);
@SqlQuery("select name from something where id = :id")
String findNameById(@Bind("id") int id);
}
...
{
use(new Jdbc());
use(new Jdbi());
get("/handle", req -> {
try (MyRepository h = req.require(MyRepository.class)) {
h.createSomethingTable();
h.insert(1, "Jooby");
String name = h.findNameById(1);
return name;
}
});
}
If you need to configure and/or customize a DBI instance, just do:
{
use(new Jdbc());
use(new Jdbi().doWith((dbi, config) -> {
// set custom option
}));
}
That's all folks! Enjoy it!!!
| Modifier and Type | Field and Description |
|---|---|
static String |
ARG_FACTORIES |
| Constructor and Description |
|---|
Jdbi(Class<?>... sqlObjects) |
Jdbi(String db,
Class<?>... sqlObjects) |
| Modifier and Type | Method and Description |
|---|---|
void |
configure(org.jooby.Env env,
com.typesafe.config.Config config,
com.google.inject.Binder binder) |
Jdbi |
doWith(BiConsumer<org.skife.jdbi.v2.DBI,com.typesafe.config.Config> configurer)
Configure DBI instance.
|
Jdbi |
doWith(Consumer<org.skife.jdbi.v2.DBI> configurer)
Configure DBI instance.
|
public static final String ARG_FACTORIES
public Jdbi(Class<?>... sqlObjects)
public void configure(org.jooby.Env env,
com.typesafe.config.Config config,
com.google.inject.Binder binder)
configure in interface org.jooby.Jooby.Modulepublic Jdbi doWith(BiConsumer<org.skife.jdbi.v2.DBI,com.typesafe.config.Config> configurer)
configurer - Configurer.Copyright © 2021. All rights reserved.