error:attempted to return null from a method with a primitive return type (int)

1,起因

今天老大讓改bug,說有個模塊數據提交不了,讓我看看。。。檢查服務器發現這個報錯。。attempted to return null from a method with a primitive return type (int)

2,報錯原因

mybatis中resultType返回數據爲int類型,而因爲沒有查詢結果返回爲null,int初始化爲0,不能爲空,所以報了這個錯誤。

3,解決方法

(1)可以通過修改返回數據類型,將int改爲integer類型,integer初始化是可以爲null的,所以可以解決該問題.

(2)可以使用mysql的ifnull函數和max函數將null轉成0.如下:

select IFNULL(MAX(xspx),0)  from user where ID = #{id}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章