1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.codegen.mybatis3.javamapper.elements.annotated;
17
18 import static org.mybatis.generator.api.dom.OutputUtilities.javaIndent;
19 import static org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities.getEscapedColumnName;
20 import static org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities.getParameterClause;
21 import static org.mybatis.generator.internal.util.StringUtility.escapeStringForJava;
22
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.mybatis.generator.api.IntrospectedColumn;
28 import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
29 import org.mybatis.generator.api.dom.java.Interface;
30 import org.mybatis.generator.api.dom.java.Method;
31 import org.mybatis.generator.codegen.mybatis3.ListUtilities;
32 import org.mybatis.generator.codegen.mybatis3.javamapper.elements.InsertMethodGenerator;
33
34 public class AnnotatedInsertMethodGenerator extends InsertMethodGenerator {
35
36 public AnnotatedInsertMethodGenerator(boolean isSimple) {
37 super(isSimple);
38 }
39
40 @Override
41 public void addMapperAnnotations(Method method) {
42
43 method.addAnnotation("@Insert({");
44 StringBuilder insertClause = new StringBuilder();
45 StringBuilder valuesClause = new StringBuilder();
46
47 javaIndent(insertClause, 1);
48 javaIndent(valuesClause, 1);
49
50 insertClause.append("\"insert into ");
51 insertClause.append(escapeStringForJava(introspectedTable
52 .getFullyQualifiedTableNameAtRuntime()));
53 insertClause.append(" (");
54
55 valuesClause.append("\"values (");
56
57 List<String> valuesClauses = new ArrayList<>();
58 Iterator<IntrospectedColumn> iter =
59 ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns())
60 .iterator();
61 boolean hasFields = false;
62 while (iter.hasNext()) {
63 IntrospectedColumn introspectedColumn = iter.next();
64
65 insertClause.append(escapeStringForJava(getEscapedColumnName(introspectedColumn)));
66 valuesClause.append(getParameterClause(introspectedColumn));
67 hasFields = true;
68 if (iter.hasNext()) {
69 insertClause.append(", ");
70 valuesClause.append(", ");
71 }
72
73 if (valuesClause.length() > 60) {
74 if (!iter.hasNext()) {
75 insertClause.append(')');
76 valuesClause.append(')');
77 }
78 insertClause.append("\",");
79 valuesClause.append('\"');
80 if (iter.hasNext()) {
81 valuesClause.append(',');
82 }
83
84 method.addAnnotation(insertClause.toString());
85 insertClause.setLength(0);
86 javaIndent(insertClause, 1);
87 insertClause.append('\"');
88
89 valuesClauses.add(valuesClause.toString());
90 valuesClause.setLength(0);
91 javaIndent(valuesClause, 1);
92 valuesClause.append('\"');
93 hasFields = false;
94 }
95 }
96
97 if (hasFields) {
98 insertClause.append(")\",");
99 method.addAnnotation(insertClause.toString());
100
101 valuesClause.append(")\"");
102 valuesClauses.add(valuesClause.toString());
103 }
104
105 for (String clause : valuesClauses) {
106 method.addAnnotation(clause);
107 }
108
109 method.addAnnotation("})");
110
111 buildGeneratedKeyAnnotation().ifPresent(method::addAnnotation);
112 }
113
114 @Override
115 public void addExtraImports(Interface interfaze) {
116 interfaze.addImportedTypes(buildGeneratedKeyImportsIfRequired());
117 interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Insert"));
118 }
119 }