简体   繁体   中英

MyBatis - bind in foreach for parameter

I've found following issue for mybatis: SQL parameter substitution functionality differs substantially from string substitution functionality .

Does it exist a workaround, eg. using java code (static methods?) in #{} expression?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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