1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.batch_keys;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19
20 import java.io.Reader;
21 import java.sql.Connection;
22 import java.sql.PreparedStatement;
23 import java.sql.ResultSet;
24 import java.sql.ResultSetMetaData;
25 import java.sql.Statement;
26 import java.util.List;
27
28 import org.apache.ibatis.BaseDataTest;
29 import org.apache.ibatis.io.Resources;
30 import org.apache.ibatis.session.ExecutorType;
31 import org.apache.ibatis.session.SqlSession;
32 import org.apache.ibatis.session.SqlSessionFactory;
33 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
34 import org.junit.jupiter.api.Assertions;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37
38 class BatchKeysTest {
39
40 private SqlSessionFactory sqlSessionFactory;
41
42 @BeforeEach
43 void setUp() throws Exception {
44 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/batch_keys/Config.xml")) {
45 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
46 }
47
48 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
49 "org/apache/ibatis/submitted/batch_keys/CreateDB.sql");
50 }
51
52 public void testJdbc3Support() throws Exception {
53 try (Connection conn = sqlSessionFactory.getConfiguration().getEnvironment().getDataSource().getConnection();
54 PreparedStatement stmt = conn.prepareStatement("insert into users2 values(null, 'Pocoyo')",
55 Statement.RETURN_GENERATED_KEYS)) {
56 stmt.addBatch();
57 stmt.executeBatch();
58 try (ResultSet rs = stmt.getGeneratedKeys()) {
59 if (rs.next()) {
60 ResultSetMetaData rsmd = rs.getMetaData();
61 int colCount = rsmd.getColumnCount();
62 do {
63 for (int i = 1; i <= colCount; i++) {
64 String key = rs.getString(i);
65 System.out.println("key " + i + " is " + key);
66 }
67 } while (rs.next());
68 } else {
69 System.out.println("There are no generated keys.");
70 }
71 }
72 }
73 }
74
75 @Test
76 void testInsert() {
77 try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
78 User user1 = new User(null, "Pocoyo");
79 sqlSession.insert("insert", user1);
80 User user2 = new User(null, "Valentina");
81 sqlSession.insert("insert", user2);
82 sqlSession.flushStatements();
83 assertEquals(Integer.valueOf(50), user1.getId());
84 assertEquals(Integer.valueOf(50), user2.getId());
85 sqlSession.commit();
86 }
87 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
88 List<User> users = sqlSession.selectList("select");
89 Assertions.assertEquals(2, users.size());
90 }
91 }
92
93 @Test
94 void testInsertJdbc3() {
95 try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
96 User user1 = new User(null, "Pocoyo");
97 sqlSession.insert("insertIdentity", user1);
98 User user2 = new User(null, "Valentina");
99 sqlSession.insert("insertIdentity", user2);
100 sqlSession.flushStatements();
101 assertEquals(Integer.valueOf(0), user1.getId());
102 assertEquals(Integer.valueOf(1), user2.getId());
103 sqlSession.commit();
104 }
105
106 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
107 List<User> users = sqlSession.selectList("selectIdentity");
108 Assertions.assertEquals(2, users.size());
109 }
110 }
111
112 @Test
113 void testInsertWithMapper() {
114 try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
115 Mapper userMapper = sqlSession.getMapper(Mapper.class);
116 User user1 = new User(null, "Pocoyo");
117 userMapper.insert(user1);
118 User user2 = new User(null, "Valentina");
119 userMapper.insert(user2);
120 sqlSession.flushStatements();
121 assertEquals(Integer.valueOf(50), user1.getId());
122 assertEquals(Integer.valueOf(50), user2.getId());
123 sqlSession.commit();
124 }
125
126 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
127 List<User> users = sqlSession.selectList("select");
128 Assertions.assertEquals(2, users.size());
129 }
130 }
131
132 @Test
133 void testInsertMapperJdbc3() {
134 try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
135 Mapper userMapper = sqlSession.getMapper(Mapper.class);
136 User user1 = new User(null, "Pocoyo");
137 userMapper.insertIdentity(user1);
138 User user2 = new User(null, "Valentina");
139 userMapper.insertIdentity(user2);
140 sqlSession.flushStatements();
141 assertEquals(Integer.valueOf(0), user1.getId());
142 assertEquals(Integer.valueOf(1), user2.getId());
143 sqlSession.commit();
144 }
145
146 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
147 List<User> users = sqlSession.selectList("selectIdentity");
148 Assertions.assertEquals(2, users.size());
149 }
150 }
151
152 @Test
153 void testInsertMapperNoBatchJdbc3() {
154 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
155 Mapper userMapper = sqlSession.getMapper(Mapper.class);
156 User user1 = new User(null, "Pocoyo");
157 userMapper.insertIdentity(user1);
158 assertEquals(Integer.valueOf(0), user1.getId());
159 sqlSession.commit();
160 }
161
162 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
163 List<User> users = sqlSession.selectList("selectIdentity");
164 Assertions.assertEquals(1, users.size());
165 }
166 }
167
168 }