IFNULL

int selectCartProductCount(Integer userId)
<select id="selectCartProductCount">
    select  sum(count) as count from mmall_cart where user_Id = #{userId}
</select>

When userId does not exist, select statement returns null, null values ​​can not be assigned to the basic type int, database statements should be read

<select id="selectCartProductCount">
    select IFNULL(sum(quantity), 0) as count from mmall_cart where user_Id = #{userId}
</select>

If the sum (quantity) set to the default value is empty 0

Reproduced in: https: //www.jianshu.com/p/ad1e2c317f70

Guess you like

Origin blog.csdn.net/weixin_34038293/article/details/91052253