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  import static org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities.getAliasedEscapedColumnName;
20  import static org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities.getParameterClause;
21  import static org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities.getSelectListPhrase;
22  import static org.mybatis.generator.internal.util.StringUtility.escapeStringForJava;
23  
24  import java.util.Iterator;
25  
26  import org.mybatis.generator.api.IntrospectedColumn;
27  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
28  import org.mybatis.generator.api.dom.java.Interface;
29  import org.mybatis.generator.api.dom.java.Method;
30  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.SelectByPrimaryKeyMethodGenerator;
31  
32  /**
33   * 
34   * @author Jeff Butler
35   */
36  public class AnnotatedSelectByPrimaryKeyMethodGenerator extends
37      SelectByPrimaryKeyMethodGenerator {
38      
39      private boolean useResultMapIfAvailable;
40  
41      public AnnotatedSelectByPrimaryKeyMethodGenerator(boolean useResultMapIfAvailable, boolean isSimple) {
42          super(isSimple);
43          this.useResultMapIfAvailable = useResultMapIfAvailable;
44      }
45  
46      @Override
47      public void addMapperAnnotations(Interface interfaze, Method method) {
48          interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Select")); //$NON-NLS-1$
49  
50          StringBuilder sb = new StringBuilder();
51          method.addAnnotation("@Select({"); //$NON-NLS-1$
52          javaIndent(sb, 1);
53          sb.append("\"select\","); //$NON-NLS-1$
54          method.addAnnotation(sb.toString());
55          
56          Iterator<IntrospectedColumn> iter = introspectedTable
57              .getAllColumns().iterator();
58          sb.setLength(0);
59          javaIndent(sb, 1);
60          sb.append('"');
61          boolean hasColumns = false;
62          while (iter.hasNext()) {
63              sb.append(escapeStringForJava(getSelectListPhrase(iter.next())));
64              hasColumns = true;
65  
66              if (iter.hasNext()) {
67                  sb.append(", "); //$NON-NLS-1$
68              }
69  
70              if (sb.length() > 80) {
71                  sb.append("\","); //$NON-NLS-1$
72                  method.addAnnotation(sb.toString());
73                  
74                  sb.setLength(0);
75                  javaIndent(sb, 1);
76                  sb.append('"');
77                  hasColumns = false;
78              }
79          }
80  
81          if (hasColumns) {
82              sb.append("\","); //$NON-NLS-1$
83              method.addAnnotation(sb.toString());
84          }
85          
86          sb.setLength(0);
87          javaIndent(sb, 1);
88          sb.append("\"from "); //$NON-NLS-1$
89          sb.append(escapeStringForJava(introspectedTable
90                  .getAliasedFullyQualifiedTableNameAtRuntime()));
91          sb.append("\","); //$NON-NLS-1$
92          method.addAnnotation(sb.toString());
93          
94          boolean and = false;
95          iter = introspectedTable.getPrimaryKeyColumns().iterator();
96          while (iter.hasNext()) {
97              IntrospectedColumn introspectedColumn = iter.next();
98          
99              sb.setLength(0);
100             javaIndent(sb, 1);
101             if (and) {
102                 sb.append("  \"and "); //$NON-NLS-1$
103             } else {
104                 sb.append("\"where "); //$NON-NLS-1$
105                 and = true;
106             }
107 
108             sb.append(escapeStringForJava(getAliasedEscapedColumnName(introspectedColumn)));
109             sb.append(" = "); //$NON-NLS-1$
110             sb.append(getParameterClause(introspectedColumn));
111             sb.append('\"');
112             if (iter.hasNext()) {
113                 sb.append(',');
114             }
115             method.addAnnotation(sb.toString());
116         }
117 
118         method.addAnnotation("})"); //$NON-NLS-1$
119 
120         if (useResultMapIfAvailable) {
121             if (introspectedTable.getRules().generateBaseResultMap()
122                     || introspectedTable.getRules().generateResultMapWithBLOBs()) {
123                 addResultMapAnnotation(interfaze, method);
124             } else {
125                 addAnnotatedResults(interfaze, method);
126             }
127         } else {
128             addAnnotatedResults(interfaze, method);
129         }
130     }
131     
132     private void addResultMapAnnotation(Interface interfaze, Method method) {
133         interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.ResultMap")); //$NON-NLS-1$
134         
135         String annotation = String.format("@ResultMap(\"%s.%s\")", //$NON-NLS-1$
136         		introspectedTable.getMyBatis3SqlMapNamespace(),
137                 introspectedTable.getRules().generateResultMapWithBLOBs() ?
138                         introspectedTable.getResultMapWithBLOBsId() : introspectedTable.getBaseResultMapId());
139         method.addAnnotation(annotation);
140     }
141     
142     private void addAnnotatedResults(Interface interfaze, Method method) {
143         interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.type.JdbcType")); //$NON-NLS-1$
144         
145         if (introspectedTable.isConstructorBased()) {
146             interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Arg")); //$NON-NLS-1$
147             interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.ConstructorArgs")); //$NON-NLS-1$
148             method.addAnnotation("@ConstructorArgs({"); //$NON-NLS-1$
149         } else {
150             interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Result")); //$NON-NLS-1$
151             interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Results")); //$NON-NLS-1$
152             method.addAnnotation("@Results({"); //$NON-NLS-1$
153         }
154         
155         StringBuilder sb = new StringBuilder();
156         
157         Iterator<IntrospectedColumn> iterPk = introspectedTable.getPrimaryKeyColumns().iterator();
158         Iterator<IntrospectedColumn> iterNonPk = introspectedTable.getNonPrimaryKeyColumns().iterator();
159         while (iterPk.hasNext()) {
160             IntrospectedColumn introspectedColumn = iterPk.next();
161             sb.setLength(0);
162             javaIndent(sb, 1);
163             sb.append(getResultAnnotation(interfaze, introspectedColumn, true,
164                     introspectedTable.isConstructorBased()));
165             
166             if (iterPk.hasNext() || iterNonPk.hasNext()) {
167                 sb.append(',');
168             }
169             
170             method.addAnnotation(sb.toString());
171         }
172 
173         while (iterNonPk.hasNext()) {
174             IntrospectedColumn introspectedColumn = iterNonPk.next();
175             sb.setLength(0);
176             javaIndent(sb, 1);
177             sb.append(getResultAnnotation(interfaze, introspectedColumn, false,
178                     introspectedTable.isConstructorBased()));
179             
180             if (iterNonPk.hasNext()) {
181                 sb.append(',');
182             }
183             
184             method.addAnnotation(sb.toString());
185         }
186         
187         method.addAnnotation("})"); //$NON-NLS-1$
188     }
189 }