java.lang.Object
io.jooby.jdbi.JdbiModule
- All Implemented Interfaces:
Extension
Jdbi module: https://jooby.io/modules/hikari.
Usage:
- Add hikari and jdbi dependency
- Install them
{
install(new HikariModule());
install(new JdbiModule());
}
- Use it
{
get("/", ctx -> {
Jdbi jdbi = require(Jdbi.class);
// do with jdbi
});
}
Handle instances are also available:
{
get("/", ctx -> {
try(Handle handle = require(Handle.class)) {
// do with handle
}
});
}
The use of try-with-resources is required here. Handle must be closed once you finish.
For automatic handle managment see the TransactionalRequest
class.
- Since:
- 2.0.0
- Author:
- edgar
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new Jdbi module using thedb
property key.JdbiModule
(String name) Creates a new Jdbi module.JdbiModule
(String name, Function<DataSource, org.jdbi.v3.core.Jdbi> factory) Creates a new Jdbi module using the given jdbi factory.JdbiModule
(Function<DataSource, org.jdbi.v3.core.Jdbi> factory) Creates a new Jdbi module. -
Method Summary
Modifier and TypeMethodDescriptionvoid
sqlObjects
(Class... sqlObjects) Attach SQL objects to a jdbi handle.
-
Constructor Details
-
JdbiModule
public JdbiModule()Creates a new Jdbi module using thedb
property key. This key must be present in the application configuration file, like:db.url = "jdbc:url" db.user = dbuser db.password = dbpass
-
JdbiModule
Creates a new Jdbi module.- Parameters:
name
- The name/key of the data source to attach.
-
JdbiModule
Creates a new Jdbi module. Use the default/first datasource and register objects using thedb
key.- Parameters:
factory
- Jdbi factory.
-
JdbiModule
public JdbiModule(@NonNull String name, @NonNull Function<DataSource, org.jdbi.v3.core.Jdbi> factory) Creates a new Jdbi module using the given jdbi factory.- Parameters:
name
- Name for registering the service.factory
- Jdbi factory.
-
-
Method Details
-
sqlObjects
Attach SQL objects to a jdbi handle.This method simplify the injection or require of SQL objects. So, it is just a shortcut for
Handle.attach(Class)
. Due the SQL objects depends on a Handle this method is only available when theTransactionalRequest
decorator is present.install(new JdbiModule() .sqlObjects(UserDAO.class) ); use(new TransactionalRequest()); get("/users", ctx -> { UserDAO dao = require(UserDAO.class); });
- Parameters:
sqlObjects
- List of SQL object to register as services.- Returns:
- This module.
-
install
-