Does iBATIS 2.3.x support foreach tag?

倾然丶 夕夏残阳落幕 提交于 2019-12-13 16:23:19

问题


I have a personal website which uses iBATIS 2.3.x. Recently I'm adding a complex searching feature to the site, need to query the data by a list of object, likes:

public Class PromotionAttribute {
    String attributeName;
    String attributeValue;
}

The query looks like:

select p.* from promotions p
join promotion_attributes pa on p.id=pa.id
where 
<foreach item="PromotionAttribute" index="index" collection="list" open="(" separator=" or " close=")">
pa.attribute_name=#{attributeName} and pa.attribute_value=#{attributeValue}#
</foreach>

For the above query, it's only a pseudocode since I didn't use the higher version of iBATIS, its meaning is I want to generate a dynamic query condition.

My question is: I'm not sure whether iBATIS 2.3.x supports "foreach" tag, if not, how to implement this kind of query?

Thanks, Shuiqing


回答1:


You can use "iterate" in 2.3.* in place of foreach like below. Only iBATIS 3/ MyBATIS uses OGNL based expressions like choose, foreach, trim...

in Java,

        Map paramMap = new HashMap();
        paramMap.put("productTypes", productTypes);
        sqlMapClient.queryForList("getProducts", paramMap);
in xml,

<select id="getProducts" parameterClass="java.util.Map" 
resultClass="Product">
SELECT * FROM Products
<dynamic prepend="WHERE productType IN ">
<iterate property="productTypes"
    open="(" close=")"
    conjunction=",">
    productType=#productType#
 </iterate>
 </dynamic>
 </select>

You can use parameterClass as "java.util.Map" and pass list value by setting "productTypes" as key.



来源:https://stackoverflow.com/questions/7859338/does-ibatis-2-3-x-support-foreach-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!