View Javadoc
1   /*
2    *    Copyright 2006-2023 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    *       https://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.internal.util.StringUtility.escapeStringForJava;
20  
21  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
22  import org.mybatis.generator.api.dom.java.Interface;
23  import org.mybatis.generator.api.dom.java.Method;
24  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.SelectByPrimaryKeyMethodGenerator;
25  
26  public class AnnotatedSelectByPrimaryKeyMethodGenerator extends SelectByPrimaryKeyMethodGenerator {
27  
28      private final boolean useResultMapIfAvailable;
29  
30      public AnnotatedSelectByPrimaryKeyMethodGenerator(boolean useResultMapIfAvailable, boolean isSimple) {
31          super(isSimple);
32          this.useResultMapIfAvailable = useResultMapIfAvailable;
33      }
34  
35      @Override
36      public void addMapperAnnotations(Interface interfaze, Method method) {
37          interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Select")); //$NON-NLS-1$
38  
39          buildInitialSelectAnnotationStrings().forEach(method::addAnnotation);
40  
41          StringBuilder sb = new StringBuilder();
42          javaIndent(sb, 1);
43          sb.append("\"from "); //$NON-NLS-1$
44          sb.append(escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
45          sb.append("\","); //$NON-NLS-1$
46          method.addAnnotation(sb.toString());
47  
48          buildByPrimaryKeyWhereClause().forEach(method::addAnnotation);
49  
50          method.addAnnotation("})"); //$NON-NLS-1$
51  
52          if (useResultMapIfAvailable) {
53              if (introspectedTable.getRules().generateBaseResultMap()
54                      || introspectedTable.getRules().generateResultMapWithBLOBs()) {
55                  addResultMapAnnotation(method);
56              } else {
57                  addAnnotatedResults(interfaze, method, introspectedTable.getNonPrimaryKeyColumns());
58              }
59          } else {
60              addAnnotatedResults(interfaze, method, introspectedTable.getNonPrimaryKeyColumns());
61          }
62      }
63  
64      private void addResultMapAnnotation(Method method) {
65  
66          String annotation = String.format("@ResultMap(\"%s.%s\")", //$NON-NLS-1$
67                  introspectedTable.getMyBatis3SqlMapNamespace(),
68                  introspectedTable.getRules().generateResultMapWithBLOBs()
69                      ? introspectedTable.getResultMapWithBLOBsId() : introspectedTable.getBaseResultMapId());
70          method.addAnnotation(annotation);
71      }
72  
73      @Override
74      public void addExtraImports(Interface interfaze) {
75          interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Select")); //$NON-NLS-1$
76  
77          if (useResultMapIfAvailable) {
78              if (introspectedTable.getRules().generateBaseResultMap()
79                      || introspectedTable.getRules().generateResultMapWithBLOBs()) {
80                  interfaze.addImportedType(
81                          new FullyQualifiedJavaType("org.apache.ibatis.annotations.ResultMap")); //$NON-NLS-1$
82              } else {
83                  addAnnotationImports(interfaze);
84              }
85          } else {
86              addAnnotationImports(interfaze);
87          }
88      }
89  
90      private void addAnnotationImports(Interface interfaze) {
91          addAnnotatedSelectImports(interfaze);
92      }
93  }