1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.codegen.ibatis2.dao.elements;
17
18 import java.util.Set;
19 import java.util.TreeSet;
20
21 import org.mybatis.generator.api.DAOMethodNameCalculator;
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.JavaVisibility;
26 import org.mybatis.generator.api.dom.java.Method;
27 import org.mybatis.generator.api.dom.java.Parameter;
28 import org.mybatis.generator.api.dom.java.PrimitiveTypeWrapper;
29 import org.mybatis.generator.api.dom.java.TopLevelClass;
30
31
32
33
34
35
36 public class InsertMethodGenerator extends AbstractDAOElementGenerator {
37
38 public InsertMethodGenerator() {
39 super();
40 }
41
42 @Override
43 public void addImplementationElements(TopLevelClass topLevelClass) {
44 Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
45 Method method = getMethodShell(importedTypes);
46
47 FullyQualifiedJavaType returnType = method.getReturnType();
48
49 StringBuilder sb = new StringBuilder();
50
51 if (returnType != null) {
52 sb.append("Object newKey = ");
53 }
54
55 sb.append(daoTemplate.getInsertMethod(introspectedTable
56 .getIbatis2SqlMapNamespace(), introspectedTable
57 .getInsertStatementId(), "record"));
58 method.addBodyLine(sb.toString());
59
60 if (returnType != null) {
61 if ("Object".equals(returnType.getShortName())) {
62
63 method.addBodyLine("return newKey;");
64 } else {
65 sb.setLength(0);
66
67 if (returnType.isPrimitive()) {
68 PrimitiveTypeWrapper ptw = returnType
69 .getPrimitiveTypeWrapper();
70 sb.append("return ((");
71 sb.append(ptw.getShortName());
72 sb.append(") newKey");
73 sb.append(").");
74 sb.append(ptw.getToPrimitiveMethod());
75 sb.append(';');
76 } else {
77 sb.append("return (");
78 sb.append(returnType.getShortName());
79 sb.append(") newKey;");
80 }
81
82 method.addBodyLine(sb.toString());
83 }
84 }
85
86 if (context.getPlugins().clientInsertMethodGenerated(method,
87 topLevelClass, introspectedTable)) {
88 topLevelClass.addImportedTypes(importedTypes);
89 topLevelClass.addMethod(method);
90 }
91 }
92
93 @Override
94 public void addInterfaceElements(Interface interfaze) {
95 Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
96 Method method = getMethodShell(importedTypes);
97
98 if (context.getPlugins().clientInsertMethodGenerated(method,
99 interfaze, introspectedTable)) {
100 interfaze.addImportedTypes(importedTypes);
101 interfaze.addMethod(method);
102 }
103 }
104
105 private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
106 Method method = new Method();
107
108 FullyQualifiedJavaType returnType;
109 if (introspectedTable.getGeneratedKey() != null) {
110 IntrospectedColumn introspectedColumn = introspectedTable
111 .getColumn(introspectedTable.getGeneratedKey().getColumn());
112 if (introspectedColumn == null) {
113
114
115
116 returnType = null;
117 } else {
118 returnType = introspectedColumn.getFullyQualifiedJavaType();
119 importedTypes.add(returnType);
120 }
121 } else {
122 returnType = null;
123 }
124
125 method.setReturnType(returnType);
126 method.setVisibility(JavaVisibility.PUBLIC);
127 DAOMethodNameCalculator methodNameCalculator = getDAOMethodNameCalculator();
128 method.setName(methodNameCalculator
129 .getInsertMethodName(introspectedTable));
130
131 FullyQualifiedJavaType parameterType = introspectedTable.getRules()
132 .calculateAllFieldsClass();
133
134 importedTypes.add(parameterType);
135 method.addParameter(new Parameter(parameterType, "record"));
136
137 for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) {
138 method.addException(fqjt);
139 importedTypes.add(fqjt);
140 }
141
142 context.getCommentGenerator().addGeneralMethodComment(method,
143 introspectedTable);
144
145 return method;
146 }
147 }