1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.dynamic.sql.insert;
17
18 import static org.assertj.core.api.Assertions.assertThat;
19 import static org.mybatis.dynamic.sql.SqlBuilder.insertBatch;
20 import static org.mybatis.dynamic.sql.SqlBuilder.insertMultiple;
21
22 import java.sql.JDBCType;
23 import java.util.List;
24
25 import org.junit.jupiter.api.Test;
26 import org.mybatis.dynamic.sql.SqlColumn;
27 import org.mybatis.dynamic.sql.SqlTable;
28 import org.mybatis.dynamic.sql.insert.render.BatchInsert;
29 import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
30 import org.mybatis.dynamic.sql.render.RenderingStrategies;
31
32 class MapToRowTest {
33 private static final SqlTable foo = SqlTable.of("foo");
34 private static final SqlColumn<Integer> id1 = foo.column("id1", JDBCType.INTEGER);
35 private static final SqlColumn<Integer> id2 = foo.column("id2", JDBCType.INTEGER);
36
37 @Test
38 void testBasicInsertMultipleWithMyBatis() {
39 List<Record> records = List.of(
40 new Record(33, 1),
41 new Record(33, 2),
42 new Record(33, 3));
43
44 MultiRowInsertStatementProvider<Record> insertStatement = insertMultiple(records)
45 .into(foo)
46 .map(id1).toConstant("22")
47 .map(id2).toProperty("id2")
48 .build()
49 .render(RenderingStrategies.MYBATIS3);
50
51 String expected = "insert into foo (id1, id2) values (22, #{records[0].id2,jdbcType=INTEGER}), (22, #{records[1].id2,jdbcType=INTEGER}), (22, #{records[2].id2,jdbcType=INTEGER})";
52 assertThat(insertStatement.getInsertStatement()).isEqualTo(expected);
53 }
54
55 @Test
56 void testBasicInsertMultipleWithSpring() {
57 List<Record> records = List.of(
58 new Record(33, 1),
59 new Record(33, 2),
60 new Record(33, 3));
61
62 MultiRowInsertStatementProvider<Record> insertStatement = insertMultiple(records)
63 .into(foo)
64 .map(id1).toConstant("22")
65 .map(id2).toProperty("id2")
66 .build()
67 .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
68
69 String expected = "insert into foo (id1, id2) values (22, :records[0].id2), (22, :records[1].id2), (22, :records[2].id2)";
70 assertThat(insertStatement.getInsertStatement()).isEqualTo(expected);
71 }
72
73 @Test
74 void testBasicInsertMultipleRowMappingWithMyBatis() {
75 List<Integer> integers = List.of(1, 2, 3);
76
77 MultiRowInsertStatementProvider<Integer> insertStatement = insertMultiple(integers)
78 .into(foo)
79 .map(id1).toConstant("22")
80 .map(id2).toRow()
81 .build()
82 .render(RenderingStrategies.MYBATIS3);
83
84 String expected = "insert into foo (id1, id2) values (22, #{records[0],jdbcType=INTEGER}), (22, #{records[1],jdbcType=INTEGER}), (22, #{records[2],jdbcType=INTEGER})";
85 assertThat(insertStatement.getInsertStatement()).isEqualTo(expected);
86 }
87
88 @Test
89 void testBasicInsertMultipleRowMappingWithSpring() {
90 List<Integer> integers = List.of(1, 2, 3);
91
92 MultiRowInsertStatementProvider<Integer> insertStatement = insertMultiple(integers)
93 .into(foo)
94 .map(id1).toConstant("22")
95 .map(id2).toRow()
96 .build()
97 .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
98
99 String expected = "insert into foo (id1, id2) values (22, :records[0]), (22, :records[1]), (22, :records[2])";
100 assertThat(insertStatement.getInsertStatement()).isEqualTo(expected);
101 }
102
103 @Test
104 void testBatchInsertWithMyBatis() {
105 List<Record> records = List.of(
106 new Record(33, 1),
107 new Record(33, 2),
108 new Record(33, 3));
109
110 BatchInsert<Record> batchInsert = insertBatch(records)
111 .into(foo)
112 .map(id1).toConstant("22")
113 .map(id2).toProperty("id2")
114 .build()
115 .render(RenderingStrategies.MYBATIS3);
116
117 String expected = "insert into foo (id1, id2) values (22, #{row.id2,jdbcType=INTEGER})";
118 assertThat(batchInsert.getInsertStatementSQL()).isEqualTo(expected);
119 }
120
121 @Test
122 void testBatchInsertWithSpring() {
123 List<Record> records = List.of(
124 new Record(33, 1),
125 new Record(33, 2),
126 new Record(33, 3));
127
128 BatchInsert<Record> batchInsert = insertBatch(records)
129 .into(foo)
130 .map(id1).toConstant("22")
131 .map(id2).toProperty("id2")
132 .build()
133 .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
134
135 String expected = "insert into foo (id1, id2) values (22, :row.id2)";
136 assertThat(batchInsert.getInsertStatementSQL()).isEqualTo(expected);
137 }
138
139 @Test
140 void testBatchInsertRowMappingWithMyBatis() {
141 List<Integer> integers = List.of(1, 2, 3);
142
143 BatchInsert<Integer> batchInsert = insertBatch(integers)
144 .into(foo)
145 .map(id1).toConstant("22")
146 .map(id2).toRow()
147 .build()
148 .render(RenderingStrategies.MYBATIS3);
149
150 String expected = "insert into foo (id1, id2) values (22, #{row,jdbcType=INTEGER})";
151 assertThat(batchInsert.getInsertStatementSQL()).isEqualTo(expected);
152 }
153
154 @Test
155 void testBatchInsertRowMappingWithSpring() {
156 List<Integer> integers = List.of(1, 2, 3);
157
158 BatchInsert<Integer> batchInsert = insertBatch(integers)
159 .into(foo)
160 .map(id1).toConstant("22")
161 .map(id2).toRow()
162 .build()
163 .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
164
165 String expected = "insert into foo (id1, id2) values (22, :row)";
166 assertThat(batchInsert.getInsertStatementSQL()).isEqualTo(expected);
167 }
168
169 record Record(Integer id1, Integer id2) { }
170 }