Mybatis plus uses the Page object for pagination query exception problem handling

  • 异常打印
    [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: 无效的列索引]

  • View the logic of generating countsql
    insert image description here
    According to the source code, we can see that countsql will be generated at the position in the above figure

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

The printed sql has only one parameter, why the error message indicates that there is an error in the binding of the second parameter.
At this time, I found that our countsql is an optimized sql, and a part of the left join is optimized, and there is a where condition in the left join.
Check the official website to find and confirm the problem
insert image description here

  • Solution
    Set page.setOptimizeCountSql(false) for the incoming page object;
    restart the project, check countSql, and find that select count(*) is wrapped in the outer layer of our query statement, without optimization, and it is no problem to set parameters at this time.

Guess you like

Origin blog.csdn.net/Tanganling/article/details/125296792
Recommended