1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package examples.spring;
17
18 import static examples.spring.CompoundKeyDynamicSqlSupport.compoundKey;
19 import static examples.spring.CompoundKeyDynamicSqlSupport.id1;
20 import static examples.spring.CompoundKeyDynamicSqlSupport.id2;
21 import static org.assertj.core.api.Assertions.assertThat;
22 import static org.mybatis.dynamic.sql.SqlBuilder.insert;
23 import static org.mybatis.dynamic.sql.SqlBuilder.insertBatch;
24 import static org.mybatis.dynamic.sql.SqlBuilder.insertMultiple;
25 import static org.mybatis.dynamic.sql.SqlBuilder.select;
26
27 import java.util.List;
28 import java.util.stream.IntStream;
29
30 import org.junit.jupiter.api.Test;
31 import org.mybatis.dynamic.sql.insert.render.BatchInsert;
32 import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
33 import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
34 import org.mybatis.dynamic.sql.render.RenderingStrategies;
35 import org.mybatis.dynamic.sql.select.SelectModel;
36 import org.mybatis.dynamic.sql.util.Buildable;
37 import org.mybatis.dynamic.sql.util.spring.NamedParameterJdbcTemplateExtensions;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.jdbc.core.RowMapper;
40 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
41 import org.springframework.transaction.annotation.Transactional;
42
43 @SpringJUnitConfig(classes = SpringConfiguration.class)
44 @Transactional
45 class SpringMapToRowTest {
46 @Autowired
47 private NamedParameterJdbcTemplateExtensions template;
48
49 @Test
50 void testInsertOne() {
51 Integer i = 1;
52
53 InsertStatementProvider<Integer> insertStatement = insert(i)
54 .into(compoundKey)
55 .map(id1).toConstant("22")
56 .map(id2).toRow()
57 .build()
58 .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
59
60 String expected = "insert into CompoundKey (id1, id2) values (22, :row)";
61 assertThat(insertStatement.getInsertStatement()).isEqualTo(expected);
62
63 int rows = template.insert(insertStatement);
64 assertThat(rows).isEqualTo(1);
65
66 Buildable<SelectModel> selectStatement = select(id1, id2)
67 .from(compoundKey)
68 .orderBy(id1, id2);
69
70 List<CompoundKeyRow> records = template.selectList(selectStatement, rowMapper);
71 assertThat(records).hasSize(1);
72 }
73
74 @Test
75 void testInsertMultiple() {
76 List<Integer> integers = List.of(1, 2, 3);
77
78 MultiRowInsertStatementProvider<Integer> insertStatement = insertMultiple(integers)
79 .into(compoundKey)
80 .map(id1).toConstant("22")
81 .map(id2).toRow()
82 .build()
83 .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
84
85 String expected = "insert into CompoundKey (id1, id2) values (22, :records[0]), (22, :records[1]), (22, :records[2])";
86 assertThat(insertStatement.getInsertStatement()).isEqualTo(expected);
87
88 int rows = template.insertMultiple(insertStatement);
89 assertThat(rows).isEqualTo(3);
90
91 Buildable<SelectModel> selectStatement = select(id1, id2)
92 .from(compoundKey)
93 .orderBy(id1, id2);
94
95 List<CompoundKeyRow> records = template.selectList(selectStatement, rowMapper);
96 assertThat(records).hasSize(3);
97 }
98
99 @Test
100 void testInsertBatch() {
101 List<Integer> integers = List.of(1, 2, 3);
102
103 BatchInsert<Integer> insertStatement = insertBatch(integers)
104 .into(compoundKey)
105 .map(id1).toConstant("22")
106 .map(id2).toRow()
107 .build()
108 .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
109
110 String expected = "insert into CompoundKey (id1, id2) values (22, :row)";
111 assertThat(insertStatement.getInsertStatementSQL()).isEqualTo(expected);
112
113 int[] rowCounts = template.insertBatch(insertStatement);
114
115 assertThat(IntStream.of(rowCounts).sum()).isEqualTo(3);
116
117 Buildable<SelectModel> selectStatement = select(id1, id2)
118 .from(compoundKey)
119 .orderBy(id1, id2);
120
121 List<CompoundKeyRow> records = template.selectList(selectStatement, rowMapper);
122 assertThat(records).hasSize(3);
123 }
124
125 static final RowMapper<CompoundKeyRow> rowMapper =
126 (rs, i) -> new CompoundKeyRow(rs.getInt("ID1"), rs.getInt("ID2"));
127 }