美文网首页
select 返回null

select 返回null

作者: 山顶冻人0 | 来源:发表于2017-12-21 17:25 被阅读0次

数据库中没有满足条件的id 就会报
HTTP Status 500 - Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Mapper method 'org.tsinghua.cis.dao.CisMedicineDetailsIdDao.findMedicineDetailsIdByClinicIdAndBarCode attempted to return null from a method with a primitive return type (int).

   <select id="findMedicineDetailsIdByClinicIdAndBarCode" resultType="int">
       <![CDATA[
        SELECT
          a.`id`
        FROM
          `cis_medicine_details_id` AS  a
        INNER JOIN
          `cis_medicine_pricing` AS b
        ON
          a.id = b.medicine_details_id
        WHERE
          `medicine_bar_code` = ${medicineBarCode}
        AND
          `clinic_id` = ${clinicId}
        ]]>
    </select>

解决方案

1.把resultType="int" 改为resultType="Integer"

在server层 判断 if(null==参数){
做相应的操作 是赋值0还是返回null 根据需要自行编写
}

2.使用IFNULL函数

   <select id="findMedicineDetailsIdByClinicIdAndBarCode" resultType="int">
       <![CDATA[
        SELECT
          IFNULL(a.`id`,0)
        FROM
          `cis_medicine_details_id` AS  a
        INNER JOIN
          `cis_medicine_pricing` AS b
        ON
          a.id = b.medicine_details_id
        WHERE
          `medicine_bar_code` = ${medicineBarCode}
        AND
          `clinic_id` = ${clinicId}
        ]]>
    </select>

相关文章

网友评论

      本文标题:select 返回null

      本文链接:https://www.haomeiwen.com/subject/qivawxtx.html