假设数据库中一个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可以改成你想要的数据。