假設數據庫中一個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可以改成你想要的數據。