让 JDBI 映射自定义查询的结果

Let JDBI map the results for a custom query

我想做一个复杂的查询,让JDBI处理结果映射。通常,我会做这样的事情:

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

由于查询是在运行时生成的,我不能这样做,但我仍然想使用结果集映射功能。我试过使用 Handle 接口:

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

但我没有找到通过 arg 的方法。我可以将其字符串连接到生成的查询中,但我宁愿像使用 PreparedStatement.

一样传递它

我相信你想利用 bind

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