Skip to content

Commit 50e2f10

Browse files
committed
add test case
1 parent 5e2fc17 commit 50e2f10

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/MyBatisTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ void test() {
3333
try (SqlSession sqlSession = sqlSessionFactory.openSession()){
3434
UserDao mapper = sqlSession.getMapper(UserDao.class);
3535
List<User> users = mapper.flindAll();
36-
System.out.println(users);
3736
Assertions.assertNotNull(users, "users must not be null");
3837
Assertions.assertEquals(4, users.size(), "should return 4 results");
3938
Assertions.assertEquals(2, users.get(0).getRoles().size(), "should have 2 roles");
@@ -45,11 +44,22 @@ void test2() {
4544
try (SqlSession sqlSession = sqlSessionFactory.openSession()){
4645
UserDao mapper = sqlSession.getMapper(UserDao.class);
4746
List<User> users = mapper.flindAll2();
48-
System.out.println(users);
4947
Assertions.assertNotNull(users, "users must not be null");
5048
Assertions.assertEquals(4, users.size(), "should return 4 results");
5149
Assertions.assertEquals(2, users.get(0).getRoles().size(), "should have 2 roles");
5250
}
5351
}
5452

53+
@Test
54+
void test3() {
55+
try (SqlSession sqlSession = sqlSessionFactory.openSession()){
56+
UserDao mapper = sqlSession.getMapper(UserDao.class);
57+
List<User> users = mapper.flindAll3();
58+
Assertions.assertNotNull(users, "users must not be null");
59+
Assertions.assertEquals(2, users.size(), "should return 2 results");
60+
Assertions.assertEquals(1, users.get(0).getRoles().size(), "should have 1 roles");
61+
Assertions.assertEquals("老师", users.get(0).getRoles().get(0).getRoleName(), "should have 1 roles");
62+
}
63+
}
64+
5565
}

src/test/java/org/apache/ibatis/submitted/annotion_many_one_add_resultmapid/UserDao.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
*/
44
package org.apache.ibatis.submitted.annotion_many_one_add_resultmapid;
55

6-
import org.apache.ibatis.annotations.Many;
7-
import org.apache.ibatis.annotations.Result;
8-
import org.apache.ibatis.annotations.Results;
9-
import org.apache.ibatis.annotations.Select;
6+
import org.apache.ibatis.annotations.*;
107

118
import java.util.List;
129

@@ -42,4 +39,16 @@ public interface UserDao {
4239
@Result(property = "roles",many = @Many(resultMapId = "org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao.roleMap2"))
4340
})
4441
public List<User> flindAll2();
42+
43+
@Select("select\n" +
44+
" u.id,u.username,r.id role_id,r.role_name\n" +
45+
" from user u\n" +
46+
" left join user_role ur on u.id =ur.user_id\n" +
47+
" left join role r on ur.role_id = r.id where u.id in (2,3)")
48+
@Results({
49+
@Result(id = true,column="id",property = "id"),
50+
@Result(column="username",property = "username"),
51+
@Result(property = "roles",one = @One(resultMapId = "org.apache.ibatis.submitted.annotion_many_one_add_resultmapid.RoleDao.roleMap2"))
52+
})
53+
public List<User> flindAll3();
4554
}

0 commit comments

Comments
 (0)