quarkiverse / quarkus-jdbi

Jdbi provides convenient, idiomatic access to relational data in Java
https://jdbi.org/
Apache License 2.0
13 stars 4 forks source link

Documentation/Example code #57

Closed ahalma closed 1 year ago

ahalma commented 1 year ago

Thank you for this initiative to bring JDBI to Quarkus! I'm coming from Dropwizard and I'm new to Quarkus... and without a SQL centric database approach like JDBI I don't know if it is going to work out for me.

Can you please provide an example of how to use this extension?

And possibly clarify if it working well together with

smil2k commented 1 year ago

Hi,

You need to inject AgroalDatasource:

public class JdbiProvider {
    @Inject
    AgroalDataSource ds;

    @Singleton
    @Produces
    public Jdbi schedJDBI() {
        Jdbi jdbi = Jdbi.create(ds);
        jdbi.installPlugin(new SqlObjectPlugin());
        return jdbi;
    }

and you can use it everywhere:

public class UserDAO {
    @Inject
    Jdbi jdbi;

    public Optional<User> findUserById(Jdbi jdbi, long id) {
        return jdbi.inTransaction(transactionHandle ->
                transactionHandle.createQuery("SELECT * FROM users WHERE id=:id")
                        .bind("id", id)
                        .mapTo(User.class)
                        .findFirst());
    }
}
smil2k commented 1 year ago

Connection pool is working per default. Jdbi does not support reactive drivers.

ahalma commented 1 year ago

Thank you for the helpful example!

Concerning the reactive drivers: do you think this is a fundamental issue or likely to be supported at one point?

smil2k commented 1 year ago

I don't think it will support it soon: https://github.com/jdbi/jdbi/issues/1454