2017-02-28 12 views
0

私はSQLiteのJDBI resultsetmapperは、クエリ結果セットからオブジェクトのリストを作成しますか?

リポジトリコード

/** 
* SQLite implementation of Foo Repository 
*/ 
public class FooRepository implements FooRepository { 

    private final DBI connection; 

    /** 
    * The constructor initialises the connection to the local SQLite file 
    * 
    * @param dataSource jdbc connection string e.g. "jdbc:sqlite::resource:db/foo.db" 
    * @throws IllegalArgumentException when an invalid DB file is given 
    */ 
    public FooRepository(final SQLiteDataSource dataSource) { 
     checkNotNull(dataSource, "dataSource required"); 
     connection = new DBI(dataSource); 
    } 

    /** 
    * Returns a list of Foo objects for a website locale in the DB 

    * @return List 
    * @throws SQLException error querying 
    */ 
    @Override 
    public List<Foo> getFoosByWebsiteLocale(f) throws SQLException { 
     checkNotNull(websiteLocale, "websiteLocale required"); 

     final String fooQuery = query... 

     Handle queryHandler = connection.open(); 

     final List<Foo> fooList = queryHandler.createQuery(fooQuery) 
      .map(FooMapper.class); 

     queryHandler.close(); 

     return fooList; 
    } 
} 

マッパー

パブリッククラスFooMapperの実装と私のリポジトリの実装のためJDBIを試してみたかったJPAと春データへの代替を選びます結果セットマッパー{

/** 
    * Construct a Foo object from a record in the result set 
    * @param index row number 
    * @param resultRow row 
    * @param ctx statementcontext 
    * @return Foo object 
    * @throws SQLException when accessing sql result set 
    */ 
    @Override 
    public Foo map(final int index, final ResultSet resultRow, final StatementContext ctx) throws SQLException { 
     return Foo.builder() 
       .name(resultRow.getString("foo_name")) 
       .type(resultRow.getString("foo_type")) 
       .build(); 
    } 
} 

私はResultSetMapperを使用してFooオブジェクトのリストを作成する方法を理解するのに苦労しています。

JDBIのドキュメントでは、この領域に壊れているように見える:

http://jdbi.org/maven_site/apidocs/org/skife/jdbi/v2/tweak/ResultSetMapper.html

ヘルプは、この作業を行う方法については理解されるであろう。

答えて

1

マッパーでは、1つの行を1つのFooオブジェクトにマップするだけで済みます。 JDBIはリストを作成し、オブジェクトをリストに入れます。 私は:

final List<Foo> fooList = queryHandler.createQuery(fooQuery).map(FooMapper.class).list(); 
+0

申し訳ありませんが、コードソリューションを教えてください。 – kaleeway

+0

クエリを実行するコード行を次のように変更してください:finalリスト fooList = queryHandler.createQuery(fooQuery) .map(FooMapper.class).list(); – jenarros

関連する問題