mapper 的返回

發現一個問題記錄一下

如果mapper是返回的對象
Integer selectStaffIdByPayroll(@Param("payroll") String payroll);
<select id="selectStaffIdByPayroll" resultType="java.lang.Integer">
    select staff_id from tb_staff where payroll= #{payroll}
    order by staff_id desc limit 1
</select>

然後你在代碼中接收返回用int:

int staffIdByPayroll = staffMapper.selectStaffIdByPayroll(name);

如果mapper查詢結果沒有,會按Integer的返回,返回爲null,而int 是0.

如果用int接就會出現空指針異常的問題。所以要注意!!

但是這樣

<select id="selectByArticleAccountId" resultType="java.lang.Integer">
    select count(*)
    <include refid="Base_Column_List"/>
    from tb_article_task
    where article_account_id = #{articleAccountId}
</select>

這樣

Integer articleAccountCount = tbArticleTaskMapper.selectByArticleAccountId(id);

if (articleAccountCount == 0) { return updateCount; }

 

就可以避免了。、

不過呢,一般除了更新刪除添加,mybatis給我們處理返回結果之外用int

最好還是用Integer

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章