1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.multipleresultsetswithassociation;
17
18 import java.io.Reader;
19 import java.sql.Connection;
20 import java.util.List;
21
22 import org.apache.ibatis.io.Resources;
23 import org.apache.ibatis.jdbc.ScriptRunner;
24 import org.apache.ibatis.session.SqlSession;
25 import org.apache.ibatis.session.SqlSessionFactory;
26 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27 import org.junit.jupiter.api.Assertions;
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.Test;
30
31
32
33
34
35
36 class MultipleResultSetTest {
37
38 private static SqlSessionFactory sqlSessionFactory;
39
40 @BeforeAll
41 static void setUp() throws Exception {
42 try (Reader reader = Resources
43 .getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/mybatis-config.xml")) {
44 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
45 }
46
47
48
49
50 try (SqlSession session = sqlSessionFactory.openSession(); Connection conn = session.getConnection()) {
51 try (Reader reader = Resources
52 .getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/CreateDB1.sql")) {
53 runReaderScript(conn, reader);
54 }
55 try (Reader reader = Resources
56 .getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/CreateDB2.sql")) {
57 runReaderScript(conn, reader);
58 }
59 try (Reader reader = Resources
60 .getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/CreateDB3.sql")) {
61 runReaderScript(conn, reader);
62 }
63 }
64 }
65
66 private static void runReaderScript(Connection conn, Reader reader) {
67 ScriptRunner runner = new ScriptRunner(conn);
68 runner.setLogWriter(null);
69 runner.setSendFullScript(true);
70 runner.setAutoCommit(true);
71 runner.setStopOnError(false);
72 runner.runScript(reader);
73 }
74
75 @Test
76 void shouldGetOrderDetailsEachHavingAnOrderHeader() {
77 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
78 Mapper mapper = sqlSession.getMapper(Mapper.class);
79 List<OrderDetail> orderDetails = mapper.getOrderDetailsWithHeaders();
80
81
82
83 Assertions.assertEquals(6, orderDetails.size());
84
85
86
87 for (OrderDetail orderDetail : orderDetails) {
88 Assertions.assertNotNull(orderDetail.getOrderHeader());
89 }
90 }
91 }
92
93 @Test
94 void shouldGetOrderDetailsEachHavingAnOrderHeaderAnnotationBased() {
95 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
96 Mapper mapper = sqlSession.getMapper(Mapper.class);
97 List<OrderDetail> orderDetails = mapper.getOrderDetailsWithHeadersAnnotationBased();
98
99
100
101 Assertions.assertEquals(6, orderDetails.size());
102
103
104
105 for (OrderDetail orderDetail : orderDetails) {
106 Assertions.assertNotNull(orderDetail.getOrderHeader());
107 }
108 }
109 }
110
111 }