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 DeleteByPrimaryKeyMethodGenerator extends
35          AbstractJavaMapperMethodGenerator {
36  
37      private boolean isSimple;
38      
39      public DeleteByPrimaryKeyMethodGenerator(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          method.setReturnType(FullyQualifiedJavaType.getIntInstance());
50          method.setName(introspectedTable.getDeleteByPrimaryKeyStatementId());
51  
52          if (!isSimple && introspectedTable.getRules().generatePrimaryKeyClass()) {
53              FullyQualifiedJavaType type = new FullyQualifiedJavaType(
54                      introspectedTable.getPrimaryKeyType());
55              importedTypes.add(type);
56              method.addParameter(new Parameter(type, "key")); //$NON-NLS-1$
57          } else {
58              // no primary key class - fields are in the base class
59              // if more than one PK field, then we need to annotate the
60              // parameters
61              // for MyBatis
62              List<IntrospectedColumn> introspectedColumns = introspectedTable
63                      .getPrimaryKeyColumns();
64              boolean annotate = introspectedColumns.size() > 1;
65              if (annotate) {
66                  importedTypes.add(new FullyQualifiedJavaType(
67                          "org.apache.ibatis.annotations.Param")); //$NON-NLS-1$
68              }
69              StringBuilder sb = new StringBuilder();
70              for (IntrospectedColumn introspectedColumn : introspectedColumns) {
71                  FullyQualifiedJavaType type = introspectedColumn
72                          .getFullyQualifiedJavaType();
73                  importedTypes.add(type);
74                  Parameter parameter = new Parameter(type, introspectedColumn
75                          .getJavaProperty());
76                  if (annotate) {
77                      sb.setLength(0);
78                      sb.append("@Param(\""); //$NON-NLS-1$
79                      sb.append(introspectedColumn.getJavaProperty());
80                      sb.append("\")"); //$NON-NLS-1$
81                      parameter.addAnnotation(sb.toString());
82                  }
83                  method.addParameter(parameter);
84              }
85          }
86  
87          context.getCommentGenerator().addGeneralMethodComment(method,
88                  introspectedTable);
89  
90          addMapperAnnotations(interfaze, method);
91          
92          if (context.getPlugins().clientDeleteByPrimaryKeyMethodGenerated(
93                  method, interfaze, introspectedTable)) {
94              interfaze.addImportedTypes(importedTypes);
95              interfaze.addMethod(method);
96          }
97      }
98  
99      public void addMapperAnnotations(Interface interfaze, Method method) {
100     }
101 }