1
2
3
4
5
6
7
8
9
10
11
12
13
14
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"));
38
39 buildInitialSelectAnnotationStrings().forEach(method::addAnnotation);
40
41 StringBuilder sb = new StringBuilder();
42 javaIndent(sb, 1);
43 sb.append("\"from ");
44 sb.append(escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()));
45 sb.append("\",");
46 method.addAnnotation(sb.toString());
47
48 buildByPrimaryKeyWhereClause().forEach(method::addAnnotation);
49
50 method.addAnnotation("})");
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\")",
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"));
76
77 if (useResultMapIfAvailable) {
78 if (introspectedTable.getRules().generateBaseResultMap()
79 || introspectedTable.getRules().generateResultMapWithBLOBs()) {
80 interfaze.addImportedType(
81 new FullyQualifiedJavaType("org.apache.ibatis.annotations.ResultMap"));
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 }