mybatis设置参数的时候报错

xiaoxiao2021-02-28  52

前言

现在正在赶一个项目,但还是想花点时间来总结一下遇到的坑,希望能够帮助到其他小伙伴

错误类型

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='0', mode=IN, javaType=class java.lang.Integer, 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.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer ### The error may exist in mapper/ComplaintMapper.xml ### The error may involve dao.ComplaintMapper.getSearchComplaintData-Inline ### The error occurred while setting parameters

其中的核心是

java.lang.String cannot be cast to java.lang.Integer

看到这一句我有点懵逼,为什么会有这个错误呢?并且自己在之前设置参数的时候并没有遇到这个问题呀,好奇怪。然后我们仔细看看我们写的在mapper中写的SQL语句

下面是可以正常工作的语句
<select id="getSearchComplaintData" resultMap="resultComplaintList"> SELECT complaintID,content,courierNumber,content,complaintTime FROM Complaint LEFT JOIN Customer ON Customer.customerID = Complaint.customerID WHERE content LIKE "%"#{0}"%" AND courierNumber LIKE "%"#{1}"%" </select>
下面是不可以正常工作的语句
<select id="getSearchComplaintData" parameterType="int" resultMap="resultComplaintList"> SELECT complaintID,content,courierNumber,content,complaintTime FROM Complaint LEFT JOIN Customer ON Customer.customerID = Complaint.customerID WHERE content LIKE "%"#{0}"%" AND courierNumber LIKE "%"#{1}"%" </select>

仔细观察我们可以发现不能够正常工作的语句多了一个

parameterType="int"

我们把这个删除就可以正常了,为什么呢?因为这个字段是设置参数的类型,很明显我们上面的content和courierNumber 字段并不是int类型,所以肯定会报错了。

总结

使用框架不了解其中的原理,遇到这些问题也是正常的事情,我会去尝试看源码的,先给自己挖个坑。

转载请注明原文地址: https://www.6miu.com/read-2621902.html

最新回复(0)