View Javadoc
1   /**
2    *    Copyright 2006-2016 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    *       http://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.ibatis2.sqlmap.elements;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.isTrue;
19  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
20  
21  import org.mybatis.generator.api.IntrospectedColumn;
22  import org.mybatis.generator.api.dom.xml.Attribute;
23  import org.mybatis.generator.api.dom.xml.XmlElement;
24  import org.mybatis.generator.codegen.ibatis2.Ibatis2FormattingUtilities;
25  import org.mybatis.generator.config.PropertyRegistry;
26  
27  /**
28   * 
29   * @author Jeff Butler
30   * 
31   */
32  public class ResultMapWithoutBLOBsElementGenerator extends
33          AbstractXmlElementGenerator {
34  
35      public ResultMapWithoutBLOBsElementGenerator() {
36          super();
37      }
38  
39      @Override
40      public void addElements(XmlElement parentElement) {
41          boolean useColumnIndex = isTrue(introspectedTable
42                          .getTableConfigurationProperty(PropertyRegistry.TABLE_USE_COLUMN_INDEXES));
43          XmlElement answer = new XmlElement("resultMap"); //$NON-NLS-1$
44          answer.addAttribute(new Attribute("id", //$NON-NLS-1$
45                  introspectedTable.getBaseResultMapId()));
46  
47          String returnType;
48          if (introspectedTable.getRules().generateBaseRecordClass()) {
49              returnType = introspectedTable.getBaseRecordType();
50          } else {
51              returnType = introspectedTable.getPrimaryKeyType();
52          }
53  
54          answer.addAttribute(new Attribute("class", //$NON-NLS-1$
55                  returnType));
56  
57          context.getCommentGenerator().addComment(answer);
58  
59          int i = 1;
60          if (stringHasValue(introspectedTable
61                  .getSelectByPrimaryKeyQueryId())
62                  || stringHasValue(introspectedTable
63                          .getSelectByExampleQueryId())) {
64              i++;
65          }
66  
67          for (IntrospectedColumn introspectedColumn : introspectedTable
68                  .getNonBLOBColumns()) {
69              XmlElement resultElement = new XmlElement("result"); //$NON-NLS-1$
70  
71              if (useColumnIndex) {
72                  resultElement.addAttribute(new Attribute(
73                          "columnIndex", Integer.toString(i++))); //$NON-NLS-1$
74              } else {
75                  resultElement
76                          .addAttribute(new Attribute(
77                                  "column", Ibatis2FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn))); //$NON-NLS-1$
78              }
79  
80              resultElement.addAttribute(new Attribute(
81                      "property", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
82              resultElement.addAttribute(new Attribute("jdbcType", //$NON-NLS-1$
83                      introspectedColumn.getJdbcTypeName()));
84  
85              if (stringHasValue(introspectedColumn
86                      .getTypeHandler())) {
87                  resultElement.addAttribute(new Attribute(
88                          "typeHandler", introspectedColumn.getTypeHandler())); //$NON-NLS-1$
89              }
90  
91              answer.addElement(resultElement);
92          }
93  
94          if (context.getPlugins()
95                  .sqlMapResultMapWithoutBLOBsElementGenerated(answer,
96                          introspectedTable)) {
97              parentElement.addElement(answer);
98          }
99      }
100 }