11/*
2- * Copyright 2016-2020 the original author or authors.
2+ * Copyright 2016-2021 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.
2323import java .util .List ;
2424import java .util .Optional ;
2525
26- import org .apache .ibatis .annotations .Insert ;
2726import org .apache .ibatis .annotations .InsertProvider ;
2827import org .apache .ibatis .annotations .Options ;
2928import org .apache .ibatis .annotations .Param ;
3332import org .apache .ibatis .annotations .SelectProvider ;
3433import org .mybatis .dynamic .sql .BasicColumn ;
3534import org .mybatis .dynamic .sql .insert .render .InsertStatementProvider ;
36- import org .mybatis .dynamic .sql .insert .render .MultiRowInsertStatementProvider ;
3735import org .mybatis .dynamic .sql .select .SelectDSLCompleter ;
3836import org .mybatis .dynamic .sql .select .render .SelectStatementProvider ;
3937import org .mybatis .dynamic .sql .update .UpdateDSL ;
4240import org .mybatis .dynamic .sql .util .SqlProviderAdapter ;
4341
4442import examples .generated .always .GeneratedAlwaysRecord ;
45- import org .mybatis .dynamic .sql .util .mybatis3 .CommonInsertMapper ;
4643import org .mybatis .dynamic .sql .util .mybatis3 .CommonUpdateMapper ;
4744import org .mybatis .dynamic .sql .util .mybatis3 .MyBatis3Utils ;
4845
49- public interface GeneratedAlwaysMapper extends CommonInsertMapper < GeneratedAlwaysRecord >, CommonUpdateMapper {
46+ public interface GeneratedAlwaysMapper extends CommonUpdateMapper {
5047 @ SelectProvider (type =SqlProviderAdapter .class , method ="select" )
5148 @ Results (id ="gaResults" , value ={
5249 @ Result (property ="id" , column ="id" , id =true ),
@@ -60,23 +57,14 @@ public interface GeneratedAlwaysMapper extends CommonInsertMapper<GeneratedAlway
6057 @ ResultMap ("gaResults" )
6158 Optional <GeneratedAlwaysRecord > selectOne (SelectStatementProvider selectStatement );
6259
63- @ Override
6460 @ InsertProvider (type =SqlProviderAdapter .class , method ="insert" )
6561 @ Options (useGeneratedKeys =true , keyProperty ="record.fullName" )
6662 int insert (InsertStatementProvider <GeneratedAlwaysRecord > insertStatement );
6763
68- // This is kludgy. Currently MyBatis does not support nested lists in parameter objects
69- // when returning generated keys.
70- // So we need to do this silliness and decompose the multi row insert into its component parts
71- // for the actual MyBatis call
72- @ Insert ("${insertStatement}" )
64+ @ InsertProvider (type =SqlProviderAdapter .class , method ="insertMultipleWithGeneratedKeys" )
7365 @ Options (useGeneratedKeys =true , keyProperty ="records.fullName" )
7466 int insertMultiple (@ Param ("insertStatement" ) String statement , @ Param ("records" ) List <GeneratedAlwaysRecord > records );
7567
76- default int insertMultiple (MultiRowInsertStatementProvider <GeneratedAlwaysRecord > multiInsert ) {
77- return insertMultiple (multiInsert .getInsertStatement (), multiInsert .getRecords ());
78- }
79-
8068 BasicColumn [] selectList =
8169 BasicColumn .columnList (id , firstName , lastName , fullName );
8270
@@ -100,6 +88,18 @@ default int insert(GeneratedAlwaysRecord record) {
10088 );
10189 }
10290
91+ default int insertMultiple (GeneratedAlwaysRecord ...records ) {
92+ return insertMultiple (Arrays .asList (records ));
93+ }
94+
95+ default int insertMultiple (Collection <GeneratedAlwaysRecord > records ) {
96+ return MyBatis3Utils .insertMultipleWithGeneratedKeys (this ::insertMultiple , records , generatedAlways , c ->
97+ c .map (id ).toProperty ("id" )
98+ .map (firstName ).toProperty ("firstName" )
99+ .map (lastName ).toProperty ("lastName" )
100+ );
101+ }
102+
103103 default int insertSelective (GeneratedAlwaysRecord record ) {
104104 return MyBatis3Utils .insert (this ::insert , record , generatedAlways , c ->
105105 c .map (id ).toPropertyWhenPresent ("id" , record ::getId )
@@ -133,16 +133,4 @@ static UpdateDSL<UpdateModel> updateSelectiveColumns(GeneratedAlwaysRecord recor
133133 .set (firstName ).equalToWhenPresent (record ::getFirstName )
134134 .set (lastName ).equalToWhenPresent (record ::getLastName );
135135 }
136-
137- default int insertMultiple (GeneratedAlwaysRecord ...records ) {
138- return insertMultiple (Arrays .asList (records ));
139- }
140-
141- default int insertMultiple (Collection <GeneratedAlwaysRecord > records ) {
142- return MyBatis3Utils .insertMultiple (this ::insertMultiple , records , generatedAlways , c ->
143- c .map (id ).toProperty ("id" )
144- .map (firstName ).toProperty ("firstName" )
145- .map (lastName ).toProperty ("lastName" )
146- );
147- }
148136}
0 commit comments