bekkou68 の日記

Gogengo! や IT 技術など。

JdbcManager の selectBySqlFile() で SQL ファイルの IN 句にコレクションを渡す - Put a Collection to IN Phrase of SQL File by selectBySqlFile() of JdbcManager

実装 - Implementation

EntityService.java
/* JavaBeans のフィールドが Integer[] の場合 - In the case of field of JavaBeans is Integer[] */
public class EntityService extends AbstractService<Entity> {
    public class Param {
        public Integer[] ids;
    }

    public List<Entity> findEntities() {
        Param param = new Param();
        param.ids = new Integer[] {1, 2};
        return selectBySqlFile(Entity.class, "inTest.sql", param).getResultList();
    }
}
/* JavaBeans のフィールドが List<Integer> の場合 - In the case of field of JavaBeans is List<Integer> */
public class EntityService extends AbstractService<Entity> {
    public class Param {
        public List<Integer> ids;
    }

    public List<Entity> findEntities() {
        Param param = new Param();
        param.ids = new ArrayList<Integer>();
        param.ids.add(1);
        param.ids.add(2);
        return selectBySqlFile(Entity.class, "inTest.sql", param).getResultList();
    }
}
inTest.sql
SELECT * FROM entity WHERE id IN /*ids*/()
おまけ(inTest.sql を色々書き換えた) - Appendix (Tried Several Writing Style of inTest.sql)
SELECT * FROM entity WHERE id IN/*ids*/ -- NG
SELECT * FROM entity WHERE id IN /*ids*/ -- NG
SELECT * FROM entity WHERE id IN/*ids*/() -- OK
SELECT * FROM entity WHERE id IN /*ids*/() -- OK
SELECT * FROM entity WHERE id IN ()/*ids*/ -- NG
SELECT * FROM entity WHERE id IN/*ids*/(1) -- OK
SELECT * FROM entity WHERE id IN/*ids*/(1, 2) -- OK
SELECT * FROM entity WHERE id IN/*ids*/(1, 2, 3) -- OK

/*xxx*/ の後にリテラルを書くと、その型で渡すという書き方になるのだろうか? - I wonder that if I write a literal after /*xxx*/ then the writing style says put a value with a type of the literal.