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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.