Mybatis异常解决之:java.sql.SQLException: Parameter index out of range

mybatis使用过程中遇到下面一个异常:

org.springframework.dao.TransientDataAccessResourceException:
### Error querying database.  Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
### The error may exist in conf/entitymap/diseaseMapper.xml
### The error may involve diseaseMapper.queryCountByName-Inline
### The error occurred while setting parameters
### SQL: select         count(1)         from tb_disease where 1=1                       AND name like CONCAT('%','?','%')
### Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
; SQL []; Parameter index out of range (1 > number of parameters, which is 0).; nested exception is java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
  at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:106) ~[spring-jdbc-3.2.9.RELEASE.jar:3.2.9.RELEASE]
  at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) ~[spring-jdbc-3.2.9.RELEASE.jar:3.2.9.RELEASE]
  at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80) ~[spring-jdbc-3.2.9.RELEASE.jar:3.2.9.RELEASE]
  at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80) ~[spring-jdbc-3.2.9.RELEASE.jar:3.2.9.RELEASE]
  at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73) ~[mybatis-spring-1.2.2.jar:1.2.2]
  at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371) ~[mybatis-spring-1.2.2.jar:1.2.2]
  at com.sun.proxy.$Proxy70.selectOne(Unknown Source) ~[na:na]
  at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:163) ~[mybatis-spring-1.2.2.jar:1.2.2]

根据错误信息 

select count(1) from tb_disease where 1=1 AND name like CONCAT('%','?','%')

找到相应配置如下,发现原来是#{diseaseName}用法有误:CONCAT('%','#{diseaseName}','%')

mapper文件配置如下:

select id="queryCountByName" resultType="java.lang.Long" parameterType="java.util.HashMap">
        select
        count(1)
        from tb_disease where 1=1
        <if test="diseaseName!=null and diseaseName!=''">
            AND name like CONCAT('%','#{diseaseName}','%')
        </if>
    </select>

在mapper文件中使用#占位符传参时,一定不要在两侧添加引号,否则会导致参数解析异常。

正确的配置方式应该是以下这样:

<select id="queryCountByName" resultType="java.lang.Long" parameterType="java.util.HashMap">
        select
        count(1)
        from tb_disease where 1=1
        <if test="diseaseName!=null and diseaseName!=''">
            AND name like CONCAT('%',#{diseaseName},'%')
        </if>
    </select>

另外,或者可以把#改成$,但是mapper文件中使用$会有sql注入漏洞,所以不建议使用!

附:Mybatis模糊查询MySQL中记录的的常用三种方法

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值