Mybatis使用foreach标签,in的限制(oracle、mysql)

1.Mysql数据库

  • 对 in 语句不限制元素个数

使用示例:

	select * from sys_user
        <where>
            <if test="list!=null and list.size()>0">
                id in
                <foreach collection="list" item="item" open="(" close=")" separator="," index="index">
                    #{item}
                </foreach>
            </if>
        </where>
  • 对 sql 整体长度大小限制

解决方案:

需要修改my.ini 中的 max_allowed_packet 参数

2.Oracle数据库

  • 对 in 语句限制元素个数

使用示例:

	select * from sys_user
        <where>
            <if test="list!=null and list.size()>0">
                (
                id IN
                <trim suffixOverrides=" OR id IN()">
                    <foreach collection="list" item="item" open="(" close=")" index="index">
                        <if test="index != 0">
                            <choose>
                                <when test="(index % 1000 ) == 999">) OR id IN (</when>
                                <otherwise>,</otherwise>
                            </choose>
                        </if>
                        #{item}
                    </foreach>
                </trim>
                )
            </if>
        </where>

标签详解:

<trim>
一般用于去除sql语句中多余的and关键字,逗号等多余符号
prefix:拼接的前缀
suffix:拼接的后缀
prefixOverrides:指定去除多余的前缀内容
suffixOverrides:指定去除多余的后缀内容
<choose>
拼接SQL语句,类似于 Java中的switch语句,when相当于case,otherwise相当于default
<foreach>
collection: 要遍历的集合
item: 每一项
open: 开始符号
close: 结束符号
index: 从0开始,遍历次数
  • 未查到明确的对sql 整体长度大小的限制
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值