How to do in-query in jDBI?

后端 未结 4 618
轮回少年
轮回少年 2020-12-05 17:11

How can I execute somethings like this in jDBI ?

@SqlQuery(\"select id from foo where name in \")
List getIds(@Bind(         


        
4条回答
  •  爱一瞬间的悲伤
    2020-12-05 18:05

    If you are using the JDBI 3 Fluent API, you can use bindList() with an attribute:

    List keys = new ArrayList()
    keys.add("user_name");
    keys.add("street");
    
    handle.createQuery("SELECT value FROM items WHERE kind in ()")
          .bindList("listOfKinds", keys)
          .mapTo(String.class)
          .list();
    
    // Or, using the 'vararg' definition
    handle.createQuery("SELECT value FROM items WHERE kind in ()")
          .bindList("varargListOfKinds", "user_name", "docs", "street", "library")
          .mapTo(String.class)
          .list();
    

    Note how the query string uses instead of the usual :listOfKinds.

    Documentation is here: http://jdbi.org/#_binding_arguments

提交回复
热议问题