Mybatis plus使用Page对象进行分页查询时异常问题处理

  • 异常打印
    [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property=‘dto.aaa’, mode=IN, javaType=class java.lang.Object, jdbcType=NVARCHAR, numericScale=null, resultMapId=‘null’, jdbcTypeName=‘null’, expression=‘null’}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType NVARCHAR . Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType NVARCHAR . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: 无效的列索引]

  • 查看生成countsql逻辑
    在这里插入图片描述
    根据源码我们查看到在上图的位置会生成countsql

// 这个方法中第一个参数为sql优化,默认为开启,第二个为组装的sql
//SELECT COUNT(*) FROM qhyu t WHERE 1 = 1 AND t.CREATED_BY = ? 
autoCountSql(page.optimizeCountSql(), boundSql.getSql()

打印出来的sql只有一个参数,为什么报错信息提示第二个参数绑定的时候出错了。
此时我发现我们的countsql是进行优化过后的sql,将left join中的一部分给优化了,其中left join中还有where条件。
查看官网发现确认问题
在这里插入图片描述

  • 解决方案
    传入的page对象设置page.setOptimizeCountSql(false);
    重启项目,查看countSql,发现就是在我们的查询语句外层包装select count(*),没有进行优化,这个时候设置参数就没问题了。

猜你喜欢

转载自blog.csdn.net/Tanganling/article/details/125296792