使用@BindBean 时如何忽略 属性

How to ignore a property when using @BindBean

我在这个简单的查询中使用 JDBI:

@SqlQuery("SELECT id FROM myTable WHERE value = :bean.one")
int search(@BindBean("bean") MyBean bean);

public class Bean { 
  String one; 
  String two; 
  public String getTwo() { throw new IllegalStateException(); }
  // other methods omitted
}

查询中只使用了 one 属性,所以我希望它能正常工作。不幸的是,默认的 bean 映射器首先从 bean 收集所有属性,然后在查询中填充它们。

我可以告诉 JDBI 忽略 bean 属性 或方法以便它不会被调用吗?

这应该被视为 v2 中的一个错误(或至少是一个剪纸)——如果您希望修复它,请report it to the team

作为变通方法,Query.bindFromProperties(bean) 将忽略不在查询中的属性。如果您将 SQL 方法重写为 concrete/default 方法:

interface MyDao extends GetHandle {
  default int search(MyBean bean) {
    return getHandle().createQuery(
            "SELECT id FROM myTable WHERE value = :bean.one")
        .bindFromProperties(bean)
        .mapTo(int.class)
        .first();
  }
}

这已在最新的 v3 alpha 中得到修复,这是值得的。