在使用模糊查询时,由于传参问题,导致报500错误!
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='bookName', mode=IN, javaType=class java.lang.String, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
原先的代码(报错)
select * from ssm.books where bookName like concat('%', #{bookName} ,'%')
修改后
select * from ssm.books where bookName like concat('%', #{queryBooks} ,'%')
为什么这么修改呢?
在Java开发中,查询条件往往是作为参数传递过来的,所以既要使用concat()函数,concat()函数的作用为连接字符串。
select * from user where u_name like concat('%', #{name}, '%')
name是从页面上传递过来的参数,不是数据库中的列名