[笔记]Mybatis通过 Mapper 代理理实现⾃自定义接⼝模糊查询的坑

1,108 阅读1分钟

Mapper.xml select 语句如下

 <select id="findAll" resultType="com.xhz.api.entity.TextBookFunction">
        SELECT * FROM (SELECT ROW_NUMBER() OVER (order by T.sort asc,id desc)AS Row, T.* from Text_Book_Function T
        where 1=1
        <if test="bookType != 0 and bookType != -1">and BookType = #{bookType}</if>
        <if test="name != ''">and Name like '%#{name}%'</if>
        )TT WHERE TT.Row between #{startIndex} and #{endIndex};
</select>

结果报如下错误

### Error querying database.  Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='endIndex', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #3 with JdbcType null . 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 #3 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 索引 3 超出范围。
### The error may exist in com/example1/demo1/repository/TextBookFunctionRepository.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT * FROM (SELECT ROW_NUMBER() OVER (order by T.sort asc,id desc)AS Row, T.* from Text_Book_Function T         where 1=1                    and Name like '%?%'          )TT WHERE TT.Row between ? and ?;
### Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='endIndex', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #3 with JdbcType null . 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 #3 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 索引 3 超出范围。] with root cause

com.microsoft.sqlserver.jdbc.SQLServerException: 索引 3 超出范围。

查询了很久才知道并不是因为传进来的参数类型有问题
问题出在了Name like '%#{name}%'这里 在字符串中#无法起到占位符的作用了,在字符串中需要使用$!!