使用mybatis报错: Error setting non null for parameter #1 with JdbcType null .

本文详细解析了在使用MyBatis框架时遇到的参数设置错误,包括TypeException和MySQLSyntaxErrorException的具体原因及解决方法。通过调整SQL语句中的参数引用方式,如避免使用不合适的引号和正确使用concat函数进行字符串拼接,有效解决了MyBatis在执行特定SQL语句时出现的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@TOC使用mybatis报错:

详细错误信息如下:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property=‘tel’, 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 #1 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 (1 > number of parameters, which is 0).

刚开始百思不得其解,仔细查看方法,没错呀!代码如下:
在这里插入图片描述
只得百度了,然后在这篇文章中看到如下说法:
https://blog.youkuaiyun.com/hebsun/article/details/84399483

@Delete("DELETE FROM t_xxx WHERE uid =’#{deviceSn}’ ")
注意,使用#{}的不能用这个双引号’’。真是画蛇添足啊。

我这里尝试了一下,结果还是报错:
Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘%‘14567890987’% and recodStatus=1 order by createTime desc limit 1’ at line 1
这个错误就比较明显了,sql 语句错误,很明显like 不能这么用了,
必须要使用 CONCAT(’%’,#{0},’%’) 进行拼接,结合下面的帖子改了一下,
问题解决了
https://blog.youkuaiyun.com/weixin_38570967/article/details/80753489

修改如下:
在这里插入图片描述

在mybatis 中like 比较特殊,很多时候不能采用常规的方式进行参数传递,必须用concat
进行拼接才能得到正确的结果,这里记录一下。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值