关于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可以改成你想要的数据。

 

posted @ 2019-04-01 14:04  疯狂的字母  阅读(3363)  评论(0编辑  收藏  举报