当使用 myBatis 时,我应该从数据库中获取非常大的结果集并进行后续操作。 (例如 CSV 导出)
我在想,也担心如果返回类型是List,所有返回的数据都在我的内存中,会导致OutOfMemoryException。
因此,我想使用 myBatis 将结果作为 ResultSet 或 Iterable
告诉我任何解决方案。
请您参考如下方法:
从mybatis 3.4.1开始可以返回Cursor它是 Iterable
并且可以像这样使用(在结果有序的情况下,请参阅上面的 Cursor
API java doc 了解详细信息):
MyEntityMapper.java
@Select({
"SELECT *",
"FROM my_entity",
"ORDER BY id"
})
Cursor<MyEntity> getEntities();
MapperClient.java
MyEntityMapper mapper = session.getMapper(MyEntityMapper.class);
try (Cursor<MyEntity> entities = mapper.getEntities()) {
for (MyEntity entity:entities) {
// process one entity
}
}