1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }