IT源码网

mybatis where in语句中参数过多

qq123 2021年02月13日 编程语言 470 0

ps : mybatis在in中参数过多

出现问题

com.microsoft.sqlserver.jdbc.SQLServerException: 传入的请求具有过多的参数。该服务器支持最多 2100 个参数

原因

  • SqlServer 对语句的条数和参数的数量都有限制,分别是 1000 和 2100。
  • Mysql 对语句的长度有限制,默认是 4M。
  • Mybatis 对动态语句没有数量上的限制

https://blog.csdn.net/wangfei964279744/article/details/78352055#commentsedit

 
### Error querying database.  Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 传入的请求具有过多的参数。该服务器支持最多 2100 个参数。请减少参数的数目,然后重新发送该请求。 
### The error occurred while setting parameters 
### SQL:  
### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 传入的请求具有过多的参数。该服务器支持最多 2100 个参数。请减少参数的数目,然后重新发送该请求。 
; uncategorized SQLException for SQL []; SQL state [S0001]; error code [8003]; 传入的请求具有过多的参数。该服务器支持最多 2100 个参数。请减少参数的数目,然后重新发送该请求。; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: 传入的请求具有过多的参数。该服务器支持最多 2100 个参数。请减少参数的数目,然后重新发送该请求。 

解决方案

SQLserver对可以设置的参数有限制。

我们可以采用自己拼接 SQL 语句的方式来实现 in 的操作

我在 java 代码中进行如下操作

String list = parkingIdss.toString(); 
        String list1 = list.substring(1, list.length() - 1); 
        List<ParkingVo> parking = null; 
        if ("".equals(list1)) { 
            parking = parkMapper.selectParkingInfoByPrimaryKeyList(null); 
        } else { 
            parking = parkMapper.selectParkingInfoByPrimaryKeyList("(" + list1 + ")"); 
        } 

SQL 语句中如下操作:

<where> 
    <if test="list != null"> 
        and p.Id in ${list} 
        
    </if> 
</where> 
评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!