關於mybatis返回值resultType為空的問題


假設數據庫中一個user表 此時只有id為1的數據,當我們查詢id為2的年齡時的時候返回值為null

但是在mybatis中預定義UserMapper.xml中

<select id="findUserAgeById" parameterType="int" resultType="int">
SELECT user.age  FROM user WHERE id = #{id}
</select>

此時會報錯:attempted to return null from a method with a primitive return type (int)

改正之后的select語句為

<select id="findUserAgeById" parameterType="int" resultType="int">
SELECT IFNULL(max(news.userid),0) FROM news WHERE id = #{id}
</select>

這樣就會使返回的null變成0,此時返回值為0,不報錯。

在 IFNULL(max(news.userid),0)這段代碼中,0可以改成你想要的數據。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM