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.annotated;
17  
18  import static org.mybatis.generator.api.dom.OutputUtilities.javaIndent;
19  
20  import java.util.Iterator;
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.Method;
26  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.SelectByExampleWithoutBLOBsMethodGenerator;
27  
28  /**
29   * 
30   * @author Jeff Butler
31   */
32  public class AnnotatedSelectByExampleWithoutBLOBsMethodGenerator extends
33      SelectByExampleWithoutBLOBsMethodGenerator {
34  
35      public AnnotatedSelectByExampleWithoutBLOBsMethodGenerator() {
36          super();
37      }
38  
39      @Override
40      public void addMapperAnnotations(Interface interfaze, Method method) {
41          FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType());
42          interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.SelectProvider")); //$NON-NLS-1$
43          interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.type.JdbcType")); //$NON-NLS-1$
44  
45          if (introspectedTable.isConstructorBased()) {
46              interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Arg")); //$NON-NLS-1$
47              interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.ConstructorArgs")); //$NON-NLS-1$
48          } else {
49              interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Result")); //$NON-NLS-1$
50              interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Results")); //$NON-NLS-1$
51          }
52          
53          StringBuilder sb = new StringBuilder();
54          sb.append("@SelectProvider(type="); //$NON-NLS-1$
55          sb.append(fqjt.getShortName());
56          sb.append(".class, method=\""); //$NON-NLS-1$
57          sb.append(introspectedTable.getSelectByExampleStatementId());
58          sb.append("\")"); //$NON-NLS-1$
59          
60          method.addAnnotation(sb.toString());
61          
62          if (introspectedTable.isConstructorBased()) {
63              method.addAnnotation("@ConstructorArgs({"); //$NON-NLS-1$
64          } else {
65              method.addAnnotation("@Results({"); //$NON-NLS-1$
66          }
67          
68          Iterator<IntrospectedColumn> iterPk = introspectedTable.getPrimaryKeyColumns().iterator();
69          Iterator<IntrospectedColumn> iterNonPk = introspectedTable.getBaseColumns().iterator();
70          while (iterPk.hasNext()) {
71              IntrospectedColumn introspectedColumn = iterPk.next();
72              sb.setLength(0);
73              javaIndent(sb, 1);
74              sb.append(getResultAnnotation(interfaze, introspectedColumn, true,
75                      introspectedTable.isConstructorBased()));
76              
77              if (iterPk.hasNext() || iterNonPk.hasNext()) {
78                  sb.append(',');
79              }
80              
81              method.addAnnotation(sb.toString());
82          }
83  
84          while (iterNonPk.hasNext()) {
85              IntrospectedColumn introspectedColumn = iterNonPk.next();
86              sb.setLength(0);
87              javaIndent(sb, 1);
88              sb.append(getResultAnnotation(interfaze, introspectedColumn, false,
89                      introspectedTable.isConstructorBased()));
90              
91              if (iterNonPk.hasNext()) {
92                  sb.append(',');
93              }
94              
95              method.addAnnotation(sb.toString());
96          }
97          
98          method.addAnnotation("})"); //$NON-NLS-1$
99      }
100 }