Skip to content

Commit b670991

Browse files
committed
Add utility support for batch inserts
1 parent cfedb8c commit b670991

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

src/main/java/org/mybatis/dynamic/sql/insert/BatchInsertDSL.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
import org.mybatis.dynamic.sql.SqlColumn;
2424
import org.mybatis.dynamic.sql.SqlTable;
2525
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
26+
import org.mybatis.dynamic.sql.util.Buildable;
2627
import org.mybatis.dynamic.sql.util.ConstantMapping;
2728
import org.mybatis.dynamic.sql.util.NullMapping;
2829
import org.mybatis.dynamic.sql.util.PropertyMapping;
2930
import org.mybatis.dynamic.sql.util.StringConstantMapping;
3031

31-
public class BatchInsertDSL<T> {
32+
public class BatchInsertDSL<T> implements Buildable<BatchInsertModel<T>> {
3233

3334
private Collection<T> records;
3435
private SqlTable table;
@@ -43,6 +44,7 @@ public <F> ColumnMappingFinisher<F> map(SqlColumn<F> column) {
4344
return new ColumnMappingFinisher<>(column);
4445
}
4546

47+
@Override
4648
public BatchInsertModel<T> build() {
4749
return BatchInsertModel.withRecords(records)
4850
.withTable(table)

src/main/java/org/mybatis/dynamic/sql/insert/render/BatchInsert.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,6 +56,10 @@ public String getInsertStatementSQL() {
5656
return insertStatement;
5757
}
5858

59+
public List<T> getRecords() {
60+
return Collections.unmodifiableList(records);
61+
}
62+
5963
public static <T> Builder<T> withRecords(List<T> records) {
6064
return new Builder<T>().withRecords(records);
6165
}

src/main/java/org/mybatis/dynamic/sql/util/spring/NamedParameterJdbcTemplateExtensions.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121

2222
import org.mybatis.dynamic.sql.delete.DeleteModel;
2323
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
24+
import org.mybatis.dynamic.sql.insert.BatchInsertModel;
2425
import org.mybatis.dynamic.sql.insert.GeneralInsertModel;
2526
import org.mybatis.dynamic.sql.insert.InsertModel;
2627
import org.mybatis.dynamic.sql.insert.MultiRowInsertModel;
28+
import org.mybatis.dynamic.sql.insert.render.BatchInsert;
2729
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
2830
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
2931
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
@@ -37,8 +39,11 @@
3739
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
3840
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
3941
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
42+
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
43+
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
4044
import org.springframework.jdbc.support.KeyHolder;
4145

46+
4247
public class NamedParameterJdbcTemplateExtensions {
4348
private NamedParameterJdbcTemplate template;
4449

@@ -97,6 +102,15 @@ public <T> int insert(InsertStatementProvider<T> insertStatement, KeyHolder keyH
97102
new BeanPropertySqlParameterSource(insertStatement.getRecord()), keyHolder);
98103
}
99104

105+
public <T> int[] insertBatch(Buildable<BatchInsertModel<T>> insertStatement) {
106+
return insertBatch(SpringUtils.buildBatchInsert(insertStatement));
107+
}
108+
109+
public <T> int[] insertBatch(BatchInsert<T> insertStatement) {
110+
SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(insertStatement.getRecords());
111+
return template.batchUpdate(insertStatement.getInsertStatementSQL(), batch);
112+
}
113+
100114
public <T> int insertMultiple(Buildable<MultiRowInsertModel<T>> insertStatement) {
101115
return insertMultiple(SpringUtils.buildMultiRowInsert(insertStatement));
102116
}

src/main/java/org/mybatis/dynamic/sql/util/spring/SpringUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
import org.mybatis.dynamic.sql.delete.DeleteModel;
1919
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
20+
import org.mybatis.dynamic.sql.insert.BatchInsertModel;
2021
import org.mybatis.dynamic.sql.insert.GeneralInsertModel;
2122
import org.mybatis.dynamic.sql.insert.InsertModel;
2223
import org.mybatis.dynamic.sql.insert.MultiRowInsertModel;
24+
import org.mybatis.dynamic.sql.insert.render.BatchInsert;
2325
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
2426
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
2527
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
@@ -37,6 +39,10 @@ public static DeleteStatementProvider buildDelete(Buildable<DeleteModel> deleteS
3739
return deleteStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER);
3840
}
3941

42+
public static <T> BatchInsert<T> buildBatchInsert(Buildable<BatchInsertModel<T>> insertStatement) {
43+
return insertStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER);
44+
}
45+
4046
public static GeneralInsertStatementProvider buildGeneralInsert(Buildable<GeneralInsertModel> insertStatement) {
4147
return insertStatement.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER);
4248
}

src/test/java/examples/generated/always/spring/SpringTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.jupiter.api.BeforeEach;
2727
import org.junit.jupiter.api.Test;
2828
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
29+
import org.mybatis.dynamic.sql.insert.BatchInsertModel;
2930
import org.mybatis.dynamic.sql.insert.GeneralInsertModel;
3031
import org.mybatis.dynamic.sql.insert.InsertModel;
3132
import org.mybatis.dynamic.sql.insert.MultiRowInsertModel;
@@ -240,7 +241,7 @@ record = new GeneratedAlwaysRecord();
240241
record.setLastName("Smith");
241242
records.add(record);
242243

243-
SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(records.toArray());
244+
SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(records);
244245

245246
BatchInsert<GeneratedAlwaysRecord> batchInsert = insert(records)
246247
.into(generatedAlways)
@@ -257,6 +258,36 @@ record = new GeneratedAlwaysRecord();
257258
assertThat(updateCounts[1]).isEqualTo(1);
258259
}
259260

261+
@Test
262+
void testInsertBatchWithExtensions() {
263+
NamedParameterJdbcTemplateExtensions extensions = new NamedParameterJdbcTemplateExtensions(template);
264+
265+
List<GeneratedAlwaysRecord> records = new ArrayList<>();
266+
GeneratedAlwaysRecord record = new GeneratedAlwaysRecord();
267+
record.setId(100);
268+
record.setFirstName("Bob");
269+
record.setLastName("Jones");
270+
records.add(record);
271+
272+
record = new GeneratedAlwaysRecord();
273+
record.setId(101);
274+
record.setFirstName("Jim");
275+
record.setLastName("Smith");
276+
records.add(record);
277+
278+
Buildable<BatchInsertModel<GeneratedAlwaysRecord>> insertStatement = insert(records)
279+
.into(generatedAlways)
280+
.map(id).toProperty("id")
281+
.map(firstName).toProperty("firstName")
282+
.map(lastName).toProperty("lastName");
283+
284+
int[] updateCounts = extensions.insertBatch(insertStatement);
285+
286+
assertThat(updateCounts).hasSize(2);
287+
assertThat(updateCounts[0]).isEqualTo(1);
288+
assertThat(updateCounts[1]).isEqualTo(1);
289+
}
290+
260291
@Test
261292
void testMultiRowInsert() {
262293
NamedParameterJdbcTemplateExtensions extensions = new NamedParameterJdbcTemplateExtensions(template);

0 commit comments

Comments
 (0)