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;
17  
18  import static org.mybatis.generator.internal.util.messages.Messages.getString;
19  
20  import java.util.Set;
21  import java.util.TreeSet;
22  
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  public class SelectByExampleWithoutBLOBsMethodGenerator extends AbstractJavaMapperMethodGenerator {
30  
31      public SelectByExampleWithoutBLOBsMethodGenerator() {
32          super();
33      }
34  
35      @Override
36      public void addInterfaceElements(Interface interfaze) {
37          Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();
38          FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
39          importedTypes.add(type);
40          importedTypes.add(FullyQualifiedJavaType.getNewListInstance());
41  
42          Method method = new Method(introspectedTable.getSelectByExampleStatementId());
43          method.setVisibility(JavaVisibility.PUBLIC);
44          method.setAbstract(true);
45  
46          FullyQualifiedJavaType returnType = FullyQualifiedJavaType.getNewListInstance();
47          FullyQualifiedJavaType listType;
48          if (introspectedTable.getRules().generateBaseRecordClass()) {
49              listType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
50          } else if (introspectedTable.getRules().generatePrimaryKeyClass()) {
51              listType = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType());
52          } else {
53              throw new RuntimeException(getString("RuntimeError.12")); //$NON-NLS-1$
54          }
55  
56          importedTypes.add(listType);
57          returnType.addTypeArgument(listType);
58          method.setReturnType(returnType);
59  
60          method.addParameter(new Parameter(type, "example")); //$NON-NLS-1$
61  
62          context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
63  
64          addMapperAnnotations(interfaze, method);
65  
66          if (context.getPlugins()
67                  .clientSelectByExampleWithoutBLOBsMethodGenerated(method, interfaze, introspectedTable)) {
68              addExtraImports(interfaze);
69              interfaze.addImportedTypes(importedTypes);
70              interfaze.addMethod(method);
71          }
72      }
73  
74      public void addMapperAnnotations(Interface interfaze, Method method) {
75          // extension point for subclasses
76      }
77  
78      public void addExtraImports(Interface interfaze) {
79          // extension point for subclasses
80      }
81  }