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 org.mybatis.generator.api.dom.xml.Attribute;
19  import org.mybatis.generator.api.dom.xml.XmlElement;
20  
21  public class ResultMapWithBLOBsElementGenerator extends AbstractXmlElementGenerator {
22  
23      public ResultMapWithBLOBsElementGenerator() {
24          super();
25      }
26  
27      @Override
28      public void addElements(XmlElement parentElement) {
29          XmlElement answer = new XmlElement("resultMap"); //$NON-NLS-1$
30  
31          answer.addAttribute(new Attribute("id", //$NON-NLS-1$
32                  introspectedTable.getResultMapWithBLOBsId()));
33  
34          String returnType;
35          if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
36              returnType = introspectedTable.getRecordWithBLOBsType();
37          } else {
38              // table has BLOBs, but no BLOB class - BLOB fields must be
39              // in the base class
40              returnType = introspectedTable.getBaseRecordType();
41          }
42  
43          answer.addAttribute(new Attribute("type", returnType)); //$NON-NLS-1$
44  
45          if (!introspectedTable.isConstructorBased()) {
46              answer.addAttribute(new Attribute("extends", //$NON-NLS-1$
47                      introspectedTable.getBaseResultMapId()));
48          }
49  
50          context.getCommentGenerator().addComment(answer);
51  
52          if (introspectedTable.isConstructorBased()) {
53              addResultMapConstructorElements(answer);
54          } else {
55              addResultMapElements(answer);
56          }
57  
58          if (context.getPlugins().sqlMapResultMapWithBLOBsElementGenerated(answer, introspectedTable)) {
59              parentElement.addElement(answer);
60          }
61      }
62  
63      private void addResultMapElements(XmlElement answer) {
64          buildResultMapItems(ResultElementType.RESULT, introspectedTable.getBLOBColumns()).forEach(answer::addElement);
65      }
66  
67      private void addResultMapConstructorElements(XmlElement answer) {
68          answer.addElement(buildConstructorElement(true));
69      }
70  }