问题描述:
org.apache.ibatis.binding.BindingException: Mapper method 'com.dao.UserDao.getSum' attempted to return null from a method with a primitive return type (long).
问题分析:
1、使用sum()函数时如果没有符合where条件的记录会返回null,同时resultType为基本类型,数据绑定时会报错。
<select id="getSum" resultType="long">
SELECT
sum(t.age)
FROM t_user t
WHERE user_id='1'
</select>
解决办法:resultType使用包装类。
<select id="getSum" resultType="java.lang.Long">
SELECT
sum(t.age)
FROM t_user t
WHERE user_id='1'
</select>