Skip to content

Commit e5e31e0

Browse files
committed
Related to #618. Multiple resultsets with annotations.
1 parent b6977e6 commit e5e31e0

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

src/main/java/org/apache/ibatis/annotations/Many.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@
2828
@Retention(RetentionPolicy.RUNTIME)
2929
@Target(ElementType.METHOD)
3030
public @interface Many {
31+
3132
String select() default "";
3233

3334
FetchType fetchType() default FetchType.DEFAULT;
3435

36+
String resultSet() default "";
37+
38+
String resultMap() default "";
39+
40+
String foreignColumn() default "";
41+
3542
}

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,18 +511,46 @@ private void applyResults(Result[] results, Class<?> resultType, List<ResultMapp
511511
result.javaType() == void.class ? null : result.javaType(),
512512
result.jdbcType() == JdbcType.UNDEFINED ? null : result.jdbcType(),
513513
hasNestedSelect(result) ? nestedSelectId(result) : null,
514-
null,
514+
hasResultMap(result) ? getResultMap(result) : null,
515515
null,
516516
null,
517517
typeHandler,
518518
flags,
519-
null,
520-
null,
519+
hasResultSet(result) ? getResultSet(result) : null,
520+
hasForeignColumn(result) ? getForeignColumn(result) : null,
521521
isLazy(result));
522522
resultMappings.add(resultMapping);
523523
}
524524
}
525525

526+
private boolean hasResultSet(Result result) {
527+
return result.many().resultSet().length() > 0;
528+
}
529+
530+
private boolean hasResultMap(Result result) {
531+
return result.many().resultMap().length() > 0;
532+
}
533+
534+
private boolean hasForeignColumn(Result result) {
535+
return result.many().foreignColumn().length() > 0;
536+
}
537+
538+
private String getResultSet(Result result) {
539+
return result.many().resultSet();
540+
}
541+
542+
private String getResultMap(Result result) {
543+
String nestedResultMap = result.many().resultMap();
544+
if (!nestedResultMap.contains(".")) {
545+
nestedResultMap = type.getName() + "." + nestedResultMap;
546+
}
547+
return nestedResultMap;
548+
}
549+
550+
private String getForeignColumn(Result result) {
551+
return result.many().foreignColumn();
552+
}
553+
526554
private String nestedSelectId(Result result) {
527555
String nestedSelect = result.one().select();
528556
if (nestedSelect.length() < 1) {

src/test/java/org/apache/ibatis/submitted/sptests/SPMapper.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.List;
1919
import java.util.Map;
2020

21+
import org.apache.ibatis.annotations.Many;
2122
import org.apache.ibatis.annotations.Options;
2223
import org.apache.ibatis.annotations.Param;
2324
import org.apache.ibatis.annotations.Result;
@@ -47,6 +48,15 @@ public interface SPMapper {
4748

4849
List<Name> getNamesAndItemsLinkedById(int id);
4950

51+
@Select("{call sptest.getnamesanditems()}")
52+
@Results({
53+
@Result(property = "firstName", column = "FIRST_NAME"),
54+
@Result(property = "lastName", column = "LAST_NAME"),
55+
@Result(property = "items", column = "ID", many = @Many(resultMap = "itemResult", resultSet = "items", foreignColumn = "name_id"))
56+
})
57+
@Options(resultSets = "names,items", statementType = StatementType.CALLABLE)
58+
List<Name> getNamesAndItemsLinkedAnnotationBased();
59+
5060
Object echoDate(Map<String, Object> parameter); // issue #145
5161

5262
// annotated
@@ -103,4 +113,4 @@ public interface SPMapper {
103113
@Options(statementType = StatementType.CALLABLE)
104114
List<List<?>> getNamesAndItemsAnnotatedWithXMLResultMapArray();
105115

106-
}
116+
}

src/test/java/org/apache/ibatis/submitted/sptests/SPTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,4 +858,21 @@ public void testGetNamesAndItemsLinkedWithNoMatchingInfo() throws SQLException {
858858
}
859859
}
860860

861+
@Test
862+
public void testGetNamesAndItemsLinkedAnnotationBased() throws SQLException {
863+
SqlSession sqlSession = sqlSessionFactory.openSession();
864+
try {
865+
SPMapper spMapper = sqlSession.getMapper(SPMapper.class);
866+
867+
List<Name> names = spMapper.getNamesAndItemsLinkedAnnotationBased();
868+
assertEquals(4, names.size());
869+
assertEquals(2, names.get(0).getItems().size());
870+
assertEquals(1, names.get(1).getItems().size());
871+
assertNull(names.get(2).getItems());
872+
assertNull(names.get(3).getItems());
873+
} finally {
874+
sqlSession.close();
875+
}
876+
}
877+
861878
}

0 commit comments

Comments
 (0)