ibatis的iterate使用
Iterate:这属性遍历整个集合
Iterate 的属性:
prepend - 可被覆盖的 SQL 语句组成部分,添加在语句的前面(可选)
property - 类型为
java.util.List 的用于遍历的元素(必选)
open - 整个遍历内容体开始的字符串,用于定义括号(可选)
close -整个遍历内容体结束的字符串,用于定义括号(可选)
conjunction - 每次遍历内容之间的字符串, AND 或 OR(可选)或者逗点","
遍历类型为 java.util.List的元素
例子:
<iterate prepend=”AND” property=”userNameList”open=”(” close=”)”
conjunction=”OR”> username= # userNameList[] #
</iterate>
ibatis中如何配置in语句,需要迭代,不能直接用string的写法
eg-1:
<select id="sql_test" parameterresultconjunction="," close=")" open="(" />
#value[]#
</iterate>
and code=#code#
</select>
eg-2:
<delete id="deleteSb" parameteropen="(" close=")" > # value[] #
</iterate>
</delete>
eg-3
<sql id="sql_where">
from idleYouths iy,idleYouthsHasPropertyDicts hDicts
where 1=1 and iy.id = hDicts.Idleyouthsid
<dynamic>
<isNotNull property="staffTypeIds" prepend=" and ">
hDicts.PROPERTYDICTID in
<iterate property="staffTypeIds" close=")" open="(" conjunction="," >
#staffTypeIds[]#
</iterate>
</isNotNull>
</dynamic>
</sql>
注意:使用<iterate>时,在List元素名后面包括方括号[]非常重要,方括号[ ]将对象标记为List,以防解析器简单地将List输出成String。