假设数据库中一个user表 此时只有id为1的数据,当我们查询id为2的年龄时的时候返回值为null

但是在mybatis中预定义UserMapper.xml中

<select >
SELECT user.age  FROM user WHERE id = #{id}
</select>

此时会报错:attempted to return null from a method with a primitive return type (int)

改正之后的select语句为

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

这样就会使返回的null变成0,此时返回值为0,不报错。

在 IFNULL(max(news.userid),0)这段代码中,0可以改成你想要的数据。

 

相关文章:

  • 2021-06-11
  • 2022-12-23
  • 2022-02-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
  • 2021-05-22
  • 2021-05-28
  • 2022-12-23
相关资源
相似解决方案