View Javadoc
1   /*
2    *    Copyright 2016-2025 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 examples.generated.always.mybatis;
17  
18  import static examples.generated.always.mybatis.PersonDynamicSqlSupport.*;
19  import static org.assertj.core.api.Assertions.assertThat;
20  
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  import java.sql.Connection;
24  import java.sql.DriverManager;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
29  import org.apache.ibatis.jdbc.ScriptRunner;
30  import org.apache.ibatis.mapping.Environment;
31  import org.apache.ibatis.session.Configuration;
32  import org.apache.ibatis.session.SqlSession;
33  import org.apache.ibatis.session.SqlSessionFactory;
34  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
35  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
36  import org.junit.jupiter.api.BeforeEach;
37  import org.junit.jupiter.api.Test;
38  import org.mybatis.dynamic.sql.SqlBuilder;
39  import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider;
40  import org.mybatis.dynamic.sql.render.RenderingStrategies;
41  
42  import examples.generated.always.PersonRecord;
43  
44  class PersonMapperTest {
45  
46      private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
47      private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
48  
49      private SqlSessionFactory sqlSessionFactory;
50  
51      @BeforeEach
52      void setup() throws Exception {
53          Class.forName(JDBC_DRIVER);
54          InputStream is = getClass().getResourceAsStream("/examples/generated/always/CreateGeneratedAlwaysDB.sql");
55          assert is != null;
56          try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
57              ScriptRunner sr = new ScriptRunner(connection);
58              sr.setLogWriter(null);
59              sr.runScript(new InputStreamReader(is));
60          }
61  
62          UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
63          Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
64          Configuration config = new Configuration(environment);
65          config.addMapper(PersonMapper.class);
66          sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
67      }
68  
69      @Test
70      void testInsertSelectWithOneRecord() {
71          try (SqlSession session = sqlSessionFactory.openSession()) {
72              PersonMapper mapper = session.getMapper(PersonMapper.class);
73  
74              PersonRecord row = new PersonRecord();
75              row.setFirstName("Fred");
76              row.setLastName("Flintstone");
77  
78              int rows = mapper.insert(row);
79  
80              assertThat(rows).isEqualTo(1);
81              assertThat(row.getId()).isEqualTo(22);
82  
83              InsertSelectStatementProvider insertSelectStatement = SqlBuilder.insertInto(person)
84                      .withColumnList(firstName, lastName)
85                      .withSelectStatement(SqlBuilder.select(firstName, lastName).from(person))
86                      .build()
87                      .render(RenderingStrategies.MYBATIS3);
88  
89              rows = mapper.insertSelect(insertSelectStatement);
90              assertThat(rows).isEqualTo(1);
91              assertThat(insertSelectStatement.getParameters()).containsEntry("id", 23);
92  
93              List<PersonRecord> records = mapper.select(c -> c.orderBy(id));
94              assertThat(records).hasSize(2);
95              assertThat(records.get(0).getId()).isEqualTo(22);
96              assertThat(records.get(1).getId()).isEqualTo(23);
97          }
98      }
99  
100     @Test
101     void testInsertSelectWithMultipleRecords() {
102         try (SqlSession session = sqlSessionFactory.openSession()) {
103             PersonMapper mapper = session.getMapper(PersonMapper.class);
104 
105             PersonRecord record1 = new PersonRecord();
106             record1.setFirstName("Fred");
107             record1.setLastName("Flintstone");
108 
109             PersonRecord record2 = new PersonRecord();
110             record2.setFirstName("Barney");
111             record2.setLastName("Rubble");
112 
113             int rows = mapper.insertMultiple(record1, record2);
114 
115             assertThat(rows).isEqualTo(2);
116             assertThat(record1.getId()).isEqualTo(22);
117             assertThat(record2.getId()).isEqualTo(23);
118 
119             InsertSelectStatementProvider insertSelectStatement = SqlBuilder.insertInto(person)
120                     .withColumnList(firstName, lastName)
121                     .withSelectStatement(SqlBuilder.select(firstName, lastName).from(person))
122                     .build()
123                     .render(RenderingStrategies.MYBATIS3);
124 
125             insertSelectStatement.getParameters().put("keys", new ArrayList<GeneratedKey>());
126 
127             GeneratedKeyList keys = new GeneratedKeyList(5);
128 
129             rows = mapper.insertSelect(insertSelectStatement, keys);
130             assertThat(rows).isEqualTo(2);
131             assertThat(keys.get(0).getKey()).isEqualTo(24);
132             assertThat(keys.get(1).getKey()).isEqualTo(25);
133 
134             List<PersonRecord> records = mapper.select(c -> c.orderBy(id));
135             assertThat(records).hasSize(4);
136             assertThat(records.get(0).getId()).isEqualTo(22);
137             assertThat(records.get(1).getId()).isEqualTo(23);
138             assertThat(records.get(2).getId()).isEqualTo(24);
139             assertThat(records.get(3).getId()).isEqualTo(25);
140         }
141     }
142 }