attempted to return null from a method with a primitive return type (double)

问题场景:
服务启动正常,MyBatis Interface 中的接口调用时出现异常。

业务描述:
通过用户标识获取用户目前为止的所有投资收益。但测试使用的用户是新注册的用户,无投资金额。

问题分析:
DEBUG模式跟踪,发现底层mapper.xml 中的 SQL
select sum(invest) from db


此时 invest 为空 , sum (null) , 赋值给 double ,所以系统提示异常,尝试把一个 null值复制给 double 类型数据

问题结局:
1.添加函数判空处理
select IFNULL(sum(invest),0) FROM DB

返回值时进行判断,若为空,则返回0
mapper的返回值,尽量使用包装类

2.修改mybatis 中 select 接口的 resultType = Double 改为 double
若结果为空,默认返回 0

猜你喜欢

转载自mingyundezuoan.iteye.com/blog/2394613