ObjectAPI形式で戻り値を勝手に推測してもらう
今までもずっと使ってたんですけど、ObjectAPI形式の場合は @SqlQuery アノテーションを付けたメソッドの戻り値によって、JDBIが勝手に推測して相応の処理をしてくれます。
Dao003.java
public interface Dao003 { @SqlQuery("select id from table001") List<Integer> findIds(); @SqlQuery("select name from table001") List<String> findNames(); @SqlQuery("select * from table001") List<Map<String,Object>> findAll(); // 残念ながらこれはダメ }
Sample008.java
public static void main(String[] args) { String url = "jdbc:postgresql://192.168.52.128/jdbi"; DBI dbi = new DBI(url, "jdbi_user", "jdbi_pass"); Dao003 dao003 = dbi.onDemand(Dao003.class); List<Integer> ids = dao003.findIds(); System.out.println("ids count = " + ids.size()); for( Integer id : ids ) { System.out.println("id = " + id); } List<String> names = dao003.findNames(); System.out.println("names count = " + names.size()); for( String name : names ) { System.out.println("name = " + name); } List<Map<String,Object>> rows = dao003.findAll(); // ここで例外「org.skife.jdbi.v2.MappingRegistry$1: No mapper registered for java.util.Map」 System.out.println("rows count = " + rows.size()); for( Map<String,Object> row : rows ) { System.out.println("id = " + row.get("id") + ", name = " + row.get("name")); } }
List