|
| 1 | +/* |
| 2 | + * Copyright 2016-2023 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 | + */ |
1 | 16 | package examples.spring; |
2 | 17 |
|
3 | 18 | import static examples.spring.CompoundKeyDynamicSqlSupport.compoundKey; |
4 | 19 | import static examples.spring.CompoundKeyDynamicSqlSupport.id1; |
5 | 20 | import static examples.spring.CompoundKeyDynamicSqlSupport.id2; |
6 | 21 | import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.mybatis.dynamic.sql.SqlBuilder.insert; |
7 | 23 | import static org.mybatis.dynamic.sql.SqlBuilder.insertBatch; |
8 | 24 | import static org.mybatis.dynamic.sql.SqlBuilder.insertMultiple; |
9 | 25 | import static org.mybatis.dynamic.sql.SqlBuilder.select; |
10 | 26 |
|
11 | | -import java.sql.PreparedStatement; |
12 | | -import java.sql.SQLException; |
13 | 27 | import java.util.ArrayList; |
14 | 28 | import java.util.List; |
15 | 29 | import java.util.stream.IntStream; |
16 | 30 |
|
17 | | -import org.jetbrains.annotations.NotNull; |
18 | 31 | import org.junit.jupiter.api.Test; |
19 | 32 | import org.mybatis.dynamic.sql.insert.render.BatchInsert; |
| 33 | +import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider; |
20 | 34 | import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider; |
21 | 35 | import org.mybatis.dynamic.sql.render.RenderingStrategies; |
22 | 36 | import org.mybatis.dynamic.sql.select.SelectModel; |
23 | 37 | import org.mybatis.dynamic.sql.util.Buildable; |
24 | 38 | import org.mybatis.dynamic.sql.util.spring.NamedParameterJdbcTemplateExtensions; |
25 | 39 | import org.springframework.beans.factory.annotation.Autowired; |
26 | | -import org.springframework.jdbc.core.BatchPreparedStatementSetter; |
27 | 40 | import org.springframework.jdbc.core.RowMapper; |
28 | 41 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
29 | 42 | import org.springframework.transaction.annotation.Transactional; |
30 | 43 |
|
31 | 44 | @SpringJUnitConfig(classes = SpringConfiguration.class) |
32 | 45 | @Transactional |
33 | | -public class SpringMapToRowTest { |
| 46 | +class SpringMapToRowTest { |
34 | 47 | @Autowired |
35 | 48 | private NamedParameterJdbcTemplateExtensions template; |
36 | 49 |
|
| 50 | + @Test |
| 51 | + void testInsertOne() { |
| 52 | + Integer i = 1; |
| 53 | + |
| 54 | + InsertStatementProvider<Integer> insertStatement = insert(i) |
| 55 | + .into(compoundKey) |
| 56 | + .map(id1).toConstant("22") |
| 57 | + .map(id2).toRow() |
| 58 | + .build() |
| 59 | + .render(RenderingStrategies.SPRING_NAMED_PARAMETER); |
| 60 | + |
| 61 | + String expected = "insert into CompoundKey (id1, id2) values (22, :row)"; |
| 62 | + assertThat(insertStatement.getInsertStatement()).isEqualTo(expected); |
| 63 | + |
| 64 | + int rows = template.insert(insertStatement); |
| 65 | + assertThat(rows).isEqualTo(1); |
| 66 | + |
| 67 | + Buildable<SelectModel> selectStatement = select(id1, id2) |
| 68 | + .from(compoundKey) |
| 69 | + .orderBy(id1, id2); |
| 70 | + |
| 71 | + List<CompoundKeyRow> records = template.selectList(selectStatement, rowMapper); |
| 72 | + assertThat(records).hasSize(1); |
| 73 | + } |
| 74 | + |
37 | 75 | @Test |
38 | 76 | void testInsertMultiple() { |
39 | 77 | List<Integer> integers = new ArrayList<>(); |
@@ -79,19 +117,6 @@ void testInsertBatch() { |
79 | 117 | String expected = "insert into CompoundKey (id1, id2) values (22, :row)"; |
80 | 118 | assertThat(insertStatement.getInsertStatementSQL()).isEqualTo(expected); |
81 | 119 |
|
82 | | -// String sql = insertStatement.getInsertStatementSQL().replace(":row", "?"); |
83 | | -// int[] rowCounts = template.getJdbcOperations().batchUpdate(sql, new BatchPreparedStatementSetter() { |
84 | | -// @Override |
85 | | -// public void setValues(@NotNull PreparedStatement ps, int i) throws SQLException { |
86 | | -// ps.setInt(1, insertStatement.getRecords().get(i)); |
87 | | -// } |
88 | | -// |
89 | | -// @Override |
90 | | -// public int getBatchSize() { |
91 | | -// return insertStatement.getRecords().size(); |
92 | | -// } |
93 | | -// }); |
94 | | - |
95 | 120 | int[] rowCounts = template.insertBatch(insertStatement); |
96 | 121 |
|
97 | 122 | assertThat(IntStream.of(rowCounts).sum()).isEqualTo(3); |
|
0 commit comments