栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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(*),没有进行优化,这个时候设置参数就没问题了。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/975350.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号