2011-10-22 89 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