This is a rough stab at integrating JOOQ into Dropwizard, as an alternative to the recommended JDBI solution
JDBI works okay for simple queries like select id from person
, but quickly becomes cumbersome when you want to create more complex queries with dynamic order by
and where
clauses.
Also the documentation for JDBI is thin on the ground and hard to dig through.
This is very early code and has no unit tests right now. I would not recommend this for production code unless you really know what you are doing
gradle clean jar
Optionally you can publish this to your local maven repo
gradle publishToMavenLocal
I currently have no idea how to get the JAR up to maven central.
In your application's run
method (remember to change SQLDialect
to your DBMS of choice if not using Postgres)
@Override
public void run(ExampleConfiguration config,
Environment environment) throws ClassNotFoundException {
final JooqDSLContextFactory dslContextFactory = new JooqDSLContextFactory();
this.dslContext = dslContextFactory.build(environment, config.getDataSourceFactory(), SQLDialect.POSTGRES);
final UserDAO dao = new UserDAO(dslContext);
// other application code...
}
A DAO can be something like this
public class UserDAO {
private final DSLContext dslContext;
public UserDAO(final DSLContext dslContext) {
this.dslContext = dslContext;
}
public String findNameById(int id) {
return this.dslContext.select(field("name")).from("users").where(field("id").equal(id)).fetch();
}
}
- See Issues