user8681
user8681

Reputation:

Let JDBI map the results for a custom query

I want to make a complicated query, and let JDBI handle the result mapping. Normally, I would do something like this:

interface MyDao {
  @MapResultAsBean @SqlQuery("hardcoded query with :arg here")
  ResultDto query(@Bind("arg") String arg);
}
ResultDto result = dbi.open(MyDao.class).query(arg);

Since the query is generated at runtime, I cannot do this, but I still want to use the result set mapping features. I've tried using the Handle interface:

String query = generateCustomQuery();
ResultDto result = dbi.open().createQuery(query).mapTo(ResultDto.class).first();

but I don't see a way to pass the arg. I could string-concat it into the generated query, but I'd rather pass it as if using PreparedStatement.

Upvotes: 1

Views: 1177

Answers (1)

Michael Lloyd Lee mlk
Michael Lloyd Lee mlk

Reputation: 14661

I believe you want to make use of bind.

dbi.open().createQuery(query).mapTo(ResultDto.class).bind(":arg", "value").first();

Upvotes: 2

Related Questions