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.xmlmapper.elements;
17  
18  import java.util.List;
19  
20  import org.mybatis.generator.api.IntrospectedColumn;
21  import org.mybatis.generator.api.dom.xml.Attribute;
22  import org.mybatis.generator.api.dom.xml.XmlElement;
23  
24  public class ResultMapWithoutBLOBsElementGenerator extends AbstractXmlElementGenerator {
25  
26      private final boolean isSimple;
27  
28      public ResultMapWithoutBLOBsElementGenerator(boolean isSimple) {
29          super();
30          this.isSimple = isSimple;
31      }
32  
33      @Override
34      public void addElements(XmlElement parentElement) {
35          XmlElement answer = new XmlElement("resultMap"); //$NON-NLS-1$
36          answer.addAttribute(new Attribute("id", introspectedTable.getBaseResultMapId())); //$NON-NLS-1$
37  
38          String returnType;
39          if (isSimple) {
40              returnType = introspectedTable.getBaseRecordType();
41          } else {
42              if (introspectedTable.getRules().generateBaseRecordClass()) {
43                  returnType = introspectedTable.getBaseRecordType();
44              } else {
45                  returnType = introspectedTable.getPrimaryKeyType();
46              }
47          }
48  
49          answer.addAttribute(new Attribute("type", returnType)); //$NON-NLS-1$
50  
51          context.getCommentGenerator().addComment(answer);
52  
53          if (introspectedTable.isConstructorBased()) {
54              addResultMapConstructorElements(answer);
55          } else {
56              addResultMapElements(answer);
57          }
58  
59          if (context.getPlugins().sqlMapResultMapWithoutBLOBsElementGenerated(answer, introspectedTable)) {
60              parentElement.addElement(answer);
61          }
62      }
63  
64      private void addResultMapElements(XmlElement answer) {
65          buildResultMapItems(ResultElementType.ID, introspectedTable.getPrimaryKeyColumns()).forEach(answer::addElement);
66  
67          List<IntrospectedColumn> columns;
68          if (isSimple) {
69              columns = introspectedTable.getNonPrimaryKeyColumns();
70          } else {
71              columns = introspectedTable.getBaseColumns();
72          }
73  
74          buildResultMapItems(ResultElementType.RESULT, columns).forEach(answer::addElement);
75      }
76  
77      private void addResultMapConstructorElements(XmlElement answer) {
78          answer.addElement(buildConstructorElement(isSimple));
79      }
80  }