前言:
foreach元素的属性主要有 collection、item、index、open、separator、close
collection : foreach循环的对象
item : 集合中每一个元素或者该集合的对象,支持对象点属性的方式获取属性#{obj.filed} 或#{value}
index :循环的下标,从0开始
open : 表示以什么开始
separator : 每次进行迭代之间以什么符号作为分隔符
close :表示以什么结束
trim 标签四个属性和其作用
prefix : 添加前缀
prefixOverrides : 删除前缀
suffix : 添加后缀
suffixOverrides : 删除后缀
问题复现:
select distinct MV_NO "mvnoMark" from table_user
where mv_no=#{mvno}
<if test="userIdList!=null and userIdList.size()>0">
and user_no in
<foreach collection="userIdList" item="userId" open="(" close=")" separator=",">
#{userId,jdbcType=VARCHAR}
</foreach>
</if>
<if test="userIdList.size()==0">
and user_no =''
</if>
错误原因:
当<foreach>标签内的数量超过1000个时会提示一下报错:
java.sql.SQLException: ORA-01795: maximum number of expressions in a list is 1000
解决方案:
一、利用or每1000条添加一个or
select distinct MV_NO "mvnoMark" from table_user
where mv_no=#{mvno}
<if test="userIdList!=null and userIdList.size()>0">
and user_no in
<foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
<if test="(index % 1000) == 999"> NULL) OR user_no in(</if> #{userId,jdbcType=VARCHAR}
</foreach>
</if>
<if test="userIdList.size()==0">
and user_no =''
</if>
分析:
其实也是用 or 进行sql拼接
SQL执行的样子:
user_no in('......','999',NULL ) OR user_noD in('1000',..... NULL) OR user_no in('.....')
二、拼接OR ID IN ()
select distinct MV_NO "mvnoMark" from table_user where mv_no=#{mvno} <if test="userIdList!=null and userIdList.size()>0"> and user_no in <trim suffixOverrides=" OR user_no in ()"> <foreach collection="userIdList" item="userId" index="index" open="(" close=")" > <if test="index != 0"> <choose> <when test="index % 1000 == 999">) OR user_no in (</when> <otherwise>,</otherwise> </choose> </if> #{userId,jdbcType=VARCHAR} </foreach> </trim> </if>
分析:
<trim>标签suffixOverrides:去掉后缀匹配上的东西,本例中后缀如果是 or user_no in()与suffixOverrides的属性值刚好匹配,则去掉 or user_no in()
index 集合迭代的位置从0开始,为何需要<if test="index != 0">?如果没有,则sql是 user_no in (,1,2..)会多一个逗号
没有1000条数据的拼接SQL为:user_no (1,2..999)
超过1000条的数据拼接SQL为:user_no (1,2..999) oruser_no(1000,1001...1999) ...