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.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          InputStream is = getClass().getResourceAsStream("/examples/array/CreateDB.sql");
52          assert is != null;
53          try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
54              ScriptRunner sr = new ScriptRunner(connection);
55              sr.setLogWriter(null);
56              sr.runScript(new InputStreamReader(is));
57          }
58  
59          UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
60          Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
61          Configuration config = new Configuration(environment);
62          config.addMapper(NamesTableMapper.class);
63          sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
64      }
65  
66      @Test
67      void testInsertSelectById() {
68          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
69              NamesTableMapper mapper = sqlSession.getMapper(NamesTableMapper.class);
70  
71              String[] someNames = {"Fred", "Wilma", "Pebbles"};
72  
73              GeneralInsertStatementProvider insertStatement = insertInto(namesTable)
74                      .set(id).toValue(1)
75                      .set(names).toValue(someNames)
76                      .build()
77                      .render(RenderingStrategies.MYBATIS3);
78              int rows = mapper.generalInsert(insertStatement);
79              assertThat(rows).isEqualTo(1);
80  
81              SelectStatementProvider selectStatement = select(id, NamesTableDynamicSqlSupport.names)
82                      .from(namesTable)
83                      .where(id, isEqualTo(1))
84                      .build()
85                      .render(RenderingStrategies.MYBATIS3);
86  
87              Optional<NamesRecord> row = mapper.selectOne(selectStatement);
88              assertThat(row).hasValueSatisfying( r -> {
89                  assertThat(r.getId()).isEqualTo(1);
90                  assertThat(r.getNames()).isEqualTo(someNames);
91              });
92          }
93      }
94  
95      @Test
96      void testInsertSelectByArray() {
97          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
98              NamesTableMapper mapper = sqlSession.getMapper(NamesTableMapper.class);
99  
100             String[] someNames = {"Fred", "Wilma", "Pebbles"};
101 
102             GeneralInsertStatementProvider insertStatement = insertInto(namesTable)
103                     .set(id).toValue(1)
104                     .set(names).toValue(someNames)
105                     .build()
106                     .render(RenderingStrategies.MYBATIS3);
107             int rows = mapper.generalInsert(insertStatement);
108             assertThat(rows).isEqualTo(1);
109 
110             SelectStatementProvider selectStatement = select(id, NamesTableDynamicSqlSupport.names)
111                     .from(namesTable)
112                     .where(names, isEqualTo(someNames))
113                     .build()
114                     .render(RenderingStrategies.MYBATIS3);
115 
116             Optional<NamesRecord> row = mapper.selectOne(selectStatement);
117             assertThat(row).hasValueSatisfying( r -> {
118                 assertThat(r.getId()).isEqualTo(1);
119                 assertThat(r.getNames()).isEqualTo(someNames);
120             });
121         }
122     }
123 }