`

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

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$)

比如
String str = "'a','b','c','d'";
配置文件中如果是:
id in ($userNameList$)

结果应该是:id in ('a','b','c','d')

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics