简体   繁体   English

MyBatis - 在foreach中绑定参数

[英]MyBatis - bind in foreach for parameter

I've found following issue for mybatis: SQL parameter substitution functionality differs substantially from string substitution functionality . 我发现以下mybatis问题: SQL参数替换功能与字符串替换功能有很大不同

Does it exist a workaround, eg. 是否存在变通方法,例如。 using java code (static methods?) in #{} expression? #{}表达式中使用java代码(静态方法?)?

I need to create following statement: 我需要创建以下语句:

<foreach item="c" collection="filter.getFilter()" separator=" AND " open="(" close=")">
    <bind name="column" value="_parameter.mappingWhere(c.colCode)"/>
    <bind name="operator" value="_parameter.conditionOperator(c.condition)"/>
    <bind name="value" value="_parameter.conditionValue(c.condition, c.value)"/>
        ${column} ${operator} #{value}
</foreach>  

but the value always takes the last value. 但该始终采用最后一个值。

I created my extension of an item in the parameter class, that just wraps all methods: 我在参数类中创建了一个项目的扩展,它只包装了所有方法:

    class MyItem extends Item {

    public MyItem (Item pxFilterItem) {
        super();
        setColCode(pxFilterItem.getColCode());
        setColHeader(pxFilterItem.getColHeader());
        setCondition(pxFilterItem.getCondition());
        setJsonCls(pxFilterItem.getJsonCls());
        setJsonValue(pxFilterItem.getJsonValue());
        setValue(pxFilterItem.getValue());
    }

    public String getSqlColumn(){
        return mappingWhere((String) getColCode());
    }

    public String getSqlOperator(){
        return conditionOperator(getCondition());
    }

    public Object getSqlValue(){
        return conditionValue(getCondition(), getValue());
    }
}

, replaced original objects with new ones before executing query: ,在执行查询之前用新的替换原始对象:

    List<Item> myList = new ArrayList<Item>();
    for (Item item: filter.getFilter()) {
        MyItem myItem = new MyItem(item);
        myList.add();
    }
    this.filter.setFilter(myList);

and rewrote the query: 并重写了查询:

        <foreach item="c" collection="filter.getFilter()" separator=" AND " open="(" close=")">
            ${c.sqlColumn} ${c.sqlOperator} #{c.sqlValue}
        </foreach>            

Not really nice, but works. 不是很好,但有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM