1
1
/*
2
- * Copyright 2009-2024 the original author or authors.
2
+ * Copyright 2009-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
23
23
import java .util .List ;
24
24
import java .util .Map ;
25
25
import java .util .Optional ;
26
+ import java .util .Spliterator ;
27
+ import java .util .Spliterators ;
28
+ import java .util .stream .Stream ;
29
+ import java .util .stream .StreamSupport ;
26
30
27
31
import org .apache .ibatis .annotations .Flush ;
28
32
import org .apache .ibatis .annotations .MapKey ;
43
47
* @author Eduardo Macarron
44
48
* @author Lasse Voss
45
49
* @author Kazuki Shimizu
50
+ * @author Yanming Zhou
46
51
*/
47
52
public class MapperMethod {
48
53
@@ -82,6 +87,9 @@ public Object execute(SqlSession sqlSession, Object[] args) {
82
87
result = executeForMap (sqlSession , args );
83
88
} else if (method .returnsCursor ()) {
84
89
result = executeForCursor (sqlSession , args );
90
+ } else if (method .returnsStream ()) {
91
+ Cursor <?> cursor = executeForCursor (sqlSession , args );
92
+ result = StreamSupport .stream (Spliterators .spliteratorUnknownSize (cursor .iterator (), Spliterator .ORDERED ), false );
85
93
} else {
86
94
Object param = method .convertArgsToSqlCommandParam (args );
87
95
result = sqlSession .selectOne (command .getName (), param );
@@ -274,6 +282,7 @@ public static class MethodSignature {
274
282
private final boolean returnsMap ;
275
283
private final boolean returnsVoid ;
276
284
private final boolean returnsCursor ;
285
+ private final boolean returnsStream ;
277
286
private final boolean returnsOptional ;
278
287
private final Class <?> returnType ;
279
288
private final String mapKey ;
@@ -293,6 +302,7 @@ public MethodSignature(Configuration configuration, Class<?> mapperInterface, Me
293
302
this .returnsVoid = void .class .equals (this .returnType );
294
303
this .returnsMany = configuration .getObjectFactory ().isCollection (this .returnType ) || this .returnType .isArray ();
295
304
this .returnsCursor = Cursor .class .equals (this .returnType );
305
+ this .returnsStream = Stream .class .equals (this .returnType );
296
306
this .returnsOptional = Optional .class .equals (this .returnType );
297
307
this .mapKey = getMapKey (method );
298
308
this .returnsMap = this .mapKey != null ;
@@ -341,6 +351,10 @@ public boolean returnsCursor() {
341
351
return returnsCursor ;
342
352
}
343
353
354
+ public boolean returnsStream () {
355
+ return returnsStream ;
356
+ }
357
+
344
358
/**
345
359
* return whether return type is {@code java.util.Optional}.
346
360
*
0 commit comments