mybatis中resultType有多種返回類型,對于每種不同類型,查詢結果為空時dao接口的返回值是一樣的嗎?
第一種:resultType為基本類型,如string(在此暫且把string歸納為基本類型)
如果select的結果為空,則dao接口返回結果為null
第二種,resultType為基本類型,如int
后臺報異常:
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、hashmap
dao層接口返回值為null
第四種 resultType 為list ,如list
dao層接口返回值為[],即空集合。
注意:此時判斷查詢是否為空就不能用null做判斷
第五種 resultType 為類 ,如com.fkit.pojo.User
dao層接口返回值null