关于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