另请参阅文档中的上一页,该页面显示了如何链接您的Handle
或DBI
与映射器。
本质上,您需要一个映射器来将其转换ResultSet
为所需的对象,并需要一个接口来引用映射器。
让我们假设一个最小的例子。首先需要提供映射器:
public class CustomizedObjectMapper implements ResultSetMapper<customizedObject> {
@Override
public customizedObject map(int index, ResultSet r, StatementContext ctx)
throws SQLException {
return new customizedObject(r.getString("uuid"), r.getString("other_column"));
}
}
然后我们需要一个接口来定义哪个查询提供了传递给映射器类的数据。一个结果行导致一次调用CustomizedObjectMapper.map(...)
:
@RegisterMapper(CustomizeObjectMapper.class)
public interface CustomizeObjectQuery {
@SqlQuery("Select uuid, other_column from schema.relation")
List<customizedObject> get();
}
最后,可以检索对象:List<customizedObject> test = dbi.open(CustomizeObjectQuery.class).get()
.
您也可以像这样将组件单独放在一起并省略接口:
dbi.open().createQuery("Select uuid, other_colum from schema.relation").map(new EventMapper()).list()