iBATIS 2.3.x是否支持foreach标签? [英] Does iBATIS 2.3.x support foreach tag?

查看:896
本文介绍了iBATIS 2.3.x是否支持foreach标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用iBATIS 2.3.x的个人网站.最近,我在网站上添加了复杂的搜索功能,需要通过对象列表来查询数据,例如:

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;
}

查询如下:

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>

对于上述查询,这只是一个伪代码,因为我没有使用更高版本的iBATIS,其含义是我想生成一个动态查询条件.

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 2.3.x是否支持"foreach"标签,如果不支持,该如何实现这种查询?

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

谢谢, 水清

推荐答案

您可以在2.3.*版中使用"iterate" 代替如下所示的foreach.只有iBATIS 3/MyBATIS使用基于OGNL的表达式,例如select,foreach,trim ...

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>

您可以将parameterClass用作"java.util.Map",并通过将"productTypes"设置为键来传递列表值.

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

这篇关于iBATIS 2.3.x是否支持foreach标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆