File tree 3 files changed +31
-2
lines changed
src/test/java/org/apache/ibatis/submitted/stringlist
3 files changed +31
-2
lines changed Original file line number Diff line number Diff line change 16
16
package org .apache .ibatis .submitted .stringlist ;
17
17
18
18
import java .util .List ;
19
+ import java .util .Map ;
19
20
20
21
public interface Mapper {
21
22
22
23
List <User > getUsersAndGroups (Integer id );
23
24
25
+ List <Map <String , Object >> getUsersAndGroupsMap (Integer id );
26
+
24
27
}
Original file line number Diff line number Diff line change 23
23
<mapper namespace =" org.apache.ibatis.submitted.stringlist.Mapper" >
24
24
25
25
<select id =" getUsersAndGroups" resultMap =" results" >
26
- select * from users
26
+ select * from users where id = #{id}
27
27
</select >
28
28
29
29
<resultMap type =" org.apache.ibatis.submitted.stringlist.User" id =" results" >
36
36
</collection >
37
37
</resultMap >
38
38
39
+ <select id =" getUsersAndGroupsMap" resultMap =" mapResults" >
40
+ select * from users where id = #{id}
41
+ </select >
42
+
43
+ <resultMap type =" map" id =" mapResults" >
44
+ <id column =" id" property =" id" />
45
+ <collection property =" groups" ofType =" string" javaType =" list" >
46
+ <result column =" group_id" />
47
+ </collection >
48
+ <collection property =" roles" ofType =" string" javaType =" list" >
49
+ <result column =" rol_id" />
50
+ </collection >
51
+ </resultMap >
52
+
39
53
</mapper >
Original file line number Diff line number Diff line change 19
19
20
20
import java .io .Reader ;
21
21
import java .util .List ;
22
+ import java .util .Map ;
22
23
23
24
import org .apache .ibatis .BaseDataTest ;
24
25
import org .apache .ibatis .exceptions .PersistenceException ;
@@ -47,7 +48,7 @@ static void setUp() throws Exception {
47
48
}
48
49
49
50
@ Test
50
- void shouldGetAUser () {
51
+ void shouldMapListOfStrings () {
51
52
try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
52
53
Mapper mapper = sqlSession .getMapper (Mapper .class );
53
54
List <User > users = mapper .getUsersAndGroups (1 );
@@ -57,6 +58,17 @@ void shouldGetAUser() {
57
58
}
58
59
}
59
60
61
+ @ Test
62
+ void shouldMapListOfStringsToMap () {
63
+ try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
64
+ Mapper mapper = sqlSession .getMapper (Mapper .class );
65
+ List <Map <String , Object >> results = mapper .getUsersAndGroupsMap (1 );
66
+ Assertions .assertEquals (1 , results .size ());
67
+ Assertions .assertEquals (2 , ((List <?>)results .get (0 ).get ("groups" )).size ());
68
+ Assertions .assertEquals (2 , ((List <?>)results .get (0 ).get ("roles" )).size ());
69
+ }
70
+ }
71
+
60
72
@ Test
61
73
void shouldFailFastIfCollectionTypeIsAmbiguous () throws Exception {
62
74
try (Reader reader = Resources
You can’t perform that action at this time.
0 commit comments