如何在jDBI中进行查询?

Moh*_*rma 31 java sql jdbi

如何在jDBI中执行这样的事情?

@SqlQuery("select id from foo where name in <list of names here>")
List<Integer> getIds(@Bind("nameList") List<String> nameList);
Run Code Online (Sandbox Code Playgroud)

表: foo(id int,name varchar)

与myBatis的@SelectProvider类似.

类似的问题已经被问到如何使用JDBI的Sql Object API在运行时创建动态Sql查询?,但不知何故,答案对我来说并不清楚.

Dei*_*del 34

这应该工作:

@SqlQuery("select id from foo where name in (<nameList>)")
List<Integer> getIds(@BindIn("nameList") List<String> nameList);
Run Code Online (Sandbox Code Playgroud)

不要忘记使用以下方法注释包含此方法的类:

@UseStringTemplate3StatementLocator
Run Code Online (Sandbox Code Playgroud)

注释(因为引擎盖下JDBI使用Apache StringTemplate进行此类替换).另请注意,使用此注释时,不能在不进行转义的情况下在SQL查询中使用"<"字符(因为它是StringTemplate使用的特殊符号).

  • 弄清楚了.你应该做`\\ <` (12认同)
  • 如何在查询中转义`<`? (3认同)

小智 8

使用@Define批注在jDBI中构建动态查询.例:

@SqlUpdate("insert into <table> (id, name) values (:id, :name)")
public void insert(@Define("table") String table, @BindBean Something s);

@SqlQuery("select id, name from <table> where id = :id")
public Something findById(@Define("table") String table, @Bind("id") Long id);
Run Code Online (Sandbox Code Playgroud)


小智 6

使用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
                    }
                }
            };
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:ANY不是ANSI SQL标准的一部分,因此这会对PostgreSQL产生硬依赖.


Ale*_*ing 5

如果您使用的是 JDBI 3 Fluent API,您可以使用bindList()一个属性:

List<String> keys = new ArrayList<String>()
keys.add("user_name");
keys.add("street");

handle.createQuery("SELECT value FROM items WHERE kind in (<listOfKinds>)")
      .bindList("listOfKinds", keys)
      .mapTo(String.class)
      .list();

// Or, using the 'vararg' definition
handle.createQuery("SELECT value FROM items WHERE kind in (<varargListOfKinds>)")
      .bindList("varargListOfKinds", "user_name", "docs", "street", "library")
      .mapTo(String.class)
      .list();
Run Code Online (Sandbox Code Playgroud)

请注意查询字符串如何使用<listOfKinds>而不是通常的:listOfKinds.

文档在这里:http : //jdbi.org/#_binding_arguments