View Javadoc
1   /*
2    *    Copyright 2016-2026 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.array;
17  
18  import static examples.array.NamesTableDynamicSqlSupport.*;
19  import static org.assertj.core.api.Assertions.assertThat;
20  import static org.mybatis.dynamic.sql.SqlBuilder.*;
21  
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import java.sql.Connection;
25  import java.sql.DriverManager;
26  import java.util.Optional;
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.insert.render.GeneralInsertStatementProvider;
39  import org.mybatis.dynamic.sql.render.RenderingStrategies;
40  import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
41  
42  class ArrayTest {
43      private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
44      private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
45  
46      private SqlSessionFactory sqlSessionFactory;
47  
48      @BeforeEach
49      void setup() throws Exception {
50          Class.forName(JDBC_DRIVER);
51          try (InputStream is = getClass().getResourceAsStream("/examples/array/CreateDB.sql")) {
52              assert is != null;
53              try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "");
54                  InputStreamReader isr = new InputStreamReader(is)) {
55                  ScriptRunner sr = new ScriptRunner(connection);
56                  sr.setLogWriter(null);
57                  sr.runScript(isr);
58              }
59          }
60  
61          UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
62          Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
63          Configuration config = new Configuration(environment);
64          config.addMapper(NamesTableMapper.class);
65          sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
66      }
67  
68      @Test
69      void testInsertSelectById() {
70          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
71              NamesTableMapper mapper = sqlSession.getMapper(NamesTableMapper.class);
72  
73              String[] someNames = {"Fred", "Wilma", "Pebbles"};
74  
75              GeneralInsertStatementProvider insertStatement = insertInto(namesTable)
76                      .set(id).toValue(1)
77                      .set(names).toValue(someNames)
78                      .build()
79                      .render(RenderingStrategies.MYBATIS3);
80              int rows = mapper.generalInsert(insertStatement);
81              assertThat(rows).isEqualTo(1);
82  
83              SelectStatementProvider selectStatement = select(id, NamesTableDynamicSqlSupport.names)
84                      .from(namesTable)
85                      .where(id, isEqualTo(1))
86                      .build()
87                      .render(RenderingStrategies.MYBATIS3);
88  
89              Optional<NamesRecord> row = mapper.selectOne(selectStatement);
90              assertThat(row).hasValueSatisfying( r -> {
91                  assertThat(r.getId()).isEqualTo(1);
92                  assertThat(r.getNames()).isEqualTo(someNames);
93              });
94          }
95      }
96  
97      @Test
98      void testInsertSelectByArray() {
99          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
100             NamesTableMapper mapper = sqlSession.getMapper(NamesTableMapper.class);
101 
102             String[] someNames = {"Fred", "Wilma", "Pebbles"};
103 
104             GeneralInsertStatementProvider insertStatement = insertInto(namesTable)
105                     .set(id).toValue(1)
106                     .set(names).toValue(someNames)
107                     .build()
108                     .render(RenderingStrategies.MYBATIS3);
109             int rows = mapper.generalInsert(insertStatement);
110             assertThat(rows).isEqualTo(1);
111 
112             SelectStatementProvider selectStatement = select(id, NamesTableDynamicSqlSupport.names)
113                     .from(namesTable)
114                     .where(names, isEqualTo(someNames))
115                     .build()
116                     .render(RenderingStrategies.MYBATIS3);
117 
118             Optional<NamesRecord> row = mapper.selectOne(selectStatement);
119             assertThat(row).hasValueSatisfying( r -> {
120                 assertThat(r.getId()).isEqualTo(1);
121                 assertThat(r.getNames()).isEqualTo(someNames);
122             });
123         }
124     }
125 }