2011-10-22 237 views
2

我有一个使用iBATIS 2.3.x的个人网站。最近我加入一个复杂的搜索功能的网站,需要通过对象的列表来查询数据,喜欢:iBATIS 2.3.x是否支持foreach标签?

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,它的含义是我想生成一个动态查询条件。

我的问题是: 我不知道iBATIS的2.3.x版本是否支持“的foreach”标签,如果没有,如何实现这种查询?

感谢, 水清

回答

4

可以在2.3使用“迭代” *到位的foreach像下面。只有iBATIS的3/MyBatis的使用像选择,的foreach,修剪基于OGNL表达式...

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” 作为关键传球列表值。

+1

因此,我可以指定一个产品列表“parameterClass”? – Shuiqing

+0

另一个类似的问题,如果parameterClass包含对象列表,则喜欢: public class PromotionAttributeQuery {0} {0} Long classId; List promotionAttributeList; } 如何在SQL映射中迭代其列表? – Shuiqing

+0

编辑了使用map作为参数的答案。我希望这会回答你的Q – Bala