org.mybatis.generator.api.dom.xml.TextElement Java Examples

The following examples show how to use org.mybatis.generator.api.dom.xml.TextElement. 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: IncrementPlugin.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 有Selective代码生成
 * @param element
 */
private void generatedWithSelective(XmlElement element, IntrospectedTable introspectedTable, boolean hasPrefix) {
    if (this.support()) {
        // 查找 set->if->text
        List<XmlElement> sets = XmlElementTools.findXmlElements(element, "set");
        if (sets.size() > 0) {
            List<XmlElement> ifs = XmlElementTools.findXmlElements(sets.get(0), "if");
            if (ifs.size() > 0) {
                for (XmlElement xmlElement : ifs) {
                    // 下面为if的text节点
                    List<Element> textEles = xmlElement.getElements();
                    TextElement textEle = (TextElement) textEles.get(0);
                    String[] strs = textEle.getContent().split("=");
                    String columnName = strs[0].trim();
                    IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
                    if (this.supportIncrement(introspectedColumn)) {
                        XmlElementTools.replaceXmlElement(xmlElement, PluginTools.getHook(IIncrementPluginHook.class).generateIncrementSetSelective(introspectedColumn, hasPrefix ? "record." : null));
                    }
                }
            }
        }
    }
}
 
Example #2
Source File: PostGreSQLReturnKeyPlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 6 votes vote down vote up
/**
 * 返回主键方法   ==> 一般sql如下面
 * <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
 *       SELECT currval('t_hui_order_id_seq')
 * </selectKey>
 * @param introspectedTable
 * @return
 */
private XmlElement addSelectKey(IntrospectedTable introspectedTable){
    String resultType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType().getFullyQualifiedName();
    String keyProperty = introspectedTable.getPrimaryKeyColumns().get(0).getActualColumnName();

    String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();
    String sql = "SELECT currval('" + tableName + "_id_seq')";

    XmlElement selectKey = new XmlElement("selectKey");
    TextElement selectKeySQL = new TextElement(sql);
    selectKey.addAttribute(new Attribute("keyProperty",keyProperty));
    selectKey.addAttribute(new Attribute("order","AFTER"));
    selectKey.addAttribute(new Attribute("resultType", resultType));
    selectKey.addElement(selectKeySQL);
    return selectKey;
}
 
Example #3
Source File: PaginationPlugin.java    From xxpay-master with MIT License 6 votes vote down vote up
/**
 * 为Mapper.xml的selectByExample添加limit,offset
 */
@Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
                                                                 IntrospectedTable introspectedTable) {

	XmlElement ifLimitNotNullElement = new XmlElement("if");
	ifLimitNotNullElement.addAttribute(new Attribute("test", "limit != null"));

	XmlElement ifOffsetNotNullElement = new XmlElement("if");
	ifOffsetNotNullElement.addAttribute(new Attribute("test", "offset != null"));
	ifOffsetNotNullElement.addElement(new TextElement("limit ${offset}, ${limit}"));
	ifLimitNotNullElement.addElement(ifOffsetNotNullElement);

	XmlElement ifOffsetNullElement = new XmlElement("if");
	ifOffsetNullElement.addAttribute(new Attribute("test", "offset == null"));
	ifOffsetNullElement.addElement(new TextElement("limit ${limit}"));
	ifLimitNotNullElement.addElement(ifOffsetNullElement);

	element.addElement(ifLimitNotNullElement);

	return true;
}
 
Example #4
Source File: PaginationPlugin.java    From xxpay-master with MIT License 6 votes vote down vote up
/**
 * 为Mapper.xml的selectByExample添加limit,offset
 */
@Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
                                                                 IntrospectedTable introspectedTable) {

	XmlElement ifLimitNotNullElement = new XmlElement("if");
	ifLimitNotNullElement.addAttribute(new Attribute("test", "limit != null"));

	XmlElement ifOffsetNotNullElement = new XmlElement("if");
	ifOffsetNotNullElement.addAttribute(new Attribute("test", "offset != null"));
	ifOffsetNotNullElement.addElement(new TextElement("limit ${offset}, ${limit}"));
	ifLimitNotNullElement.addElement(ifOffsetNotNullElement);

	XmlElement ifOffsetNullElement = new XmlElement("if");
	ifOffsetNullElement.addAttribute(new Attribute("test", "offset == null"));
	ifOffsetNullElement.addElement(new TextElement("limit ${limit}"));
	ifLimitNotNullElement.addElement(ifOffsetNullElement);

	element.addElement(ifLimitNotNullElement);

	return true;
}
 
Example #5
Source File: MySQLLimitPlugin.java    From ApiManager with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 为Mapper.xml的selectByExample添加limit
 */
@Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
        IntrospectedTable introspectedTable) {

    XmlElement choose = new XmlElement("choose");
    XmlElement rangeLimitWhen = new XmlElement("when");
    rangeLimitWhen.addAttribute(new Attribute("test", "limitStart != null and limitStart != -1 and maxResults != null and maxResults != -1"));
    rangeLimitWhen.addElement(new TextElement("limit ${limitStart} , ${maxResults}"));
    XmlElement limitStartWhen = new XmlElement("when");
    limitStartWhen.addAttribute(new Attribute("test", "limitStart != null and limitStart != -1"));
    limitStartWhen.addElement(new TextElement("limit ${limitStart}"));
    choose.addElement(rangeLimitWhen);
    choose.addElement(limitStartWhen);
    element.addElement(choose);
    return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
}
 
Example #6
Source File: IncrementsPlugin.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 有Selective代码生成
 * @param element
 */
private void generatedWithSelective(XmlElement element, IntrospectedTable introspectedTable, boolean hasPrefix) {
    if (this.support()) {
        // 查找 set->if->text
        List<XmlElement> sets = XmlElementTools.findXmlElements(element, "set");
        if (sets.size() > 0) {
            List<XmlElement> ifs = XmlElementTools.findXmlElements(sets.get(0), "if");
            if (ifs.size() > 0) {
                for (XmlElement xmlElement : ifs) {
                    // 下面为if的text节点
                    List<Element> textEles = xmlElement.getElements();
                    TextElement textEle = (TextElement) textEles.get(0);
                    String[] strs = textEle.getContent().split("=");
                    String columnName = strs[0].trim();
                    IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
                    // 查找是否需要进行增量操作
                    List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementSetElementGenerated(introspectedColumn, hasPrefix ? "record." : null, true);
                    if (!incrementEles.isEmpty()) {
                        xmlElement.getElements().clear();
                        xmlElement.getElements().addAll(incrementEles);
                    }
                }
            }
        }
    }
}
 
Example #7
Source File: CountByExampleElementGenerator.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("select");

    String fqjt = introspectedTable.getExampleType();

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

    context.getCommentGenerator().addComment(answer);

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

    if (context.getPlugins().sqlMapCountByExampleElementGenerated(
            answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example #8
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 #9
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 #10
Source File: DefaultCommentGenerator.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a suitable comment to warn users that the element was generated, and when it was generated.
 *
 * @param xmlElement
 *            the xml element
 */
@Override
public void addComment(XmlElement xmlElement) {
    if (suppressAllComments) {
        return;
    }

    xmlElement.addElement(new TextElement("<!--"));

    StringBuilder sb = new StringBuilder();
    sb.append("  WARNING - ");
    xmlElement.addElement(new TextElement(sb.toString()));
    xmlElement.addElement(new TextElement(
            "  This element is automatically generated by MyBatis Generator, do not modify."));

    String s = getDateString();
    if (s != null) {
        sb.setLength(0);
        sb.append("  This element was generated on ");
        sb.append(s);
        sb.append('.');
        xmlElement.addElement(new TextElement(sb.toString()));
    }

    xmlElement.addElement(new TextElement("-->"));
}
 
Example #11
Source File: LimitPlugin.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 生成limit节点
 * @param element
 */
private void generateLimitElementWithExample(XmlElement element) {
    XmlElement ifLimitNotNullElement = new XmlElement("if");
    ifLimitNotNullElement.addAttribute(new Attribute("test", "example != null and example.rows != null"));

    XmlElement ifOffsetNotNullElement = new XmlElement("if");
    ifOffsetNotNullElement.addAttribute(new Attribute("test", "example.offset != null"));
    ifOffsetNotNullElement.addElement(new TextElement("limit ${example.offset}, ${example.rows}"));
    ifLimitNotNullElement.addElement(ifOffsetNotNullElement);

    XmlElement ifOffsetNullElement = new XmlElement("if");
    ifOffsetNullElement.addAttribute(new Attribute("test", "example.offset == null"));
    ifOffsetNullElement.addElement(new TextElement("limit ${example.rows}"));
    ifLimitNotNullElement.addElement(ifOffsetNullElement);

    element.addElement(ifLimitNotNullElement);
}
 
Example #12
Source File: SelectiveEnhancedPlugin.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * insert column selective
 * @param columns
 * @param bracket
 * @return
 */
private XmlElement generateInsertValuesSelective(List<IntrospectedColumn> columns, boolean bracket) {
    XmlElement insertValuesChooseEle = new XmlElement("choose");

    XmlElement valuesWhenEle = new XmlElement("when");
    valuesWhenEle.addAttribute(new Attribute("test", "selective != null and selective.length > 0"));
    insertValuesChooseEle.addElement(valuesWhenEle);

    XmlElement valuesForeachEle = new XmlElement("foreach");
    valuesForeachEle.addAttribute(new Attribute("collection", "selective"));
    valuesForeachEle.addAttribute(new Attribute("item", "column"));
    valuesForeachEle.addAttribute(new Attribute("separator", ","));
    if (bracket) {
        valuesForeachEle.addAttribute(new Attribute("open", "("));
        valuesForeachEle.addAttribute(new Attribute("close", ")"));
    }
    valuesForeachEle.addElement(new TextElement("#{record.${column.javaProperty},jdbcType=${column.jdbcType}}"));
    valuesWhenEle.addElement(valuesForeachEle);

    XmlElement valuesOtherwiseEle = new XmlElement("otherwise");
    insertValuesChooseEle.addElement(valuesOtherwiseEle);
    valuesOtherwiseEle.addElement(XmlElementGeneratorTools.generateValuesSelective(columns, "record.", bracket));

    return insertValuesChooseEle;
}
 
Example #13
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 #14
Source File: AbstractXmlElementGenerator.java    From mybatis-generator-core-fix 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("resultClass", identityColumnType)); //$NON-NLS-1$
    answer.addAttribute(new Attribute(
            "keyProperty", introspectedColumn.getJavaProperty())); //$NON-NLS-1$
    if (stringHasValue(generatedKey.getType())) {
        answer.addAttribute(new Attribute("type", generatedKey.getType())); //$NON-NLS-1$  
    }
    answer
            .addElement(new TextElement(generatedKey
                    .getRuntimeSqlStatement()));

    return answer;
}
 
Example #15
Source File: LimitPlugin.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 生成limit节点
 * @param element
 */
private void generateLimitElement(XmlElement element) {
    XmlElement ifLimitNotNullElement = new XmlElement("if");
    ifLimitNotNullElement.addAttribute(new Attribute("test", "rows != null"));

    XmlElement ifOffsetNotNullElement = new XmlElement("if");
    ifOffsetNotNullElement.addAttribute(new Attribute("test", "offset != null"));
    ifOffsetNotNullElement.addElement(new TextElement("limit ${offset}, ${rows}"));
    ifLimitNotNullElement.addElement(ifOffsetNotNullElement);

    XmlElement ifOffsetNullElement = new XmlElement("if");
    ifOffsetNullElement.addAttribute(new Attribute("test", "offset == null"));
    ifOffsetNullElement.addElement(new TextElement("limit ${rows}"));
    ifLimitNotNullElement.addElement(ifOffsetNullElement);

    element.addElement(ifLimitNotNullElement);
}
 
Example #16
Source File: IncrementPlugin.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 无Selective代码生成
 * @param xmlElement
 * @param introspectedTable
 * @param hasPrefix
 */
private void generatedWithoutSelective(XmlElement xmlElement, IntrospectedTable introspectedTable, boolean hasPrefix) {
    for (int i = 0; i < xmlElement.getElements().size(); i++) {
        Element ele = xmlElement.getElements().get(i);
        // 找到text节点且格式为 set xx = xx 或者 xx = xx
        if (ele instanceof TextElement) {
            String text = ((TextElement) ele).getContent().trim();
            if (text.matches("(^set\\s)?\\S+\\s?=.*")) {
                // 清理 set 操作
                text = text.replaceFirst("^set\\s", "").trim();
                String columnName = text.split("=")[0].trim();
                IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
                // 查找判断是否需要进行节点替换
                if (this.supportIncrement(introspectedColumn)) {
                    xmlElement.getElements().set(i, PluginTools.getHook(IIncrementPluginHook.class).generateIncrementSet(introspectedColumn, hasPrefix ? "record." : null, text.endsWith(",")));
                }
            }
        }
    }
}
 
Example #17
Source File: OracelPageLimitPlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(
        XmlElement element, IntrospectedTable introspectedTable) {
    XmlElement isStart = new XmlElement("if");
    isStart.addAttribute(new Attribute("test", "limitClauseStart != null and limitClauseStart >= 0"));
    isStart.addElement(new TextElement(
            "select * from (select t_1.*,rownum as row_num from ("));
    element.getElements().add(0, isStart);

    XmlElement isNotNullElement = new XmlElement("if");
    isNotNullElement.addAttribute(new Attribute("test", "limitClauseStart != null and limitClauseStart >= 0"));
    isNotNullElement.addElement(new TextElement(
            " <![CDATA[ ) t_1 where rownum<=#{limitClauseCount,jdbcType=INTEGER} + #{limitClauseStart,jdbcType=INTEGER}) t_2 where t_2.row_num>#{limitClauseStart,jdbcType=INTEGER}]]>"));
    element.getElements().add(element.getElements().size(), isNotNullElement);

    return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element,
            introspectedTable);
}
 
Example #18
Source File: OracelPageLimitPlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 6 votes vote down vote up
@Override
    public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(
            XmlElement element, IntrospectedTable introspectedTable) {
//        XmlElement isParameterPresenteElemen = (XmlElement) element  
//                .getElements().get(element.getElements().size() - 1); 
        XmlElement isStart = new XmlElement("if");
        isStart.addAttribute(new Attribute("test", "limitClauseStart != null and limitClauseStart >= 0"));
        isStart.addElement(new TextElement(
                " select * from (select t_1.*,rownum as row_num from ("));

        element.getElements().add(0, isStart);

        XmlElement isNotNullElement = new XmlElement("if");
        isNotNullElement.addAttribute(new Attribute("test", "limitClauseStart != null and limitClauseStart >= 0"));
        isNotNullElement.addElement(new TextElement(
                " <![CDATA[ ) t_1 where rownum<=#{limitClauseCount,jdbcType=INTEGER} + #{limitClauseStart,jdbcType=INTEGER}) t_2 where t_2.row_num>#{limitClauseStart,jdbcType=INTEGER}]]>"));
        element.getElements().add(element.getElements().size(), isNotNullElement);

        // isParameterPresenteElemen.addElement(isNotNullElement);  
        return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element,
                introspectedTable);
    }
 
Example #19
Source File: BatchInsertPlugin.java    From hui-mybatis-generator-plugins with Apache License 2.0 6 votes vote down vote up
protected XmlElement getSelectKey(IntrospectedColumn introspectedColumn,
                                  GeneratedKey generatedKey) {
    String identityColumnType = introspectedColumn
            .getFullyQualifiedJavaType().getFullyQualifiedName();

    XmlElement answer = new XmlElement("selectKey");
    answer.addAttribute(new Attribute("resultType", identityColumnType));
    answer.addAttribute(new Attribute(
            "keyProperty", introspectedColumn.getJavaProperty()));
    answer.addAttribute(new Attribute("order",
            generatedKey.getMyBatis3Order()));

    answer.addElement(new TextElement(generatedKey
            .getRuntimeSqlStatement()));

    return answer;
}
 
Example #20
Source File: TemplateCommentGenerator.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 添加评论
 * @param xmlElement
 * @param map
 * @param node
 */
private void addXmlElementComment(XmlElement xmlElement, Map<String, Object> map, EnumNode node) {
    if (this.suppressAllComments) {
        return;
    }
    // 获取评论
    String[] comments = getComments(map, node);
    if (comments != null) {
        // 去除空评论
        if (comments.length == 1 && !StringUtility.stringHasValue(comments[0])) {
            return;
        }
        // 添加评论
        for (String comment : comments) {
            xmlElement.addElement(new TextElement(comment));
        }
    }
}
 
Example #21
Source File: FormatTools.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 替换已有注释
 * @param commentGenerator
 * @param element
 */
public static void replaceComment(CommentGenerator commentGenerator, XmlElement element) {
    Iterator<Element> elementIterator = element.getElements().iterator();
    boolean flag = false;
    while (elementIterator.hasNext()) {
        Element ele = elementIterator.next();
        if (ele instanceof TextElement && ((TextElement) ele).getContent().matches(".*<!--.*")) {
            flag = true;
        }

        if (flag) {
            elementIterator.remove();
        }

        if (ele instanceof TextElement && ((TextElement) ele).getContent().matches(".*-->.*")) {
            flag = false;
        }
    }

    XmlElement tmpEle = new XmlElement("tmp");
    commentGenerator.addComment(tmpEle);

    for (int i = tmpEle.getElements().size() - 1; i >= 0; i--) {
        element.addElement(0, tmpEle.getElements().get(i));
    }
}
 
Example #22
Source File: XmlElementGeneratorTools.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * 生成
 * @param element
 * @param introspectedColumn
 * @param prefix
 * @param type               1:key,2:value,3:set
 */
private static void generateSelectiveCommColumnTo(XmlElement element, IntrospectedColumn introspectedColumn, String prefix, int type) {
    switch (type) {
        case 3:
            List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementSetElementGenerated(introspectedColumn, prefix, true);
            if (!incrementEles.isEmpty()) {
                // 增量插件支持
                for (Element ele : incrementEles) {
                    element.addElement(ele);
                }
            } else {
                element.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + " = " + MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
            }
            break;
        case 2:
            element.addElement(new TextElement(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, prefix) + ","));
            break;
        case 1:
            element.addElement(new TextElement(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn) + ","));
            break;
    }
}
 
Example #23
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 #24
Source File: PaginationPlugin.java    From zheng with MIT License 6 votes vote down vote up
/**
 * 为Mapper.xml的selectByExample添加limit,offset
 */
@Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
                                                                 IntrospectedTable introspectedTable) {

	XmlElement ifLimitNotNullElement = new XmlElement("if");
	ifLimitNotNullElement.addAttribute(new Attribute("test", "limit != null"));

	XmlElement ifOffsetNotNullElement = new XmlElement("if");
	ifOffsetNotNullElement.addAttribute(new Attribute("test", "offset != null"));
	ifOffsetNotNullElement.addElement(new TextElement("limit ${offset}, ${limit}"));
	ifLimitNotNullElement.addElement(ifOffsetNotNullElement);

	XmlElement ifOffsetNullElement = new XmlElement("if");
	ifOffsetNullElement.addAttribute(new Attribute("test", "offset == null"));
	ifOffsetNullElement.addElement(new TextElement("limit ${limit}"));
	ifLimitNotNullElement.addElement(ifOffsetNullElement);

	element.addElement(ifLimitNotNullElement);

	return true;
}
 
Example #25
Source File: SelectiveEnhancedPlugin.java    From mybatis-generator-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * updateByPrimaryKeySelective
 * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
 * @param element
 * @param introspectedTable
 * @return
 */
@Override
public boolean sqlMapUpdateByPrimaryKeySelectiveElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
    // 清空
    XmlElement answer = new XmlElement("update");
    answer.addAttribute(new Attribute("id", introspectedTable.getUpdateByPrimaryKeySelectiveStatementId()));
    answer.addAttribute(new Attribute("parameterType", "map"));

    commentGenerator.addComment(answer);

    StringBuilder sb = new StringBuilder();

    sb.append("update ");
    sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));

    // selective
    answer.addElement(new TextElement("SET"));
    answer.addElement(this.generateSetsSelective(ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getNonPrimaryKeyColumns())));

    XmlElementGeneratorTools.generateWhereByPrimaryKeyTo(answer, introspectedTable.getPrimaryKeyColumns(), "record.");

    XmlElementTools.replaceXmlElement(element, answer);
    return super.sqlMapUpdateByPrimaryKeySelectiveElementGenerated(element, introspectedTable);
}
 
Example #26
Source File: CountByExampleElementGenerator.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("select"); //$NON-NLS-1$

    answer.addAttribute(new Attribute(
            "id", introspectedTable.getCountByExampleStatementId())); //$NON-NLS-1$
    answer.addAttribute(new Attribute(
            "parameterClass", introspectedTable.getExampleType())); //$NON-NLS-1$
    answer.addAttribute(new Attribute("resultClass", "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()));

    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().sqlMapCountByExampleElementGenerated(
            answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
 
Example #27
Source File: IncrementsPlugin.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 无Selective代码生成
 * @param xmlElement
 * @param introspectedTable
 * @param hasPrefix
 */
private void generatedWithoutSelective(XmlElement xmlElement, IntrospectedTable introspectedTable, boolean hasPrefix) {
    if (this.support()) {
        List<Element> newEles = new ArrayList<>();
        for (Element ele : xmlElement.getElements()) {
            // 找到text节点且格式为 set xx = xx 或者 xx = xx
            if (ele instanceof TextElement) {
                String text = ((TextElement) ele).getContent().trim();
                if (text.matches("(^set\\s)?\\S+\\s?=.*")) {
                    // 清理 set 操作
                    text = text.replaceFirst("^set\\s", "").trim();
                    String columnName = text.split("=")[0].trim();
                    IntrospectedColumn introspectedColumn = IntrospectedTableTools.safeGetColumn(introspectedTable, columnName);
                    // 查找判断是否需要进行节点替换
                    List<Element> incrementEles = PluginTools.getHook(IIncrementsPluginHook.class).incrementSetElementGenerated(introspectedColumn, hasPrefix ? "record." : null, text.endsWith(","));
                    if (!incrementEles.isEmpty()) {
                        newEles.addAll(incrementEles);

                        continue;
                    }
                }
            }
            newEles.add(ele);
        }

        // 替换节点
        xmlElement.getElements().clear();
        xmlElement.getElements().addAll(newEles);
    }
}
 
Example #28
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 #29
Source File: IncrementPlugin.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 生成增量操作节点(SelectiveEnhancedPlugin)
 * @param columns
 * @return
 * @see SelectiveEnhancedPlugin#generateSetsSelective(List, IntrospectedColumn)
 */
@Override
public List<XmlElement> generateIncrementSetForSelectiveEnhancedPlugin(List<IntrospectedColumn> columns) {
    if (this.support()) {
        List<XmlElement> results = new ArrayList<>();
        for (IntrospectedColumn incColumn : this.incColumns) {
            // !!! 不能用contains,IntrospectedColumn对象不同
            for (IntrospectedColumn column : columns) {
                if (incColumn.getActualColumnName().equals(column.getActualColumnName())) {
                    XmlElement when = new XmlElement("when");

                    // 需要 inc 的列
                    String columnMap = "record." + FIELD_INC_MAP + "." + MyBatis3FormattingUtilities.escapeStringForMyBatis3(incColumn.getActualColumnName());

                    when.addAttribute(new Attribute("test", "'" + column.getActualColumnName() + "'.toString() == column.value"));
                    when.addElement(new TextElement("${column.escapedColumnName} = ${column.escapedColumnName} "
                            + "${" + columnMap + "." + FIELD_OPERATE_FOR_CLASS_INCREMENT + "} "
                            + XmlElementGeneratorTools.getParameterClause(columnMap + "." + FIELD_VALUE_FOR_CLASS_INCREMENT, incColumn))
                    );

                    results.add(when);
                }
            }
        }
        return results.isEmpty() ? null : results;

    }
    return null;
}
 
Example #30
Source File: IncrementsPlugin.java    From mybatis-generator-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * 生成增量操作节点(SelectiveEnhancedPlugin)
 * @param columns
 * @return
 */
@Override
public List<XmlElement> incrementSetsWithSelectiveEnhancedPluginElementGenerated(List<IntrospectedColumn> columns) {
    if (this.support()) {
        List<XmlElement> results = new ArrayList<>();

        for (IntrospectedColumn incColumn : this.incColumns) {

            // !!! 不能用contains,IntrospectedColumn对象不同
            for (IntrospectedColumn column : columns) {
                if (incColumn.getActualColumnName().equals(column.getActualColumnName())) {
                    XmlElement when = new XmlElement("when");

                    // 需要 inc 的列
                    when.addAttribute(new Attribute("test", "'" + column.getActualColumnName() + "'.toString() == column.value"));
                    when.addElement(new TextElement("${column.escapedColumnName} = ${column.escapedColumnName} ${record." + METHOD_GET_INC_MAP + "()."
                            + incColumn.getActualColumnName()
                            + ".value} "
                            + XmlElementGeneratorTools.getParameterClause("record.${column.javaProperty}", incColumn)));
                    results.add(when);
                }
            }
        }

        return results.isEmpty() ? null : results;
    }
    return null;
}