View Javadoc
1   /**
2    *    Copyright 2006-2016 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    *       http://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.codegen.mybatis3.javamapper.elements;
17  
18  import static org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap;
19  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
20  
21  import org.mybatis.generator.api.IntrospectedColumn;
22  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
23  import org.mybatis.generator.api.dom.java.Interface;
24  import org.mybatis.generator.api.dom.java.Method;
25  import org.mybatis.generator.codegen.AbstractGenerator;
26  import org.mybatis.generator.config.GeneratedKey;
27  
28  /**
29   * 
30   * @author Jeff Butler
31   */
32  public abstract class AbstractJavaMapperMethodGenerator extends
33          AbstractGenerator {
34      public abstract void addInterfaceElements(Interface interfaze);
35  
36      public AbstractJavaMapperMethodGenerator() {
37          super();
38      }
39      
40      protected String getResultAnnotation(Interface interfaze, IntrospectedColumn introspectedColumn,
41              boolean idColumn, boolean constructorBased) {
42          StringBuilder sb = new StringBuilder();
43          if (constructorBased) {
44              interfaze.addImportedType(introspectedColumn.getFullyQualifiedJavaType());
45              sb.append("@Arg(column=\""); //$NON-NLS-1$
46              sb.append(getRenamedColumnNameForResultMap(introspectedColumn));
47              sb.append("\", javaType="); //$NON-NLS-1$
48              sb.append(introspectedColumn.getFullyQualifiedJavaType().getShortName());
49              sb.append(".class"); //$NON-NLS-1$
50          } else {
51              sb.append("@Result(column=\""); //$NON-NLS-1$
52              sb.append(getRenamedColumnNameForResultMap(introspectedColumn));
53              sb.append("\", property=\""); //$NON-NLS-1$
54              sb.append(introspectedColumn.getJavaProperty());
55              sb.append('\"');
56          }
57          
58          if (stringHasValue(introspectedColumn.getTypeHandler())) {
59              FullyQualifiedJavaType fqjt =
60                  new FullyQualifiedJavaType(introspectedColumn.getTypeHandler());
61              interfaze.addImportedType(fqjt);
62              sb.append(", typeHandler="); //$NON-NLS-1$
63              sb.append(fqjt.getShortName());
64              sb.append(".class"); //$NON-NLS-1$
65          }
66          
67          sb.append(", jdbcType=JdbcType."); //$NON-NLS-1$
68          sb.append(introspectedColumn.getJdbcTypeName());
69          if (idColumn) {
70              sb.append(", id=true"); //$NON-NLS-1$
71          }
72          sb.append(')');
73  
74          return sb.toString();
75      }
76  
77      protected void addGeneratedKeyAnnotation(Interface interfaze, Method method,
78              GeneratedKey gk) {
79          StringBuilder sb = new StringBuilder();
80          IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
81          if (introspectedColumn != null) {
82              if (gk.isJdbcStandard()) {
83                  interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Options")); //$NON-NLS-1$
84                  sb.append("@Options(useGeneratedKeys=true,keyProperty=\""); //$NON-NLS-1$
85                  sb.append(introspectedColumn.getJavaProperty());
86                  sb.append("\")"); //$NON-NLS-1$
87                  method.addAnnotation(sb.toString());
88              } else {
89                  interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.SelectKey")); //$NON-NLS-1$
90                  FullyQualifiedJavaType fqjt = introspectedColumn.getFullyQualifiedJavaType();
91                  interfaze.addImportedType(fqjt);
92                  sb.append("@SelectKey(statement=\""); //$NON-NLS-1$
93                  sb.append(gk.getRuntimeSqlStatement());
94                  sb.append("\", keyProperty=\""); //$NON-NLS-1$
95                  sb.append(introspectedColumn.getJavaProperty());
96                  sb.append("\", before="); //$NON-NLS-1$
97                  sb.append(gk.isIdentity() ? "false" : "true"); //$NON-NLS-1$ //$NON-NLS-2$
98                  sb.append(", resultType="); //$NON-NLS-1$
99                  sb.append(fqjt.getShortName());
100                 sb.append(".class)"); //$NON-NLS-1$
101                 method.addAnnotation(sb.toString());
102             }
103         }
104     }
105 }