foreach
foreach元素的属性主要有 item,index,collection,open,separator,close。item:表示集合中每一个元素进行迭代时的别名,index:指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open:表示该语句以什么开始,separator:表示在每次进行迭代之间以什么符号作为分隔 符,close:表示以什么结束。collection:1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list单参数List的类型:接口方法:public List dynamicForeachTest(List ids);<select id="dynamicForeachTest" parameterType="java.util.List" resultType="Blog"> select * from t_blog where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array单参数array数组的类型:接口方法:public List dynamicForeach2Test(int[] ids);<select id="dynamicForeach2Test" parameterType="java.util.ArrayList" resultType="Blog"> select * from t_blog where id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach></select> 3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可参数封装成Map的类型:接口方法:public List dynamicForeach3Test(Map params); <select id="dynamicForeach3Test" parameterType="java.util.HashMap" resultType="Blog"> select * from t_blog where title like "%"#{title}"%" and id in <foreach collection="ids" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach></select>4.实际开发使用接口方法:List<Map<String,Object>> selectFileListByDir(@Param("params") Map<String, String> params);<select id="selectFileListByDir" resultType="java.util.Map" parameterType="hashmap"> SELECT * FROM RTOC_FILE_SYSTEM WHERE <foreach collection="params" index="key" item="value" separator="AND"> <choose> <when test='key=="startTime"'> UPLOAD_DATE >= to_date(#{value},'yyyy-MM-dd hh24:mi:ss') </when> <when test='key=="endTime"'> UPLOAD_DATE <= to_date(#{value},'yyyy-MM-dd hh24:mi:ss') </when> <when test='key=="UPLOAD_USER"'> ${key} = #{value} </when> <otherwise> ${key} like '%' || #{value} || '%' </otherwise> </choose> </foreach> ORDER BY UPLOAD_DATE DESC</select>