mybatis/mybatis-3 — pull request #52 by gdarmont (2013-05-29 09:00:21+00:00)
Title: Cursor list implementation

Hi mybatis dev team,

I don’t know if creating directly a pull request is the right way to submit a feature for MyBatis, or if there’s another workflow. If so, please let me know.

This PR adds a feature which is needed to implement a MyBatisCursorItemReader for Spring Batch.

The feature allows SqlSession.selectList() to return a custom list implementation (CursorList) that will fetch item “on demand” while iterating over.

CursorList does not retain any fetched element, so it is a perfect choice for iterating over a ResultSet with several millions rows, without exhausting memory.

The drawback is that one can only iterate one time over a CursorList.

If several iterations over the list are needed, LazyList is available. It’s a simple wrapper over CursorList that keeps already fetched elements in a local storage.

Since CursorList fetches elements on demand, it needs to keep the ResultSql open. It’s automatically close when ResultSet is fully consumed. If resultset is partially consumed, it’s user responsability to close CursorList correctly using CursorList#closeResultSetAndStatement().

**Usage :**

XML mapping :
```xml

select id,name,salary,skill from employees order by id

```
Java code :
```java
// Get a list which can have millions of rows
List employees = sqlSession.selectList("getEmployeeNestedCursor");

// Get an iterator on employees
Iterator iter = employees.iterator();

List smallChunk = new ArrayList(10);
while (iter.hasNext()) {
// Fetch next 10 employees
for(int i = 0; i<10 && iter.hasNext(); i++) {
smallChunk.add(iter.next());
}
doSomethingWithAlreadyFetchedEmployees(smallChunk);
smallChunk.clear();
}
```

Best regards,
Guillaume