Iterate:这属性遍历整个集合,并为 List 集合中的元素重复元素体的内容。 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语句: <select id="selectUserInfoByNames" parameterClass="java.util.Map" resultclass="java.util.List"> select * from users where name in <iterate property="nameList" conjunction="," close=")" open="(" /> #nameList[]# </iterate> and age < #age# </select> 生成的SQL形如: select * from users where name in ('admin','system','manager') and age < 18; ibatis中配置or语句: <select id="selectUserInfoByNames" parameterClass="java.util.Map" resultClass="java.util.List"> select * from users where age < #age# <iterate prepend="AND" property="UserNameList" open="(" close=")" conjunction="OR"> username=#UserNameList[]# </iterate> </select> 生成的SQL形如: select * from users where age < 20 and username='admin' or username='scott' or username='root'; 再来一个批量插入的例子: <statement id="batchInsertUserInfo" parameterClass="java.util.Map"> insert into users <iterate property="nameList" conjunction=" union all " close=")" open="(" /> select users_seq.nextval,#namelList[]# from dual </iterate> </statement> 生成的SQL形如: insert into users select 100001,'admin' from dual union all select 100002,'system' from dual; 注意:使用<iterate>时,在List元素名后面包括方括号[]非常重要,方括号[]将 对象标记为List,以防解析器简单地将List输出成String。
IBatis之Iterate
猜你喜欢
转载自luckystar2008.iteye.com/blog/1908779
今日推荐
周排行