痛点
oracle 11g 只能in
1000个条件,大数据量无法处理
功能说明
用于解决数据库 in
子句参数数量限制问题(如 oracle 的 1000 元素限制),通过动态分块生成多组 in
条件。
完整代码示例
<if test="piids != null and piids.size > 0"> and (t0.processinstid in <trim suffixoverrides=" or t0.processinstid in()"> <foreach collection="piids" item="item" index="index" open="(" close=")"> <if test="index != 0"> <choose> <when test="index % 1000 == 999">) or t0.processinstid in (</when> <otherwise>,</otherwise> </choose> </if> #{item} </foreach> </trim> ) </if>
语法逐层解析
- 外层条件判断
<if test="piids != null and piids.size > 0">
- trim 标签清理
<trim suffixoverrides=" or t0.processinstid in()">
解决的问题:消除最后一块可能残留的无效语句
机制:自动删除结尾匹配的指定字符串 - foreach 分块逻辑
<foreach collection="piids" item="item" index="index" open="(" close=")">
分块规则
<choose> <when test="index % 1000 == 999">) or t0.processinstid in (</when> <otherwise>,</otherwise> </choose>
分块策略:
每 1000 个元素 分块(索引从0开始计算)
index % 1000 == 999 ➔ 第1000个元素时触发分块
连接方式:
同块元素用 , 连接
分块时用 ) or t0.processinstid in ( 连接
数据库适配对照表
数据库 | in 列表限制 | 调整方案 |
---|---|---|
oracle | 1000 | 保持现有代码:index % 1000 == 999 (索引从0开始,第1000个元素时分块) |
sql server | 2100 | 修改为:index % 2100 == 2099 (索引达到2099时触发分块) |
mysql | 无硬性限制 | 移除分块逻辑,直接逗号连接所有元素 |
postgresql | 无硬性限制 | 移除分块逻辑,直接逗号连接所有元素 |
补充说明:
oracle/sql server 分块逻辑
- 因数据库对
in
子句的表达式数量有严格限制,需通过模运算 (%
) 控制分块节点。 - 公式:
index % n == (n-1)
,其中n
为数据库允许的最大值(oracle=1000,sql server=2100)。
- 因数据库对
mysql/postgresql 优化
- 可直接简化为:
<foreach collection="piids" item="item" open="(" close=")" separator=","> #{item} </foreach>
- 可直接简化为:
到此这篇关于mybatis 动态sql巧用突破in数量限制的文章就介绍到这了,更多相关mybatis in数量限制内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论