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

The following examples show how to use org.mybatis.generator.api.dom.xml.XmlElement#addAttribute() . 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: AbstractXmbgPlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
protected void generateParametersSeparateByComma(String fieldPrefix, boolean ifNullCheck, boolean withParenthesis, List<IntrospectedColumn> columns, XmlElement parent) {
  XmlElement trimElement = new XmlElement("trim");
  trimElement.addAttribute(new Attribute("suffixOverrides", ","));
  if (withParenthesis) {
    trimElement.addAttribute(new Attribute("prefix", "("));
    trimElement.addAttribute(new Attribute("suffix", ")"));
  }

  StringBuilder sb = new StringBuilder();
  for (IntrospectedColumn introspectedColumn : columns) {
    sb.setLength(0);
    sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, fieldPrefix));
    sb.append(",");

    doIfNullCheck(fieldPrefix, ifNullCheck, trimElement, sb, introspectedColumn);
  }
  parent.addElement(trimElement);
}
 
Example 2
Source File: CountByExampleElementGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
@Override
public void addElements(XmlElement parentElement) {
    XmlElement answer = new XmlElement("select"); //$NON-NLS-1$

    String fqjt = introspectedTable.getExampleType();

    answer.addAttribute(new Attribute(
            "id", introspectedTable.getCountByExampleStatementId())); //$NON-NLS-1$
    answer.addAttribute(new Attribute("parameterType", fqjt)); //$NON-NLS-1$
    answer.addAttribute(new Attribute("resultType", "java.lang.Integer")); //$NON-NLS-1$ //$NON-NLS-2$

    context.getCommentGenerator().addComment(answer);

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

    if (context.getPlugins().sqlMapCountByExampleElementGenerated(
            answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example 3
Source File: SimpleXMLMapperGenerator.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
protected XmlElement getSqlMapElement() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString("Progress.12", table.toString()));
    XmlElement answer = new XmlElement("mapper");
    String namespace = introspectedTable.getMyBatis3SqlMapNamespace();
    answer.addAttribute(new Attribute("namespace",
            namespace));

    context.getCommentGenerator().addRootComment(answer);

    addResultMapElement(answer);
    addDeleteByPrimaryKeyElement(answer);
    addInsertElement(answer);
    addUpdateByPrimaryKeyElement(answer);
    addSelectByPrimaryKeyElement(answer);
    addSelectAllElement(answer);

    return answer;
}
 
Example 4
Source File: DB2UpsertPlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
protected void generateCopyForSetByPrefix(String fieldPrefix, String leftPrefix, String rightPrefix, boolean ifNullCheck, IntrospectedTable introspectedTable, XmlElement dynamicElement) {
  XmlElement trimElement = new XmlElement("trim");
  trimElement.addAttribute(new Attribute("suffixOverrides", ","));

  StringBuilder sb = new StringBuilder();
  for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {

    sb.setLength(0);
    String columnName = MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn);
    sb.append(leftPrefix + columnName);
    sb.append(" = ");
    sb.append(rightPrefix + columnName);
    sb.append(',');

    doIfNullCheck(fieldPrefix, ifNullCheck, trimElement, sb, introspectedColumn);
  }

  dynamicElement.addElement(trimElement);
}
 
Example 5
Source File: AbstractXmlElementGenerator.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
/**
 * This method should return an XmlElement for the select key used to
 * automatically generate keys.
 * 
 * @param introspectedColumn
 *            the column related to the select key statement
 * @param generatedKey
 *            the generated key for the current table
 * @return the selectKey element
 */
protected XmlElement getSelectKey(IntrospectedColumn introspectedColumn,
        GeneratedKey generatedKey) {
    String identityColumnType = introspectedColumn
            .getFullyQualifiedJavaType().getFullyQualifiedName();

    XmlElement answer = new XmlElement("selectKey"); //$NON-NLS-1$
    answer.addAttribute(new Attribute("resultType", identityColumnType)); //$NON-NLS-1$
    answer.addAttribute(new Attribute(
            "keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
    answer.addAttribute(new Attribute("order", //$NON-NLS-1$
            generatedKey.getMyBatis3Order())); 
    
    answer.addElement(new TextElement(generatedKey
                    .getRuntimeSqlStatement()));

    return answer;
}
 
Example 6
Source File: HsqldbUpsertPlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
protected void generateCopyForSetByPrefix(String fieldPrefix, String leftPrefix, String rightPrefix, boolean ifNullCheck, IntrospectedTable introspectedTable, XmlElement dynamicElement) {
  XmlElement trimElement = new XmlElement("trim");
  trimElement.addAttribute(new Attribute("suffixOverrides", ","));

  StringBuilder sb = new StringBuilder();
  for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {

    sb.setLength(0);
    String columnName = MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn);
    sb.append(leftPrefix + columnName);
    sb.append(" = ");
    sb.append(rightPrefix + columnName);
    sb.append(',');

    doIfNullCheck(fieldPrefix, ifNullCheck, trimElement, sb, introspectedColumn);
  }

  dynamicElement.addElement(trimElement);
}
 
Example 7
Source File: PaginationPlugin.java    From maven-archetype with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * sqlMapSelectByExampleWithoutBLOBsElementGenerated: 写入xml文件. <br/>
 *
 * @author Hongbin Yuan
 * @param element
 * @param introspectedTable
 * @return
 * @see PluginAdapter#sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement, IntrospectedTable)
 */
@Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(
        XmlElement element, IntrospectedTable introspectedTable) {
    // 向mapper xml中添加limit的start字句
    XmlElement limitStartElement = new XmlElement("if"); //$NON-NLS-1$
    limitStartElement.addAttribute(new Attribute("test", "start &gt;= 0 ")); //$NON-NLS-1$ //$NON-NLS-2$
    limitStartElement.addElement(new TextElement("limit ${start}"));
    element.addElement(limitStartElement);

    // 向mapper xml中添加limit的count字句
    XmlElement limitEndElement = new XmlElement("if"); //$NON-NLS-1$
    limitEndElement.addAttribute(new Attribute("test", "count &gt;= 0 ")); //$NON-NLS-1$ //$NON-NLS-2$
    limitEndElement.addElement(new TextElement(",${count}"));
    element.addElement(limitEndElement);

    return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element,
            introspectedTable);
}
 
Example 8
Source File: AbstractXmbgPlugin.java    From dolphin with Apache License 2.0 6 votes vote down vote up
protected void generateParameterForSet(String fieldPrefix, boolean ifNullCheck, List<IntrospectedColumn> columns, XmlElement dynamicElement) {
  XmlElement trimElement = new XmlElement("trim");
  trimElement.addAttribute(new Attribute("suffixOverrides", ","));

  StringBuilder sb = new StringBuilder();
  for (IntrospectedColumn introspectedColumn : columns) {
    sb.setLength(0);
    sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn));
    sb.append(" = ");
    sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, fieldPrefix));
    sb.append(',');

    doIfNullCheck(fieldPrefix, ifNullCheck, trimElement, sb, introspectedColumn);
  }

  dynamicElement.addElement(trimElement);
}
 
Example 9
Source File: AbstractUpsertPlugin.java    From dolphin with Apache License 2.0 5 votes vote down vote up
/**
 * add update xml element to mapper.xml for upsert
 *
 * @param document          The generated xml mapper dom
 * @param introspectedTable The metadata for database table
 */
protected void addSingleUpsertToSqlMap(Document document, IntrospectedTable introspectedTable) {
  XmlElement update = new XmlElement("update");
  update.addAttribute(new Attribute("id", UPSERT));
  update.addAttribute(new Attribute("parameterType", "map"));

  generateSqlMapContent(introspectedTable, update);

  document.getRootElement().addElement(update);
}
 
Example 10
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 11
Source File: AbstractXmlElementGenerator.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
protected XmlElement getExampleIncludeElement() {
    XmlElement ifElement = new XmlElement("if"); //$NON-NLS-1$
    ifElement.addAttribute(new Attribute("test", "_parameter != null")); //$NON-NLS-1$ //$NON-NLS-2$

    XmlElement includeElement = new XmlElement("include"); //$NON-NLS-1$
    includeElement.addAttribute(new Attribute("refid", //$NON-NLS-1$
            introspectedTable.getExampleWhereClauseId()));
    ifElement.addElement(includeElement);

    return ifElement;
}
 
Example 12
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 13
Source File: GenPlugin.java    From scaffold-cloud with MIT License 5 votes vote down vote up
/**
 * 查询个数
 *
 * @param id
 * @param tableName
 * @param pkColumn
 * @return
 */
private XmlElement createCount(String id, String tableName, IntrospectedColumn pkColumn) {
    XmlElement select = new XmlElement("select");
    select.addAttribute(new Attribute("id", id));
    select.addAttribute(new Attribute("resultType", "java.lang.Integer"));

    StringBuilder selectStr = new StringBuilder("select count(1) from ").append(tableName);
    if (null != pkColumn) {
        selectStr.append(" where ").append(pkColumn.getActualColumnName()).append(" = #{").append(pkColumn.getJavaProperty()).append("}");
    } else {
        selectStr.append(" <include refid=\"sql_where\" />");
    }
    select.addElement(new TextElement(selectStr.toString()));
    return select;
}
 
Example 14
Source File: MySQLPageLimitPlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(
        XmlElement element, IntrospectedTable introspectedTable) {
    XmlElement isNotNullElement = new XmlElement("isNotNull");
    isNotNullElement.addAttribute(new Attribute("property", "limitClauseStart"));
    isNotNullElement.addElement(new TextElement(
            " limit #limitClauseStart:INTEGER#, #limitClauseCount:INTEGER# "));
    element.getElements().add(element.getElements().size(), isNotNullElement);

    return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element,
            introspectedTable);
}
 
Example 15
Source File: SqlMapGeneratorConfiguration.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
public XmlElement toXmlElement() {
    XmlElement answer = new XmlElement("sqlMapGenerator"); //$NON-NLS-1$

    if (targetPackage != null) {
        answer.addAttribute(new Attribute("targetPackage", targetPackage)); //$NON-NLS-1$
    }

    if (targetProject != null) {
        answer.addAttribute(new Attribute("targetProject", targetProject)); //$NON-NLS-1$
    }

    addPropertyXmlElements(answer);

    return answer;
}
 
Example 16
Source File: PluginConfiguration.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
public XmlElement toXmlElement() {
    XmlElement answer = new XmlElement("plugin"); //$NON-NLS-1$
    if (getConfigurationType() != null) {
        answer.addAttribute(new Attribute("type", getConfigurationType())); //$NON-NLS-1$
    }

    addPropertyXmlElements(answer);

    return answer;
}
 
Example 17
Source File: ResultMapWithoutBLOBsElementGenerator.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("resultMap"); //$NON-NLS-1$
    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
            introspectedTable.getBaseResultMapId()));

    String returnType;
    if (isSimple) {
        returnType = introspectedTable.getBaseRecordType();
    } else {
        if (introspectedTable.getRules().generateBaseRecordClass()) {
            returnType = introspectedTable.getBaseRecordType();
        } else {
            returnType = introspectedTable.getPrimaryKeyType();
        }
    }

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

    context.getCommentGenerator().addComment(answer);

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

    if (context.getPlugins().sqlMapResultMapWithoutBLOBsElementGenerated(
            answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example 18
Source File: ColumnOverride.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
public XmlElement toXmlElement() {
    XmlElement xmlElement = new XmlElement("columnOverride"); //$NON-NLS-1$
    xmlElement.addAttribute(new Attribute("column", columnName)); //$NON-NLS-1$

    if (stringHasValue(javaProperty)) {
        xmlElement.addAttribute(new Attribute("property", javaProperty)); //$NON-NLS-1$
    }

    if (stringHasValue(javaType)) {
        xmlElement.addAttribute(new Attribute("javaType", javaType)); //$NON-NLS-1$
    }

    if (stringHasValue(jdbcType)) {
        xmlElement.addAttribute(new Attribute("jdbcType", jdbcType)); //$NON-NLS-1$
    }

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

    if (stringHasValue(configuredDelimitedColumnName)) {
        xmlElement.addAttribute(new Attribute(
                "delimitedColumnName", configuredDelimitedColumnName)); //$NON-NLS-1$
    }

    addPropertyXmlElements(xmlElement);

    return xmlElement;
}
 
Example 19
Source File: BatchDeletePlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 4 votes vote down vote up
/**
 * 批量删除的xml方法生成
 * @param document
 * @param introspectedTable
 */
private void addSqlMapper(Document document, IntrospectedTable introspectedTable){
    String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();

    String key = introspectedTable.getPrimaryKeyColumns().get(0).getActualColumnName();

    String baseSql = String.format("delete from %s where %s in (",tableName,key);

    FullyQualifiedJavaType paramType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType();

    XmlElement deleteElement = SqlMapperGeneratorTool.baseElementGenerator(SqlMapperGeneratorTool.DELETE, BATCH_DELETE,paramType);

    XmlElement foreachElement = SqlMapperGeneratorTool.baseForeachElementGenerator(PARAMETER_NAME,"item","index",null);

    deleteElement.addElement(new TextElement(baseSql));

    foreachElement.addAttribute(new Attribute("separator", ","));

    foreachElement.addElement(new TextElement("#{item}"));

    deleteElement.addElement(foreachElement);

    deleteElement.addElement(new TextElement(")"));

    document.getRootElement().addElement(deleteElement);
}
 
Example 20
Source File: AbstractXmlElementGenerator.java    From mapper-generator-javafx with Apache License 2.0 4 votes vote down vote up
protected XmlElement getBaseColumnListElement() {
    XmlElement answer = new XmlElement("include");
    answer.addAttribute(new Attribute("refid",
            introspectedTable.getBaseColumnListId()));
    return answer;
}