博客上处理这种情况的方法有很多,这里提供两种方式(均是本人在开发任务中使用过得):
1.直接将超过1000条的数据存入List中一般List<String> idNoAList是这样放的。方法入参getInsuredListByIdNo(Map<String, Object> inParam)使用map方便其他参数的传入。这种方式不用管其他,直接将内容往容器里面塞就可以了。下面提供xml文件内容
<select id="getInsuredListByIdNo" parameterType="map" resultMap="对应返回类型(注意resultType和resultMap的区别)">
select *
from table t where t.contract_id=#{contractId}
<if test="idNoList != null and idNoList.size > 0">
AND (t.id_no in
<!-- 处理in的集合超过1000条时Oracle不支持的情况 -->
<trim suffixOverrides=" OR t.id_no in ()"> <!-- 表示删除最后一个条件 -->
<foreach collection="idNoList" item="IdNo" index="index" open="("close=")">
<if test="index != 0">
<choose>
<when test="index % 1000 == 999">) OR t.id_no in (</when>
<otherwise>,</otherwise>
</choose>
</if>
#{IdNo}
</foreach>
</trim>
)
</if>
</select>
2.第二种方法相对麻烦一点,但是不用使用mybatis那么多标签,即现将数据处理,然后在传入。业务代码先将数据List<String>分割成List<List<String>>形式,下面提供java的分割方法
/**
* 按指定大小,分隔集合,将集合按规定个数分为n个部分
* @param list
* @param len
* @return
*/
public static List<List<String>> splitList(List<String> list, int len) {
if (list == null || list.size() == 0 || len < 1) {
return null;
}
List<List<String>> result = new ArrayList<List<String>>();
int size = list.size();
int count = (size + len - 1) / len;
for (int i = 0; i < count; i++) {
List<String> subList = list.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
result.add(subList);
}
return result;
}
如下调用方式:
inParam.put("ids", splitList(actonAList, 999));
getACheckId(inParam);
方法入参和方法一中的一样Map<String, Object> inParam方便其他参数传入。
对应xml文件写法如下:
<select id="getACheckId" resultType="string" parameterType="map">
select * where t.contract_id = #{contractId}
<if test="ids != null">
and
<foreach collection="ids" index="index" item="idsItem" open="(" separator="or" close=")" >
t.part_insured_id in
<foreach collection="idsItem" index="index" item="id" open="(" separator="," close=")" >
#{id}
</foreach>
</foreach>
</if>
</select>
不难看出,这里使用的是双重for循环,基于目标类型为List<List<String>>内层list即是分割号的999个参数。
以上两种方法均可解决如题问题,如果还有其他更好的方式可以互相学习下。