Search code examples
javasqldropwizardjdbi

How to input list of string to a sql using BindBeans


For some reason i can not use BindIn but have to use BindBeans to pass in a list of string values for in clause. I have below, but seems can not pass the Types as i wanted. Any advise please?

*MyFilter {
private final String Types; 
private final Timestamp Date;
 public MyFilter (){
    this.Types = "A','B"
    THIS.Date = now();
 }

}
@SqlQuery("select * from table where type in (:Types) and date = :Date  ")
public abstract List<xx> get(@BindBean MyFilter filter);*

Solution

  • If you want some dynamic part(s) of SQL you can use stringtemplates.

    @UseStringTemplate3StatementLocator
    public abstract class MyDAO {
    
        @SqlQuery("select * from table where type in (<types>) and date = :Date  ")
        public abstract List<xx> get(@Define("types") String types);
    
    ....
    

    You will need to add this dependency:

     <dependency>
           <groupId>org.antlr</groupId>
           <artifactId>stringtemplate</artifactId>
           <version>3.2</version>
     </dependency>
    

    In my case, it was very useful for dynamic sorting. Hope this helps.