使用PostgreSQL,我能够使用ANY比较并将集合绑定到数组来实现这一点。
public interface Foo {
@SqlQuery("SELECT id FROM foo WHERE name = ANY (:nameList)")
List<Integer> getIds(@BindStringList("nameList") List<String> nameList);
}
@BindingAnnotation(BindStringList.BindFactory.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface BindStringList {
String value() default "it";
class BindFactory implements BinderFactory {
@Override
public Binder build(Annotation annotation) {
return new Binder<BindStringList, Collection<String>>() {
@Override
public void bind(SQLStatement<?> q, BindStringList bind, Collection<String> arg) {
try {
Array array = q.getContext().getConnection().createArrayOf("varchar", arg.toArray());
q.bindBySqlType(bind.value(), array, Types.ARRAY);
} catch (SQLException e) {
// handle error
}
}
};
}
}
}
注意:ANY不是ANSI SQL标准的一部分,所以这造成了对PostgreSQL的硬依赖。