Java Code Examples for org.mybatis.generator.api.dom.xml.XmlElement#addElement()

The following examples show how to use org.mybatis.generator.api.dom.xml.XmlElement#addElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HsqldbUpsertPlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
@Override
protected void generateSqlMapContentSelective(IntrospectedTable introspectedTable, XmlElement parent) {
  generateTextBlockAppendTableName("merge into ", introspectedTable, parent);
  generateTextBlock(" using (values ", parent);
  generateParametersSeparateByComma(PROPERTY_PREFIX, true, introspectedTable.getAllColumns(), parent);
  generateTextBlock(" ) temp ", parent);
  generateActualColumnNamesWithParenthesis(PROPERTY_PREFIX, true, introspectedTable.getAllColumns(), parent);
  generateTextBlock(" on ( ", parent);

  XmlElement include = new XmlElement("include");
  include.addAttribute(new Attribute("refid", IDENTIFIERS_ARRAY_CONDITIONS));
  parent.addElement(include);

  generateTextBlock(" ) ", parent);

  generateTextBlock(" when matched then update set ", parent);
  generateCopyForSetByPrefix(PROPERTY_PREFIX,
      introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime() + ".", "temp.", true, introspectedTable, parent);

  generateTextBlock(" when not matched then insert ", parent);
  generateActualColumnNamesWithParenthesis(PROPERTY_PREFIX, true, introspectedTable.getAllColumns(), parent);
  generateTextBlock(" values ", parent);
  generateActualColumnNamesWithParenthesis(PROPERTY_PREFIX, "temp.", true, introspectedTable.getAllColumns(), parent);
}
 
Example 2
Source File: OracleSupport.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
/**
 * 在单条插入动态sql中增加查询序列,以实现oracle主键自增
 *
 * @author 吴帅
 * @parameter @param element
 * @parameter @param introspectedTable
 * @createDate 2015年9月29日 下午12:00:37
 */
@Override
public void adaptInsertSelective(XmlElement element, IntrospectedTable introspectedTable) {
    TableConfiguration tableConfiguration = introspectedTable.getTableConfiguration();
    Properties properties = tableConfiguration.getProperties();
    String incrementFieldName = properties.getProperty("incrementField");
    if (incrementFieldName != null) {// 有自增字段的配置
        List<Element> elements = element.getElements();
        XmlElement selectKey = new XmlElement("selectKey");
        selectKey.addAttribute(new Attribute("keyProperty", incrementFieldName));
        selectKey.addAttribute(new Attribute("resultType", "java.lang.Long"));
        selectKey.addAttribute(new Attribute("order", "BEFORE"));
        selectKey.addElement(new TextElement("select "));
        XmlElement includeSeq = new XmlElement("include");
        includeSeq.addAttribute(new Attribute("refid", "TABLE_SEQUENCE"));
        selectKey.addElement(includeSeq);
        selectKey.addElement(new TextElement(" from dual"));
        elements.add(0, selectKey);
    }
}
 
Example 3
Source File: DeleteByExampleElementGenerator.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    XmlElement answer = new XmlElement("delete");

    String fqjt = introspectedTable.getExampleType();

    answer.addAttribute(new Attribute(
            "id", introspectedTable.getDeleteByExampleStatementId()));
    answer.addAttribute(new Attribute("parameterType", fqjt));

    context.getCommentGenerator().addComment(answer);

    StringBuilder sb = new StringBuilder();
    sb.append("delete from ");
    sb.append(introspectedTable
            .getAliasedFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));
    answer.addElement(getExampleIncludeElement());

    if (context.getPlugins().sqlMapDeleteByExampleElementGenerated(
            answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example 4
Source File: GenPlugin.java    From scaffold-cloud with MIT License 6 votes vote down vote up
/**
 * 查询
 *
 * @param id
 * @param tableName
 * @param pkColumn
 * @return
 */
private XmlElement createSelect(String id, String tableName, IntrospectedColumn pkColumn) {
    XmlElement select = new XmlElement("select");
    select.addAttribute(new Attribute("id", id));
    select.addAttribute(new Attribute("resultMap", "BaseResultMap"));

    StringBuilder selectStr = new StringBuilder("select <include refid=\"sql_columns\" /> from ").append(tableName);
    if (null != pkColumn) {
        selectStr.append(" where ").append(pkColumn.getActualColumnName()).append(" = #{").append(pkColumn.getJavaProperty()).append("}");
    } else {
        if (!"selectMap".equals(id)) {
            selectStr.append(" <include refid=\"sql_where\" />");
        } else {
            selectStr.append(" <include refid=\"sql_map_where\" />");
        }
    }
    if ("selectPage".equals(id)) {
        selectStr.append(" limit #{page.startRow}, #{page.pageSize}");
    }
    if ("selectLockById".equals(id)) {
        selectStr.append(" for update");
    }
    select.addElement(new TextElement(selectStr.toString()));
    return select;
}
 
Example 5
Source File: XmlElementGeneratorTools.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 生成 xxxByPrimaryKey 的where 语句
 * @param element
 * @param primaryKeyColumns
 * @param prefix
 * @return
 */
public static void generateWhereByPrimaryKeyTo(XmlElement element, List<IntrospectedColumn> primaryKeyColumns, String prefix) {
    StringBuilder sb = new StringBuilder();
    boolean and = false;
    for (IntrospectedColumn introspectedColumn : primaryKeyColumns) {
        sb.setLength(0);
        if (and) {
            sb.append("  and ");
        } else {
            sb.append("where ");
            and = true;
        }

        sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
        sb.append(" = ");
        sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix));
        element.addElement(new TextElement(sb.toString()));
    }
}
 
Example 6
Source File: HsqldbUpsertPlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
@Override
protected void generateSqlMapContent(IntrospectedTable introspectedTable, XmlElement parent) {
  generateTextBlockAppendTableName("merge into ", introspectedTable, parent);
  generateTextBlock(" using (values ", parent);
  generateParametersSeparateByComma(PROPERTY_PREFIX,introspectedTable.getAllColumns(), parent);
  generateTextBlock(" ) temp ", parent);
  generateActualColumnNamesWithParenthesis(introspectedTable.getAllColumns(), parent);
  generateTextBlock(" on ( ", parent);

  XmlElement include = new XmlElement("include");
  include.addAttribute(new Attribute("refid", IDENTIFIERS_ARRAY_CONDITIONS));
  parent.addElement(include);

  generateTextBlock(" ) ", parent);

  generateTextBlock(" when matched then update set ", parent);
  generateCopyForSetByPrefix(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime() + ".", "temp.", introspectedTable, parent);

  generateTextBlock(" when not matched then insert ", parent);
  generateActualColumnNamesWithParenthesis(introspectedTable.getAllColumns(), parent);
  generateTextBlock(" values ", parent);
  generateActualColumnNamesWithParenthesis(PROPERTY_PREFIX, "temp.", false, introspectedTable.getAllColumns(), parent);
}
 
Example 7
Source File: DeleteByExampleElementGenerator.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    XmlElement answer = new XmlElement("delete"); //$NON-NLS-1$

    answer.addAttribute(new Attribute(
            "id", introspectedTable.getDeleteByExampleStatementId())); //$NON-NLS-1$
    answer.addAttribute(new Attribute(
            "parameterClass", introspectedTable.getExampleType())); //$NON-NLS-1$

    context.getCommentGenerator().addComment(answer);

    StringBuilder sb = new StringBuilder();
    sb.append("delete from "); //$NON-NLS-1$
    sb.append(introspectedTable
            .getAliasedFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));

    XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$
    sb.setLength(0);
    sb.append(introspectedTable.getIbatis2SqlMapNamespace());
    sb.append('.');
    sb.append(introspectedTable.getExampleWhereClauseId());
    includeElement.addAttribute(new Attribute("refid", //$NON-NLS-1$
            sb.toString()));

    answer.addElement(includeElement);

    if (context.getPlugins().sqlMapDeleteByExampleElementGenerated(
            answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example 8
Source File: HsqldbPaginationPlugin.java    From dolphin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
  XmlElement isNotNullElement = new XmlElement("if");
  isNotNullElement.addAttribute(new Attribute("test", "limit != null and limit>=0 and offset != null"));
  isNotNullElement.addElement(new TextElement("limit #{limit} offset #{offset}"));
  element.addElement(isNotNullElement);
  return true;
}
 
Example 9
Source File: ResultMapWithBLOBsElementGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    XmlElement answer = new XmlElement("resultMap"); //$NON-NLS-1$

    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
            introspectedTable.getResultMapWithBLOBsId()));

    String returnType;
    if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
        returnType = introspectedTable.getRecordWithBLOBsType();
    } else {
        // table has BLOBs, but no BLOB class - BLOB fields must be
        // in the base class
        returnType = introspectedTable.getBaseRecordType();
    }

    answer.addAttribute(new Attribute("type", //$NON-NLS-1$
            returnType));

    if (!introspectedTable.isConstructorBased()) {
        answer.addAttribute(new Attribute("extends", //$NON-NLS-1$
            introspectedTable.getBaseResultMapId()));
    }

    context.getCommentGenerator().addComment(answer);

    if (introspectedTable.isConstructorBased()) {
        addResultMapConstructorElements(answer);
    } else {
        addResultMapElements(answer);
    }

    if (context.getPlugins()
            .sqlMapResultMapWithBLOBsElementGenerated(answer,
                    introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example 10
Source File: MapperCommentGenerator.java    From tk-mybatis with MIT License 5 votes vote down vote up
/**
 * xml中的注释
 *
 * @param xmlElement
 */
public void addComment(XmlElement xmlElement) {
    xmlElement.addElement(new TextElement("<!--"));
    StringBuilder sb = new StringBuilder();
    sb.append("  WARNING - ");
    sb.append(MergeConstants.NEW_ELEMENT_TAG);
    xmlElement.addElement(new TextElement(sb.toString()));
    xmlElement.addElement(new TextElement("-->"));
}
 
Example 11
Source File: SQLServerUpsertPlugin.java    From dolphin with Apache License 2.0 5 votes vote down vote up
@Override protected void generateSqlMapContentSelective(IntrospectedTable introspectedTable, XmlElement parent) {
  generateTextBlockAppendTableName("update ", introspectedTable,parent);
  generateTextBlock(" set ",parent);
  generateParameterForSet(PROPERTY_PREFIX,true, introspectedTable.getAllColumns(), parent);
  parent.addElement(checkArrayWhere(introspectedTable));
  generateTextBlock(" if @@ROWCOUNT = 0 ",parent);
  generateTextBlockAppendTableName(" insert into ", introspectedTable,parent);
  generateActualColumnNamesWithParenthesis(PROPERTY_PREFIX,true,introspectedTable.getAllColumns(), parent);
  generateTextBlock(" values ", parent);
  generateParametersSeparateByCommaWithParenthesis(PROPERTY_PREFIX,true, introspectedTable.getAllColumns(), parent);
}
 
Example 12
Source File: BaseColumnListElementGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    XmlElement answer = new XmlElement("sql"); //$NON-NLS-1$

    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
            introspectedTable.getBaseColumnListId()));

    context.getCommentGenerator().addComment(answer);

    StringBuilder sb = new StringBuilder();
    Iterator<IntrospectedColumn> iter = introspectedTable
            .getNonBLOBColumns().iterator();
    while (iter.hasNext()) {
        sb.append(MyBatis3FormattingUtilities.getSelectListPhrase(iter
                .next()));

        if (iter.hasNext()) {
            sb.append(", "); //$NON-NLS-1$
        }

        if (sb.length() > 80) {
            answer.addElement(new TextElement(sb.toString()));
            sb.setLength(0);
        }
    }

    if (sb.length() > 0) {
        answer.addElement((new TextElement(sb.toString())));
    }

    if (context.getPlugins().sqlMapBaseColumnListElementGenerated(
            answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example 13
Source File: BlobColumnListElementGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    XmlElement answer = new XmlElement("sql"); //$NON-NLS-1$

    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
            introspectedTable.getBlobColumnListId()));

    context.getCommentGenerator().addComment(answer);

    StringBuilder sb = new StringBuilder();

    Iterator<IntrospectedColumn> iter = introspectedTable.getBLOBColumns()
            .iterator();
    while (iter.hasNext()) {
        sb.append(Ibatis2FormattingUtilities.getSelectListPhrase(iter
                .next()));

        if (iter.hasNext()) {
            sb.append(", "); //$NON-NLS-1$
        }

        if (sb.length() > 80) {
            answer.addElement(new TextElement(sb.toString()));
            sb.setLength(0);
        }
    }

    if (sb.length() > 0) {
        answer.addElement((new TextElement(sb.toString())));
    }

    if (context.getPlugins().sqlMapBlobColumnListElementGenerated(
            answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example 14
Source File: SelectPageKeyPlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 5 votes vote down vote up
@Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
//        if (isGeneratePagination(introspectedTable)) {
        XmlElement select = new XmlElement("select");
        select.addAttribute(new Attribute("id", "selectByPage"));
        select.addAttribute(new Attribute("resultMap", "BaseResultMap"));
        select.addElement(new TextElement("SELECT <include refid=\"Base_Column_List\" /> FROM " + introspectedTable.getFullyQualifiedTableNameAtRuntime() + " LIMIT #{offset},#{limit}"));
        XmlElement parentElement = document.getRootElement();
        parentElement.addElement(select);
        return super.sqlMapDocumentGenerated(document, introspectedTable);
//        }

//        return super.sqlMapDocumentGenerated(document, introspectedTable);
    }
 
Example 15
Source File: BaseColumnListElementGenerator.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    XmlElement answer = new XmlElement("sql");

    answer.addAttribute(new Attribute("id",
            introspectedTable.getBaseColumnListId()));

    context.getCommentGenerator().addComment(answer);

    StringBuilder sb = new StringBuilder();
    Iterator<IntrospectedColumn> iter = introspectedTable
            .getNonBLOBColumns().iterator();
    while (iter.hasNext()) {
        sb.append(MyBatis3FormattingUtilities.getSelectListPhrase(iter
                .next()));

        if (iter.hasNext()) {
            sb.append(", ");
        }

        if (sb.length() > 80) {
            answer.addElement(new TextElement(sb.toString()));
            sb.setLength(0);
        }
    }

    if (sb.length() > 0) {
        answer.addElement(new TextElement(sb.toString()));
    }

    if (context.getPlugins().sqlMapBaseColumnListElementGenerated(
            answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example 16
Source File: PropertyHolder.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
protected void addPropertyXmlElements(XmlElement xmlElement) {
    Enumeration<?> enumeration = properties.propertyNames();
    while (enumeration.hasMoreElements()) {
        String propertyName = (String) enumeration.nextElement();

        XmlElement propertyElement = new XmlElement("property");
        propertyElement.addAttribute(new Attribute("name", propertyName));
        propertyElement.addAttribute(new Attribute(
                "value", properties.getProperty(propertyName)));
        xmlElement.addElement(propertyElement);
    }
}
 
Example 17
Source File: ExampleWhereClauseElementGenerator.java    From mapper-generator-javafx with Apache License 2.0 4 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    XmlElement answer = new XmlElement("sql");

    if (isForUpdateByExample) {
        answer
                .addAttribute(new Attribute(
                        "id", introspectedTable.getMyBatis3UpdateByExampleWhereClauseId()));
    } else {
        answer.addAttribute(new Attribute(
                "id", introspectedTable.getExampleWhereClauseId()));
    }

    context.getCommentGenerator().addComment(answer);

    XmlElement whereElement = new XmlElement("where");
    answer.addElement(whereElement);

    XmlElement outerForEachElement = new XmlElement("foreach");
    if (isForUpdateByExample) {
        outerForEachElement.addAttribute(new Attribute(
                "collection", "example.oredCriteria")); //$NON-NLS-2$
    } else {
        outerForEachElement.addAttribute(new Attribute(
                "collection", "oredCriteria")); //$NON-NLS-2$
    }
    outerForEachElement.addAttribute(new Attribute("item", "criteria")); //$NON-NLS-2$
    outerForEachElement.addAttribute(new Attribute("separator", "or")); //$NON-NLS-2$
    whereElement.addElement(outerForEachElement);

    XmlElement ifElement = new XmlElement("if");
    ifElement.addAttribute(new Attribute("test", "criteria.valid")); //$NON-NLS-2$
    outerForEachElement.addElement(ifElement);

    XmlElement trimElement = new XmlElement("trim");
    trimElement.addAttribute(new Attribute("prefix", "(")); //$NON-NLS-2$
    trimElement.addAttribute(new Attribute("suffix", ")")); //$NON-NLS-2$
    trimElement.addAttribute(new Attribute("prefixOverrides", "and")); //$NON-NLS-2$

    ifElement.addElement(trimElement);

    trimElement.addElement(getMiddleForEachElement(null));

    for (IntrospectedColumn introspectedColumn : introspectedTable
            .getNonBLOBColumns()) {
        if (stringHasValue(introspectedColumn
                .getTypeHandler())) {
            trimElement
                    .addElement(getMiddleForEachElement(introspectedColumn));
        }
    }

    if (context.getPlugins()
            .sqlMapExampleWhereClauseElementGenerated(answer,
                    introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example 18
Source File: ResultMapWithBLOBsElementGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    boolean useColumnIndex = isTrue(introspectedTable
                    .getTableConfigurationProperty(PropertyRegistry.TABLE_USE_COLUMN_INDEXES));

    XmlElement answer = new XmlElement("resultMap"); //$NON-NLS-1$

    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
            introspectedTable.getResultMapWithBLOBsId()));

    String returnType;
    if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
        returnType = introspectedTable.getRecordWithBLOBsType();
    } else {
        // table has BLOBs, but no BLOB class - BLOB fields must be
        // in the base class
        returnType = introspectedTable.getBaseRecordType();
    }

    answer.addAttribute(new Attribute("class", //$NON-NLS-1$
            returnType));

    StringBuilder sb = new StringBuilder();
    sb.append(introspectedTable.getIbatis2SqlMapNamespace());
    sb.append('.');
    sb.append(introspectedTable.getBaseResultMapId());
    answer.addAttribute(new Attribute("extends", sb.toString())); //$NON-NLS-1$

    context.getCommentGenerator().addComment(answer);

    int i = introspectedTable.getNonBLOBColumnCount() + 1;
    if (stringHasValue(introspectedTable
            .getSelectByPrimaryKeyQueryId())
            || stringHasValue(introspectedTable
                    .getSelectByExampleQueryId())) {
        i++;
    }

    for (IntrospectedColumn introspectedColumn : introspectedTable
            .getBLOBColumns()) {
        XmlElement resultElement = new XmlElement("result"); //$NON-NLS-1$

        if (useColumnIndex) {
            resultElement.addAttribute(new Attribute(
                    "columnIndex", Integer.toString(i++))); //$NON-NLS-1$
        } else {
            resultElement
                    .addAttribute(new Attribute(
                            "column", Ibatis2FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn))); //$NON-NLS-1$
        }
        resultElement.addAttribute(new Attribute(
                "property", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
        resultElement.addAttribute(new Attribute(
                "jdbcType", introspectedColumn.getJdbcTypeName())); //$NON-NLS-1$

        if (stringHasValue(introspectedColumn
                .getTypeHandler())) {
            resultElement.addAttribute(new Attribute(
                    "typeHandler", introspectedColumn.getTypeHandler())); //$NON-NLS-1$
        }

        answer.addElement(resultElement);
    }

    if (context.getPlugins()
            .sqlMapResultMapWithBLOBsElementGenerated(answer,
                    introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example 19
Source File: DeleteByPrimaryKeyElementGenerator.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    XmlElement answer = new XmlElement("delete"); //$NON-NLS-1$

    answer.addAttribute(new Attribute(
            "id", introspectedTable.getDeleteByPrimaryKeyStatementId())); //$NON-NLS-1$
    String parameterClass;
    if (introspectedTable.getRules().generatePrimaryKeyClass()) {
        parameterClass = introspectedTable.getPrimaryKeyType();
    } else {
        parameterClass = introspectedTable.getBaseRecordType();
    }
    answer.addAttribute(new Attribute("parameterClass", //$NON-NLS-1$
            parameterClass));

    context.getCommentGenerator().addComment(answer);

    StringBuilder sb = new StringBuilder();
    sb.append("delete from "); //$NON-NLS-1$
    sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));

    boolean and = false;
    for (IntrospectedColumn introspectedColumn : introspectedTable
            .getPrimaryKeyColumns()) {
        sb.setLength(0);
        if (and) {
            sb.append("  and "); //$NON-NLS-1$
        } else {
            sb.append("where "); //$NON-NLS-1$
            and = true;
        }

        sb.append(Ibatis2FormattingUtilities
                .getEscapedColumnName(introspectedColumn));
        sb.append(" = "); //$NON-NLS-1$
        sb.append(Ibatis2FormattingUtilities
                .getParameterClause(introspectedColumn));
        answer.addElement(new TextElement(sb.toString()));
    }

    if (context.getPlugins()
            .sqlMapDeleteByPrimaryKeyElementGenerated(answer,
                    introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example 20
Source File: SelectByPrimaryKeyElementGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    XmlElement answer = new XmlElement("select"); //$NON-NLS-1$

    answer.addAttribute(new Attribute(
            "id", introspectedTable.getSelectByPrimaryKeyStatementId())); //$NON-NLS-1$
    if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
        answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
                introspectedTable.getResultMapWithBLOBsId()));
    } else {
        answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
                introspectedTable.getBaseResultMapId()));
    }

    String parameterType;
    if (introspectedTable.getRules().generatePrimaryKeyClass()) {
        parameterType = introspectedTable.getPrimaryKeyType();
    } else {
        // select by primary key, but no primary key class. Fields
        // must be in the base record
        parameterType = introspectedTable.getBaseRecordType();
    }

    answer.addAttribute(new Attribute("parameterClass", //$NON-NLS-1$
            parameterType));

    context.getCommentGenerator().addComment(answer);

    StringBuilder sb = new StringBuilder();
    sb.append("select "); //$NON-NLS-1$

    if (stringHasValue(introspectedTable
            .getSelectByPrimaryKeyQueryId())) {
        sb.append('\'');
        sb.append(introspectedTable.getSelectByPrimaryKeyQueryId());
        sb.append("' as QUERYID,"); //$NON-NLS-1$
    }
    answer.addElement(new TextElement(sb.toString()));
    answer.addElement(getBaseColumnListElement());
    if (introspectedTable.hasBLOBColumns()) {
        answer.addElement(new TextElement(",")); //$NON-NLS-1$
        answer.addElement(getBlobColumnListElement());
    }

    sb.setLength(0);
    sb.append("from "); //$NON-NLS-1$
    sb.append(introspectedTable
            .getAliasedFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));

    boolean and = false;
    for (IntrospectedColumn introspectedColumn : introspectedTable
            .getPrimaryKeyColumns()) {
        sb.setLength(0);
        if (and) {
            sb.append("  and "); //$NON-NLS-1$
        } else {
            sb.append("where "); //$NON-NLS-1$
            and = true;
        }

        sb.append(Ibatis2FormattingUtilities
                .getAliasedEscapedColumnName(introspectedColumn));
        sb.append(" = "); //$NON-NLS-1$
        sb.append(Ibatis2FormattingUtilities
                .getParameterClause(introspectedColumn));
        answer.addElement(new TextElement(sb.toString()));
    }

    if (context.getPlugins()
            .sqlMapSelectByPrimaryKeyElementGenerated(answer,
                    introspectedTable)) {
        parentElement.addElement(answer);
    }
}