Mybtais处理Oracle数据库in的集合超过1000条时的情况

博客上处理这种情况的方法有很多,这里提供两种方式(均是本人在开发任务中使用过得):

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个参数。

以上两种方法均可解决如题问题,如果还有其他更好的方式可以互相学习下。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值