简体   繁体   English

iBATIS 2.3.x是否支持foreach标签?

[英]Does iBATIS 2.3.x support foreach tag?

I have a personal website which uses iBATIS 2.3.x. 我有一个使用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. 对于上面的查询,这只是一个伪代码,因为我没有使用更高版本的iBATIS,其含义是我想生成一个动态查询条件。

My question is: I'm not sure whether iBATIS 2.3.x supports "foreach" tag, if not, how to implement this kind of query? 我的问题是:我不确定iBATIS 2.3.x是否支持“ foreach”标签,如果不支持,如何实现这种查询?

Thanks, Shuiqing 谢谢你水清

You can use "iterate" in 2.3.* in place of foreach like below. 您可以在2.3。*中使用“ iterate”代替如下所示的foreach。 Only iBATIS 3/ MyBATIS uses OGNL based expressions like choose, foreach, trim... 只有iBATIS 3 / MyBATIS使用基于OGNL的表达式,例如select,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. 您可以将parameterClass用作“ java.util.Map”,并通过将“ productTypes”设置为键来传递列表值。

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

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