|
| 1 | +/* |
| 2 | + * Copyright 2016-2020 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 | + * http://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 org.apache.ibatis.datasource.unpooled.UnpooledDataSource; |
| 23 | +import org.apache.ibatis.jdbc.ScriptRunner; |
| 24 | +import org.apache.ibatis.mapping.Environment; |
| 25 | +import org.apache.ibatis.session.Configuration; |
| 26 | +import org.apache.ibatis.session.SqlSession; |
| 27 | +import org.apache.ibatis.session.SqlSessionFactory; |
| 28 | +import org.apache.ibatis.session.SqlSessionFactoryBuilder; |
| 29 | +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; |
| 30 | +import org.junit.jupiter.api.BeforeEach; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider; |
| 33 | +import org.mybatis.dynamic.sql.render.RenderingStrategies; |
| 34 | +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; |
| 35 | + |
| 36 | +import java.io.InputStream; |
| 37 | +import java.io.InputStreamReader; |
| 38 | +import java.sql.Connection; |
| 39 | +import java.sql.DriverManager; |
| 40 | +import java.util.Optional; |
| 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 | + try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) { |
| 53 | + ScriptRunner sr = new ScriptRunner(connection); |
| 54 | + sr.setLogWriter(null); |
| 55 | + sr.runScript(new InputStreamReader(is)); |
| 56 | + } |
| 57 | + |
| 58 | + UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", ""); |
| 59 | + Environment environment = new Environment("test", new JdbcTransactionFactory(), ds); |
| 60 | + Configuration config = new Configuration(environment); |
| 61 | + config.addMapper(NamesTableMapper.class); |
| 62 | + sqlSessionFactory = new SqlSessionFactoryBuilder().build(config); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testInsertSelectById() { |
| 67 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 68 | + NamesTableMapper mapper = sqlSession.getMapper(NamesTableMapper.class); |
| 69 | + |
| 70 | + String[] someNames = {"Fred", "Wilma", "Pebbles"}; |
| 71 | + |
| 72 | + GeneralInsertStatementProvider insertStatement = insertInto(namesTable) |
| 73 | + .set(id).toValue(1) |
| 74 | + .set(names).toValue(someNames) |
| 75 | + .build() |
| 76 | + .render(RenderingStrategies.MYBATIS3); |
| 77 | + int rows = mapper.generalInsert(insertStatement); |
| 78 | + assertThat(rows).isEqualTo(1); |
| 79 | + |
| 80 | + SelectStatementProvider selectStatement = select(id, NamesTableDynamicSqlSupport.names) |
| 81 | + .from(namesTable) |
| 82 | + .where(id, isEqualTo(1)) |
| 83 | + .build() |
| 84 | + .render(RenderingStrategies.MYBATIS3); |
| 85 | + |
| 86 | + Optional<NamesRecord> record = mapper.selectOne(selectStatement); |
| 87 | + assertThat(record).hasValueSatisfying( r -> { |
| 88 | + assertThat(r.getId()).isEqualTo(1); |
| 89 | + assertThat(r.getNames()).isEqualTo(someNames); |
| 90 | + }); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void testInsertSelectByArray() { |
| 96 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 97 | + NamesTableMapper mapper = sqlSession.getMapper(NamesTableMapper.class); |
| 98 | + |
| 99 | + String[] someNames = {"Fred", "Wilma", "Pebbles"}; |
| 100 | + |
| 101 | + GeneralInsertStatementProvider insertStatement = insertInto(namesTable) |
| 102 | + .set(id).toValue(1) |
| 103 | + .set(names).toValue(someNames) |
| 104 | + .build() |
| 105 | + .render(RenderingStrategies.MYBATIS3); |
| 106 | + int rows = mapper.generalInsert(insertStatement); |
| 107 | + assertThat(rows).isEqualTo(1); |
| 108 | + |
| 109 | + SelectStatementProvider selectStatement = select(id, NamesTableDynamicSqlSupport.names) |
| 110 | + .from(namesTable) |
| 111 | + .where(names, isEqualTo(someNames)) |
| 112 | + .build() |
| 113 | + .render(RenderingStrategies.MYBATIS3); |
| 114 | + |
| 115 | + Optional<NamesRecord> record = mapper.selectOne(selectStatement); |
| 116 | + assertThat(record).hasValueSatisfying( r -> { |
| 117 | + assertThat(r.getId()).isEqualTo(1); |
| 118 | + assertThat(r.getNames()).isEqualTo(someNames); |
| 119 | + }); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments