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 整体长度大小的限制