Skip to content

Commit bce2c49

Browse files
committed
Merge pull request #662 from kazuki43zoo/issues/661_cursor-on-annotaion-based-mapper
Support Cursor on annotation based Mapper interface #661
2 parents 4a79005 + 47777bb commit bce2c49

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.apache.ibatis.builder.IncompleteElementException;
6363
import org.apache.ibatis.builder.MapperBuilderAssistant;
6464
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
65+
import org.apache.ibatis.cursor.Cursor;
6566
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
6667
import org.apache.ibatis.executor.keygen.KeyGenerator;
6768
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
@@ -390,7 +391,7 @@ private Class<?> getReturnType(Method method) {
390391
} else if (resolvedReturnType instanceof ParameterizedType) {
391392
ParameterizedType parameterizedType = (ParameterizedType) resolvedReturnType;
392393
Class<?> rawType = (Class<?>) parameterizedType.getRawType();
393-
if (Collection.class.isAssignableFrom(rawType)) {
394+
if (Collection.class.isAssignableFrom(rawType) || Cursor.class.isAssignableFrom(rawType)) {
394395
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
395396
if (actualTypeArguments != null && actualTypeArguments.length == 1) {
396397
Type returnTypeParameter = actualTypeArguments[0];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2009-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.cursor_simple;
17+
18+
import org.apache.ibatis.annotations.Select;
19+
import org.apache.ibatis.cursor.Cursor;
20+
21+
public interface AnnotationMapper {
22+
23+
@Select("select * from users order by id")
24+
Cursor<User> getAllUsers();
25+
26+
}

src/test/java/org/apache/ibatis/submitted/cursor_simple/CursorSimpleTest.java

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,7 +30,9 @@
3030
import java.io.IOException;
3131
import java.io.Reader;
3232
import java.sql.Connection;
33+
import java.util.ArrayList;
3334
import java.util.Iterator;
35+
import java.util.List;
3436

3537
public class CursorSimpleTest {
3638

@@ -294,4 +296,41 @@ public void testCursorUsageAfterClose() throws IOException {
294296

295297
Assert.fail("Should have returned earlier");
296298
}
299+
300+
@Test
301+
public void shouldGetAllUserUsingAnnotationBasedMapper() {
302+
SqlSession sqlSession = sqlSessionFactory.openSession();
303+
sqlSession.getConfiguration().getMapperRegistry().addMapper(AnnotationMapper.class);
304+
AnnotationMapper mapper = sqlSession.getMapper(AnnotationMapper.class);
305+
Cursor<User> usersCursor = mapper.getAllUsers();
306+
try {
307+
Assert.assertFalse(usersCursor.isOpen());
308+
Assert.assertFalse(usersCursor.isConsumed());
309+
Assert.assertEquals(-1, usersCursor.getCurrentIndex());
310+
311+
List<User> userList = new ArrayList<User>();
312+
for (User user : usersCursor){
313+
userList.add(user);
314+
Assert.assertEquals(userList.size() -1, usersCursor.getCurrentIndex());
315+
}
316+
317+
Assert.assertFalse(usersCursor.isOpen());
318+
Assert.assertTrue(usersCursor.isConsumed());
319+
Assert.assertEquals(3, usersCursor.getCurrentIndex());
320+
321+
Assert.assertEquals(4, userList.size());
322+
User user = userList.get(0);
323+
Assert.assertEquals("User1", user.getName());
324+
user = userList.get(1);
325+
Assert.assertEquals("User2", user.getName());
326+
user = userList.get(2);
327+
Assert.assertEquals("User3", user.getName());
328+
user = userList.get(3);;
329+
Assert.assertEquals("User4", user.getName());
330+
331+
} finally {
332+
sqlSession.close();
333+
}
334+
}
335+
297336
}

0 commit comments

Comments
 (0)