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 java.util.List;
19  import java.util.Set;
20  import java.util.TreeSet;
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.JavaVisibility;
26  import org.mybatis.generator.api.dom.java.Method;
27  import org.mybatis.generator.api.dom.java.Parameter;
28  
29  /**
30   * 
31   * @author Jeff Butler
32   * 
33   */
34  public class SelectByPrimaryKeyMethodGenerator extends
35          AbstractJavaMapperMethodGenerator {
36  
37      private boolean isSimple;
38      
39      public SelectByPrimaryKeyMethodGenerator(boolean isSimple) {
40          super();
41          this.isSimple = isSimple;
42      }
43  
44      @Override
45      public void addInterfaceElements(Interface interfaze) {
46          Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
47          Method method = new Method();
48          method.setVisibility(JavaVisibility.PUBLIC);
49  
50          FullyQualifiedJavaType returnType = introspectedTable.getRules()
51                  .calculateAllFieldsClass();
52          method.setReturnType(returnType);
53          importedTypes.add(returnType);
54  
55          method.setName(introspectedTable.getSelectByPrimaryKeyStatementId());
56  
57          if (!isSimple && introspectedTable.getRules().generatePrimaryKeyClass()) {
58              FullyQualifiedJavaType type = new FullyQualifiedJavaType(
59                      introspectedTable.getPrimaryKeyType());
60              importedTypes.add(type);
61              method.addParameter(new Parameter(type, "key")); //$NON-NLS-1$
62          } else {
63              // no primary key class - fields are in the base class
64              // if more than one PK field, then we need to annotate the
65              // parameters
66              // for MyBatis3
67              List<IntrospectedColumn> introspectedColumns = introspectedTable
68                      .getPrimaryKeyColumns();
69              boolean annotate = introspectedColumns.size() > 1;
70              if (annotate) {
71                  importedTypes.add(new FullyQualifiedJavaType(
72                          "org.apache.ibatis.annotations.Param")); //$NON-NLS-1$
73              }
74              StringBuilder sb = new StringBuilder();
75              for (IntrospectedColumn introspectedColumn : introspectedColumns) {
76                  FullyQualifiedJavaType type = introspectedColumn
77                          .getFullyQualifiedJavaType();
78                  importedTypes.add(type);
79                  Parameter parameter = new Parameter(type, introspectedColumn
80                          .getJavaProperty());
81                  if (annotate) {
82                      sb.setLength(0);
83                      sb.append("@Param(\""); //$NON-NLS-1$
84                      sb.append(introspectedColumn.getJavaProperty());
85                      sb.append("\")"); //$NON-NLS-1$
86                      parameter.addAnnotation(sb.toString());
87                  }
88                  method.addParameter(parameter);
89              }
90          }
91          
92          addMapperAnnotations(interfaze, method);
93  
94          context.getCommentGenerator().addGeneralMethodComment(method,
95                  introspectedTable);
96  
97          if (context.getPlugins().clientSelectByPrimaryKeyMethodGenerated(
98                  method, interfaze, introspectedTable)) {
99              interfaze.addImportedTypes(importedTypes);
100             interfaze.addMethod(method);
101         }
102     }
103 
104     public void addMapperAnnotations(Interface interfaze, Method method) {
105     }
106 }