mybatis where in语句中参数过多

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>
posted @ 2018-12-24 17:37  DaleyZou  阅读(6054)  评论(0编辑  收藏  举报