mybatis設定引數的時候報錯
阿新 • • 發佈:2019-02-01
前言
現在正在趕一個專案,但還是想花點時間來總結一下遇到的坑,希望能夠幫助到其他小夥伴
錯誤型別
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型別,所以肯定會報錯了。
總結
使用框架不瞭解其中的原理,遇到這些問題也是正常的事情,我會去嘗試看原始碼的,先給自己挖個坑。