Skip to content

Commit 0696425

Browse files
committed
Deprecate old insert method in favor of insertBatch
Now insert only means "single record insert". This is more consistent with all the helpers and other utilities in the library.
1 parent 1246416 commit 0696425

File tree

7 files changed

+141
-17
lines changed

7 files changed

+141
-17
lines changed

src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,84 @@ static DeleteDSL<DeleteModel> deleteFrom(SqlTable table) {
136136
static <T> InsertDSL.IntoGatherer<T> insert(T record) {
137137
return InsertDSL.insert(record);
138138
}
139-
139+
140+
/**
141+
* Insert a Batch of records. The model object is structured to support bulk inserts with JDBC batch support.
142+
*
143+
* @param records records to insert
144+
* @param <T> the type of record to insert
145+
* @return the next step in the DSL
146+
* @deprecated Use {@link SqlBuilder#insertBatch(Object[])} instead
147+
*/
148+
@Deprecated
140149
@SafeVarargs
141150
static <T> BatchInsertDSL.IntoGatherer<T> insert(T...records) {
142-
return BatchInsertDSL.insert(records);
151+
return insertBatch(records);
143152
}
144-
153+
154+
/**
155+
* Insert a Batch of records. The model object is structured to support bulk inserts with JDBC batch support.
156+
*
157+
* @param records records to insert
158+
* @param <T> the type of record to insert
159+
* @return the next step in the DSL
160+
* @deprecated Use {@link SqlBuilder#insertBatch(Collection)} instead
161+
*/
162+
@Deprecated
145163
static <T> BatchInsertDSL.IntoGatherer<T> insert(Collection<T> records) {
164+
return insertBatch(records);
165+
}
166+
167+
/**
168+
* Insert a Batch of records. The model object is structured to support bulk inserts with JDBC batch support.
169+
*
170+
* @param records records to insert
171+
* @param <T> the type of record to insert
172+
* @return the next step in the DSL
173+
*/
174+
@SafeVarargs
175+
static <T> BatchInsertDSL.IntoGatherer<T> insertBatch(T...records) {
146176
return BatchInsertDSL.insert(records);
147177
}
148-
178+
179+
/**
180+
* Insert a Batch of records. The model object is structured to support bulk inserts with JDBC batch support.
181+
*
182+
* @param records records to insert
183+
* @param <T> the type of record to insert
184+
* @return the next step in the DSL
185+
*/
186+
static <T> BatchInsertDSL.IntoGatherer<T> insertBatch(Collection<T> records) {
187+
return BatchInsertDSL.insert(records);
188+
}
189+
190+
/**
191+
* Insert multiple records in a single statement. The model object is structured as a single insert statement with
192+
* multiple values clauses. This statement is suitable for use with a small number of records. It is not suitable
193+
* for large bulk inserts as it is possible to exceed the limit of parameter markers in a prepared statement.
194+
*
195+
* <p>For large bulk inserts, see {@link SqlBuilder#insertBatch(Object[])}
196+
*
197+
* @param records records to insert
198+
* @param <T> the type of record to insert
199+
* @return the next step in the DSL
200+
*/
149201
@SafeVarargs
150202
static <T> MultiRowInsertDSL.IntoGatherer<T> insertMultiple(T...records) {
151203
return MultiRowInsertDSL.insert(records);
152204
}
153-
205+
206+
/**
207+
* Insert multiple records in a single statement. The model object is structured as a single insert statement with
208+
* multiple values clauses. This statement is suitable for use with a small number of records. It is not suitable
209+
* for large bulk inserts as it is possible to exceed the limit of parameter markers in a prepared statement.
210+
*
211+
* <p>For large bulk inserts, see {@link SqlBuilder#insertBatch(Collection)}
212+
*
213+
* @param records records to insert
214+
* @param <T> the type of record to insert
215+
* @return the next step in the DSL
216+
*/
154217
static <T> MultiRowInsertDSL.IntoGatherer<T> insertMultiple(Collection<T> records) {
155218
return MultiRowInsertDSL.insert(records);
156219
}

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/spring/NamedParameterJdbcTemplateExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class KeyHolderHelper(private val keyHolder: KeyHolder, private val template: Na
235235
@MyBatisDslMarker
236236
class BatchInsertHelper<T>(private val records: List<T>, private val template: NamedParameterJdbcTemplate) {
237237
fun into(table: SqlTable, completer: BatchInsertCompleter<T>) =
238-
template.insertBatch(SqlBuilder.insert(records).into(table, completer))
238+
template.insertBatch(SqlBuilder.insertBatch(records).into(table, completer))
239239
}
240240

241241
@MyBatisDslMarker

src/test/java/examples/animal/data/AnimalDataTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,7 @@ record = new AnimalData();
13831383
record.setBodyWeight(22.5);
13841384
records.add(record);
13851385

1386-
BatchInsert<AnimalData> batchInsert = insert(records)
1386+
BatchInsert<AnimalData> batchInsert = insertBatch(records)
13871387
.into(animalData)
13881388
.map(id).toProperty("id")
13891389
.map(animalName).toNull()
@@ -1433,7 +1433,7 @@ record = new AnimalData();
14331433
record.setBodyWeight(22.5);
14341434
records.add(record);
14351435

1436-
BatchInsert<AnimalData> batchInsert = insert(records)
1436+
BatchInsert<AnimalData> batchInsert = insertBatch(records)
14371437
.into(animalData)
14381438
.map(id).toProperty("id")
14391439
.map(animalName).toStringConstant("Old Fred")

src/test/java/examples/generated/always/mybatis/GeneratedAlwaysAnnotatedMapperTest.java

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void testInsert() {
121121
}
122122

123123
@Test
124-
void testBatchInsertWithList() {
124+
void testDeprecatedBatchInsertWithList() {
125125
try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
126126
GeneratedAlwaysAnnotatedMapper mapper = session.getMapper(GeneratedAlwaysAnnotatedMapper.class);
127127
List<GeneratedAlwaysRecord> records = getTestRecords();
@@ -134,7 +134,7 @@ void testBatchInsertWithList() {
134134
.build()
135135
.render(RenderingStrategies.MYBATIS3);
136136

137-
batchInsert.insertStatements().stream().forEach(mapper::insert);
137+
batchInsert.insertStatements().forEach(mapper::insert);
138138

139139
session.commit();
140140

@@ -148,7 +148,7 @@ void testBatchInsertWithList() {
148148
}
149149

150150
@Test
151-
void testBatchInsertWithArray() {
151+
void testDeprecatedBatchInsertWithArray() {
152152
try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
153153
GeneratedAlwaysAnnotatedMapper mapper = session.getMapper(GeneratedAlwaysAnnotatedMapper.class);
154154

@@ -170,7 +170,7 @@ void testBatchInsertWithArray() {
170170
.build()
171171
.render(RenderingStrategies.MYBATIS3);
172172

173-
batchInsert.insertStatements().stream().forEach(mapper::insert);
173+
batchInsert.insertStatements().forEach(mapper::insert);
174174

175175
session.commit();
176176

@@ -180,7 +180,68 @@ void testBatchInsertWithArray() {
180180
);
181181
}
182182
}
183-
183+
184+
@Test
185+
void testBatchInsertWithList() {
186+
try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
187+
GeneratedAlwaysAnnotatedMapper mapper = session.getMapper(GeneratedAlwaysAnnotatedMapper.class);
188+
List<GeneratedAlwaysRecord> records = getTestRecords();
189+
190+
BatchInsert<GeneratedAlwaysRecord> batchInsert = insertBatch(records)
191+
.into(generatedAlways)
192+
.map(id).toProperty("id")
193+
.map(firstName).toProperty("firstName")
194+
.map(lastName).toProperty("lastName")
195+
.build()
196+
.render(RenderingStrategies.MYBATIS3);
197+
198+
batchInsert.insertStatements().forEach(mapper::insert);
199+
200+
session.commit();
201+
202+
assertAll(
203+
() -> assertThat(records.get(0).getFullName()).isEqualTo("George Jetson"),
204+
() -> assertThat(records.get(1).getFullName()).isEqualTo("Jane Jetson"),
205+
() -> assertThat(records.get(2).getFullName()).isEqualTo("Judy Jetson"),
206+
() -> assertThat(records.get(3).getFullName()).isEqualTo("Elroy Jetson")
207+
);
208+
}
209+
}
210+
211+
@Test
212+
void testBatchInsertWithArray() {
213+
try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
214+
GeneratedAlwaysAnnotatedMapper mapper = session.getMapper(GeneratedAlwaysAnnotatedMapper.class);
215+
216+
GeneratedAlwaysRecord record1 = new GeneratedAlwaysRecord();
217+
record1.setId(1000);
218+
record1.setFirstName("George");
219+
record1.setLastName("Jetson");
220+
221+
GeneratedAlwaysRecord record2 = new GeneratedAlwaysRecord();
222+
record2.setId(1001);
223+
record2.setFirstName("Jane");
224+
record2.setLastName("Jetson");
225+
226+
BatchInsert<GeneratedAlwaysRecord> batchInsert = insertBatch(record1, record2)
227+
.into(generatedAlways)
228+
.map(id).toProperty("id")
229+
.map(firstName).toProperty("firstName")
230+
.map(lastName).toProperty("lastName")
231+
.build()
232+
.render(RenderingStrategies.MYBATIS3);
233+
234+
batchInsert.insertStatements().forEach(mapper::insert);
235+
236+
session.commit();
237+
238+
assertAll(
239+
() -> assertThat(record1.getFullName()).isEqualTo("George Jetson"),
240+
() -> assertThat(record2.getFullName()).isEqualTo("Jane Jetson")
241+
);
242+
}
243+
}
244+
184245
@Test
185246
void testMultiInsertWithRawMyBatisAnnotations() {
186247
try (SqlSession session = sqlSessionFactory.openSession()) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ record = new GeneratedAlwaysRecord();
243243

244244
SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(records);
245245

246-
BatchInsert<GeneratedAlwaysRecord> batchInsert = insert(records)
246+
BatchInsert<GeneratedAlwaysRecord> batchInsert = insertBatch(records)
247247
.into(generatedAlways)
248248
.map(id).toProperty("id")
249249
.map(firstName).toProperty("firstName")
@@ -275,7 +275,7 @@ record = new GeneratedAlwaysRecord();
275275
record.setLastName("Smith");
276276
records.add(record);
277277

278-
Buildable<BatchInsertModel<GeneratedAlwaysRecord>> insertStatement = insert(records)
278+
Buildable<BatchInsertModel<GeneratedAlwaysRecord>> insertStatement = insertBatch(records)
279279
.into(generatedAlways)
280280
.map(id).toProperty("id")
281281
.map(firstName).toProperty("firstName")

src/test/java/examples/spring/PersonTemplateTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ record = new PersonRecord();
347347
record.setAddressId(2);
348348
records.add(record);
349349

350-
Buildable<BatchInsertModel<PersonRecord>> insertStatement = insert(records).into(person)
350+
Buildable<BatchInsertModel<PersonRecord>> insertStatement = insertBatch(records).into(person)
351351
.map(id).toProperty("id")
352352
.map(firstName).toProperty("firstName")
353353
.map(lastName).toProperty("lastNameAsString")

src/test/kotlin/examples/kotlin/spring/canonical/CanonicalSpringKotlinTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ class CanonicalSpringKotlinTest {
319319
val record1 = PersonRecord(100, "Joe", LastName("Jones"), Date(), true, "Developer", 1)
320320
val record2 = PersonRecord(101, "Sarah", LastName("Smith"), Date(), true, "Architect", 2)
321321

322-
val insertStatement = insert(record1, record2).into(Person) {
322+
val insertStatement = insertBatch(record1, record2).into(Person) {
323323
map(id).toProperty("id")
324324
map(firstName).toProperty("firstName")
325325
map(lastName).toProperty("lastNameAsString")

0 commit comments

Comments
 (0)