|
| 1 | +/** |
| 2 | + * Copyright 2016-2019 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.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.insert.render.InsertStatementProvider; |
| 41 | +import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider; |
| 42 | +import org.mybatis.dynamic.sql.render.RenderingStrategies; |
| 43 | +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; |
| 44 | + |
| 45 | +import examples.generated.always.PersonRecord; |
| 46 | + |
| 47 | +public class PersonMapperTest { |
| 48 | + |
| 49 | + private static final String JDBC_URL = "jdbc:hsqldb:mem:aname"; |
| 50 | + private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver"; |
| 51 | + |
| 52 | + private SqlSessionFactory sqlSessionFactory; |
| 53 | + |
| 54 | + @BeforeEach |
| 55 | + public void setup() throws Exception { |
| 56 | + Class.forName(JDBC_DRIVER); |
| 57 | + InputStream is = getClass().getResourceAsStream("/examples/generated/always/CreateGeneratedAlwaysDB.sql"); |
| 58 | + try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) { |
| 59 | + ScriptRunner sr = new ScriptRunner(connection); |
| 60 | + sr.setLogWriter(null); |
| 61 | + sr.runScript(new InputStreamReader(is)); |
| 62 | + } |
| 63 | + |
| 64 | + UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", ""); |
| 65 | + Environment environment = new Environment("test", new JdbcTransactionFactory(), ds); |
| 66 | + Configuration config = new Configuration(environment); |
| 67 | + config.addMapper(PersonMapper.class); |
| 68 | + sqlSessionFactory = new SqlSessionFactoryBuilder().build(config); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testInsertSelectWithOneRecord() { |
| 73 | + try (SqlSession session = sqlSessionFactory.openSession()) { |
| 74 | + PersonMapper mapper = session.getMapper(PersonMapper.class); |
| 75 | + |
| 76 | + PersonRecord record = new PersonRecord(); |
| 77 | + record.setFirstName("Fred"); |
| 78 | + record.setLastName("Flintstone"); |
| 79 | + |
| 80 | + InsertStatementProvider<PersonRecord> insertStatement = SqlBuilder.insert(record) |
| 81 | + .into(person) |
| 82 | + .map(firstName).toProperty("firstName") |
| 83 | + .map(lastName).toProperty("lastName") |
| 84 | + .build() |
| 85 | + .render(RenderingStrategies.MYBATIS3); |
| 86 | + |
| 87 | + int rows = mapper.insert(insertStatement); |
| 88 | + |
| 89 | + assertThat(rows).isEqualTo(1); |
| 90 | + assertThat(record.getId()).isEqualTo(22); |
| 91 | + |
| 92 | + InsertSelectStatementProvider insertSelectStatement = SqlBuilder.insertInto(person) |
| 93 | + .withColumnList(firstName, lastName) |
| 94 | + .withSelectStatement(SqlBuilder.select(firstName, lastName).from(person)) |
| 95 | + .build() |
| 96 | + .render(RenderingStrategies.MYBATIS3); |
| 97 | + |
| 98 | + rows = mapper.insertSelect(insertSelectStatement); |
| 99 | + assertThat(rows).isEqualTo(1); |
| 100 | + assertThat(insertSelectStatement.getParameters().get("id")).isEqualTo(23); |
| 101 | + |
| 102 | + SelectStatementProvider selectStatement = SqlBuilder.select(id, firstName, lastName) |
| 103 | + .from(person) |
| 104 | + .orderBy(id) |
| 105 | + .build() |
| 106 | + .render(RenderingStrategies.MYBATIS3); |
| 107 | + |
| 108 | + List<PersonRecord> records = mapper.selectMany(selectStatement); |
| 109 | + assertThat(records.size()).isEqualTo(2); |
| 110 | + assertThat(records.get(0).getId()).isEqualTo(22); |
| 111 | + assertThat(records.get(1).getId()).isEqualTo(23); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testInsertSelectWithMultipleRecords() { |
| 117 | + try (SqlSession session = sqlSessionFactory.openSession()) { |
| 118 | + PersonMapper mapper = session.getMapper(PersonMapper.class); |
| 119 | + |
| 120 | + PersonRecord record1 = new PersonRecord(); |
| 121 | + record1.setFirstName("Fred"); |
| 122 | + record1.setLastName("Flintstone"); |
| 123 | + |
| 124 | + PersonRecord record2 = new PersonRecord(); |
| 125 | + record2.setFirstName("Barney"); |
| 126 | + record2.setLastName("Rubble"); |
| 127 | + |
| 128 | + MultiRowInsertStatementProvider<PersonRecord> insertStatement = SqlBuilder.insertMultiple(record1, record2) |
| 129 | + .into(person) |
| 130 | + .map(firstName).toProperty("firstName") |
| 131 | + .map(lastName).toProperty("lastName") |
| 132 | + .build() |
| 133 | + .render(RenderingStrategies.MYBATIS3); |
| 134 | + |
| 135 | + int rows = mapper.insertMultiple(insertStatement); |
| 136 | + |
| 137 | + assertThat(rows).isEqualTo(2); |
| 138 | + assertThat(record1.getId()).isEqualTo(22); |
| 139 | + assertThat(record2.getId()).isEqualTo(23); |
| 140 | + |
| 141 | + InsertSelectStatementProvider insertSelectStatement = SqlBuilder.insertInto(person) |
| 142 | + .withColumnList(firstName, lastName) |
| 143 | + .withSelectStatement(SqlBuilder.select(firstName, lastName).from(person)) |
| 144 | + .build() |
| 145 | + .render(RenderingStrategies.MYBATIS3); |
| 146 | + |
| 147 | + insertSelectStatement.getParameters().put("keys", new ArrayList<GeneratedKey>()); |
| 148 | + |
| 149 | + GeneratedKeyList keys = new GeneratedKeyList(5); |
| 150 | + |
| 151 | + rows = mapper.insertSelect(insertSelectStatement, keys); |
| 152 | + assertThat(rows).isEqualTo(2); |
| 153 | + System.out.println(keys); |
| 154 | + assertThat(keys.get(0).getKey()).isEqualTo(24); |
| 155 | + assertThat(keys.get(1).getKey()).isEqualTo(25); |
| 156 | + |
| 157 | + SelectStatementProvider selectStatement = SqlBuilder.select(id, firstName, lastName) |
| 158 | + .from(person) |
| 159 | + .orderBy(id) |
| 160 | + .build() |
| 161 | + .render(RenderingStrategies.MYBATIS3); |
| 162 | + |
| 163 | + List<PersonRecord> records = mapper.selectMany(selectStatement); |
| 164 | + assertThat(records.size()).isEqualTo(4); |
| 165 | + assertThat(records.get(0).getId()).isEqualTo(22); |
| 166 | + assertThat(records.get(1).getId()).isEqualTo(23); |
| 167 | + assertThat(records.get(2).getId()).isEqualTo(24); |
| 168 | + assertThat(records.get(3).getId()).isEqualTo(25); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments