mybatis中查詢結果爲空時不一樣返回類型對應返回值

今天在別人的代碼基礎上實現新需求,看到對於mybatis查詢結果的判斷不是很正確,若是查詢結果爲空就會異常,不知道你們有沒有這樣的疑惑:mybatis中resultType有多種返回類型,對於每種不一樣類型,查詢結果爲空時dao接口的返回值是同樣的嗎?接下來我就總結一下常見的幾種狀況。apache

第一種:resultType爲基本類型,如string(在此暫且把string概括爲基本類型)mybatis

  若是select的結果爲空,則dao接口返回結果爲nullapp

第二種,resultType爲基本類型,如intspa

後臺報異常:
org.apache.ibatis.binding.BindingException: Mapper method 'com.fkit.dao.xxDao.getUserById attempted to return null from a method with a primitive return type (int).
解釋:查詢結果爲null,試圖返回null可是方法定義的返回值是int,null轉爲int時報錯
解決辦法:修改select的返回值爲String接口

第三種 resultType爲類爲map ,如map、hashmapget

  dao層接口返回值爲nullstring

第四種 resultType 爲list ,如listhash

  dao層接口返回值爲[],即空集合。it

注意:此時判斷查詢是否爲空就不能用null作判斷io

第五種 resultType 爲類 ,如com.fkit.pojo.User

  dao層接口返回值null

相關文章
相關標籤/搜索