View Javadoc
1   /*
2    *    Copyright 2006-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.generator.runtime.dynamic.sql.elements;
17  
18  import java.util.HashSet;
19  import java.util.List;
20  import java.util.Set;
21  
22  import org.mybatis.generator.api.IntrospectedColumn;
23  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
24  import org.mybatis.generator.api.dom.java.Interface;
25  import org.mybatis.generator.api.dom.java.Method;
26  import org.mybatis.generator.api.dom.java.Parameter;
27  import org.mybatis.generator.codegen.mybatis3.ListUtilities;
28  
29  public class InsertMultipleMethodGenerator extends AbstractMethodGenerator {
30      private final FullyQualifiedJavaType recordType;
31  
32      private InsertMultipleMethodGenerator(Builder builder) {
33          super(builder);
34          recordType = builder.recordType;
35      }
36  
37      @Override
38      public MethodAndImports generateMethodAndImports() {
39          if (!Utils.generateMultipleRowInsert(introspectedTable)) {
40              return null;
41          }
42  
43          Set<FullyQualifiedJavaType> imports = new HashSet<>();
44  
45          imports.add(new FullyQualifiedJavaType("org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils")); //$NON-NLS-1$
46          imports.add(recordType);
47  
48          Method method = new Method("insertMultiple"); //$NON-NLS-1$
49          method.setDefault(true);
50          context.getCommentGenerator().addGeneralMethodAnnotation(method, introspectedTable, imports);
51          method.setReturnType(FullyQualifiedJavaType.getIntInstance());
52  
53          FullyQualifiedJavaType parameterType = new FullyQualifiedJavaType("java.util.Collection"); //$NON-NLS-1$
54          parameterType.addTypeArgument(recordType);
55          imports.add(parameterType);
56  
57          method.addParameter(new Parameter(parameterType, "records")); //$NON-NLS-1$
58  
59          String methodName;
60          if (Utils.canRetrieveMultiRowGeneratedKeys(introspectedTable)) {
61              methodName = "MyBatis3Utils.insertMultipleWithGeneratedKeys";
62          } else {
63              methodName = "MyBatis3Utils.insertMultiple";
64          }
65  
66          method.addBodyLine("return " + methodName + "(this::insertMultiple, records, " //$NON-NLS-1$ //$NON-NLS-2$
67                  + tableFieldName // $NON-NLS-1$
68                  + ", c ->"); //$NON-NLS-1$
69  
70          List<IntrospectedColumn> columns =
71                  ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
72          boolean first = true;
73          for (IntrospectedColumn column : columns) {
74              String fieldName = calculateFieldName(column);
75  
76              if (first) {
77                  method.addBodyLine("    c.map(" + fieldName //$NON-NLS-1$
78                          + ").toProperty(\"" + column.getJavaProperty() //$NON-NLS-1$
79                          + "\")"); //$NON-NLS-1$
80                  first = false;
81              } else {
82                  method.addBodyLine("    .map(" + fieldName //$NON-NLS-1$
83                          + ").toProperty(\"" + column.getJavaProperty() //$NON-NLS-1$
84                          + "\")"); //$NON-NLS-1$
85              }
86          }
87  
88          method.addBodyLine(");"); //$NON-NLS-1$
89  
90          return MethodAndImports.withMethod(method)
91                  .withImports(imports)
92                  .build();
93      }
94  
95      @Override
96      public boolean callPlugins(Method method, Interface interfaze) {
97          return context.getPlugins().clientInsertMultipleMethodGenerated(method, interfaze, introspectedTable);
98      }
99  
100     public static class Builder extends BaseBuilder<Builder> {
101         private FullyQualifiedJavaType recordType;
102 
103         public Builder withRecordType(FullyQualifiedJavaType recordType) {
104             this.recordType = recordType;
105             return this;
106         }
107 
108         @Override
109         public Builder getThis() {
110             return this;
111         }
112 
113         public InsertMultipleMethodGenerator build() {
114             return new InsertMultipleMethodGenerator(this);
115         }
116     }
117 }