Ninja Framework から JOOQ を使用してデータベースにアクセスしたいと考えています。コントローラーから JDBC 接続を取得するにはどうすればよいですか?
以下は、うまく機能しないことがわかったリソースです。
持続性ユニットが使用するデータソースをプログラムで取得する方法- EntityManager から接続を取得するための面倒な一連の手順。
http://blog.jooq.org/2015/05/26/type-safe-queries-for-jpas-native-query-api/ - JOOQ でクエリを作成し、EntityManager.createNativeQuery に渡すことで機能します。機能的ですが、単に接続するだけでは十分ではありません。
次のように接続をコントローラーに挿入できますか。
public Result myController(@DBConnection Connection connection) {
List<String> articles = DSL.using(connection).selectFrom(ARTICLE).fetch(ARTICLE.TITLE);
return Results.html().render("template", articles);
}
DropWizards には勝者のように見えるプラグインがあります: https://github.com/benjamin-bader/droptools/tree/master/dropwizard-jooq
public BlogPost getPost(@QueryParam("id") int postId, @Context DSLContext database) {
BlogPostRecord post = database
.selectFrom(POST)
.where(POST.ID.equal(postId))
.fetchOne();
// do stuff
}