日期:2014-05-20  浏览次数:20723 次

ibatis的select中如何使用in条件
在ibatis中如何使用in条件
select * from table where xxx in(参数1,参数2,参数3)
参数数量是可变的,即有可能有2个参数,有可能有3个参数

------解决方案--------------------
<delete id="deleteBookByIdOrIds">
DELETE FROM book
<isParameterPresent>
<isPropertyAvailable property="keyList">
<isNotEmpty property="keyList">
<iterate property="keyList"
prepend="where id in " open="(" close=")" conjunction=",">
#keyList[]#
</iterate>
</isNotEmpty>
</isPropertyAvailable>
</isParameterPresent>
</delete>

传参数时,
List <Integer>list=new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
Map map=new HashMap();
map.put("keyList",list);
sqlMapClient.delete("deleteBookByIdOrIds",map);



 

------解决方案--------------------
呵呵 ,下次免费。
------解决方案--------------------
探讨
<delete id="deleteBookByIdOrIds">
DELETE FROM book
<isParameterPresent>
<isPropertyAvailable property="keyList">
<isNotEmpty property="keyList">
<iterate property="keyList"
prepend="where id in " open="(" close=")" conjunction=",">
#keyList[]#
</iterate>
</isNotEmpty>
</isPropertyAvailable>
</isParameterPresent>
</delete>

传参数时,
List <Integer>list=new ArrayList …

------解决方案--------------------
支持楼上的方法

Iterate:这属性遍历整个集合,并为List集合中的元素重复元素体的内容。
Iterate的属性:
prepend - 可被覆盖的SQL语句组成部分,添加在语句的前面(可选)
property - 类型为java.util.List的用于遍历的元素(必选)
open - 整个遍历内容体开始的字符串,用于定义括号(可选)
close -整个遍历内容体结束的字符串,用于定义括号(可选)
conjunction - 每次遍历内容之间的字符串,用于定义AND或OR(可选)
<iterate>
遍历类型为java.util.List的元素。
例子:
<iterate prepend=”AND” property=”userNameList” open=”(” close=”)” conjunction=”OR”>
username=#userNameList[]#
</iterate>
注意:使用<iterate>时,在List元素名后面包括方括号[]非常重要,方括号[]将对象标记为List,以防解析器简单地将List输出成String。


其实还有一个简单方法就是动态在java中拼接出字符串,然后在xml中用$引入

id in ($userNameList$)

------解决方案--------------------
高手如云啊!学习
------解决方案--------------------
探讨
其实还有一个简单方法就是动态在java中拼接出字符串,然后在xml中用$引入
id in ($userNameList$)

------解决方案--------------------

------解决方案--------------------
扩展:
利用ibatis的iterate迭代可以动态构建sql不只是搞个in怎么小的功能
比较动态构建个内嵌视图等,有助于提升性能或解决数据库语法兼容性问题。