View Javadoc
1   /*
2    *    Copyright 2009-2023 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    *       https://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.keycolumn;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertNotNull;
20  
21  import java.util.List;
22  
23  import org.apache.ibatis.BaseDataTest;
24  import org.apache.ibatis.executor.BatchResult;
25  import org.apache.ibatis.mapping.Environment;
26  import org.apache.ibatis.session.Configuration;
27  import org.apache.ibatis.session.ExecutorType;
28  import org.apache.ibatis.session.SqlSession;
29  import org.apache.ibatis.session.SqlSessionFactory;
30  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31  import org.apache.ibatis.testcontainers.PgContainer;
32  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
33  import org.junit.jupiter.api.BeforeAll;
34  import org.junit.jupiter.api.Tag;
35  import org.junit.jupiter.api.Test;
36  
37  /**
38   * @author Jeff Butler
39   */
40  @Tag("TestcontainersTests")
41  class InsertTest {
42  
43    private static SqlSessionFactory sqlSessionFactory;
44  
45    @BeforeAll
46    static void setUp() throws Exception {
47      Configuration configuration = new Configuration();
48      Environment environment = new Environment("development", new JdbcTransactionFactory(),
49          PgContainer.getUnpooledDataSource());
50      configuration.setEnvironment(environment);
51      configuration.addMapper(InsertMapper.class);
52      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
53  
54      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
55          "org/apache/ibatis/submitted/keycolumn/CreateDB.sql");
56    }
57  
58    @Test
59    void testInsertAnnotated() {
60      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
61        InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
62        Name name = new Name();
63        name.setFirstName("Fred");
64        name.setLastName("Flintstone");
65  
66        int rows = mapper.insertNameAnnotated(name);
67  
68        assertNotNull(name.getId());
69        assertEquals(1, rows);
70      }
71    }
72  
73    @Test
74    void testInsertMapped() {
75      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
76        InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
77        Name name = new Name();
78        name.setFirstName("Fred");
79        name.setLastName("Flintstone");
80  
81        int rows = mapper.insertNameMapped(name);
82  
83        assertNotNull(name.getId());
84        assertEquals(1, rows);
85      }
86    }
87  
88    @Test
89    void testInsertMappedBatch() {
90      try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
91        InsertMapper mapper = sqlSession.getMapper(InsertMapper.class);
92        Name name = new Name();
93        name.setFirstName("Fred");
94        name.setLastName("Flintstone");
95        mapper.insertNameMapped(name);
96        Name name2 = new Name();
97        name2.setFirstName("Wilma");
98        name2.setLastName("Flintstone");
99        mapper.insertNameMapped(name2);
100       List<BatchResult> batchResults = sqlSession.flushStatements();
101       assertNotNull(name.getId());
102       assertNotNull(name2.getId());
103       assertEquals(1, batchResults.size());
104     }
105   }
106 
107 }