View Javadoc
1   /*
2    *    Copyright 2009-2021 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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')", Statement.RETURN_GENERATED_KEYS)) {
55        stmt.addBatch();
56        stmt.executeBatch();
57        try (ResultSet rs = stmt.getGeneratedKeys()) {
58          if (rs.next()) {
59            ResultSetMetaData rsmd = rs.getMetaData();
60            int colCount = rsmd.getColumnCount();
61            do {
62              for (int i = 1; i <= colCount; i++) {
63                String key = rs.getString(i);
64                System.out.println("key " + i + " is " + key);
65              }
66            } while (rs.next());
67          } else {
68            System.out.println("There are no generated keys.");
69          }
70        }
71      }
72    }
73  
74    @Test
75    void testInsert() {
76      try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
77        User user1 = new User(null, "Pocoyo");
78        sqlSession.insert("insert", user1);
79        User user2 = new User(null, "Valentina");
80        sqlSession.insert("insert", user2);
81        sqlSession.flushStatements();
82        assertEquals(Integer.valueOf(50), user1.getId());
83        assertEquals(Integer.valueOf(50), user2.getId());
84        sqlSession.commit();
85      }
86      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
87        List<User> users = sqlSession.selectList("select");
88        Assertions.assertEquals( 2, users.size());
89      }
90    }
91  
92    @Test
93    void testInsertJdbc3() {
94      try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
95        User user1 = new User(null, "Pocoyo");
96        sqlSession.insert("insertIdentity", user1);
97        User user2 = new User(null, "Valentina");
98        sqlSession.insert("insertIdentity", user2);
99        sqlSession.flushStatements();
100       assertEquals(Integer.valueOf(0), user1.getId());
101       assertEquals(Integer.valueOf(1), user2.getId());
102       sqlSession.commit();
103     }
104 
105     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
106       List<User> users = sqlSession.selectList("selectIdentity");
107       Assertions.assertEquals(2, users.size());
108     }
109   }
110 
111   @Test
112   void testInsertWithMapper() {
113     try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
114       Mapper userMapper = sqlSession.getMapper(Mapper.class);
115       User user1 = new User(null, "Pocoyo");
116       userMapper.insert(user1);
117       User user2 = new User(null, "Valentina");
118       userMapper.insert(user2);
119       sqlSession.flushStatements();
120       assertEquals(Integer.valueOf(50), user1.getId());
121       assertEquals(Integer.valueOf(50), user2.getId());
122       sqlSession.commit();
123     }
124 
125     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
126       List<User> users = sqlSession.selectList("select");
127       Assertions.assertEquals(2, users.size());
128     }
129   }
130 
131   @Test
132   void testInsertMapperJdbc3() {
133     try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
134       Mapper userMapper = sqlSession.getMapper(Mapper.class);
135       User user1 = new User(null, "Pocoyo");
136       userMapper.insertIdentity(user1);
137       User user2 = new User(null, "Valentina");
138       userMapper.insertIdentity(user2);
139       sqlSession.flushStatements();
140       assertEquals(Integer.valueOf(0), user1.getId());
141       assertEquals(Integer.valueOf(1), user2.getId());
142       sqlSession.commit();
143     }
144 
145     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
146       List<User> users = sqlSession.selectList("selectIdentity");
147       Assertions.assertEquals(2, users.size());
148     }
149   }
150 
151   @Test
152   void testInsertMapperNoBatchJdbc3() {
153     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
154       Mapper userMapper = sqlSession.getMapper(Mapper.class);
155       User user1 = new User(null, "Pocoyo");
156       userMapper.insertIdentity(user1);
157       assertEquals(Integer.valueOf(0), user1.getId());
158       sqlSession.commit();
159     }
160 
161     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
162       List<User> users = sqlSession.selectList("selectIdentity");
163       Assertions.assertEquals(1, users.size());
164     }
165   }
166 
167 }