select sum(patient_num) as patient_num from patient_report
<where>
    project_id = #{projectId}
    <if test="siteId != null">
       and site_id = #{siteId}
    </if>
</where>

数据库字段返回NULL时,报空指针异常
解决方法

select COALESCE(sum(patient_num), 0) as patient_num from patient_report
<where>
    project_id = #{projectId}
    <if test="siteId != null">
       and site_id = #{siteId}
    </if>
</where>

加上COALESCE函数

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐