今天在别人的代码基础上实现新需求,看到对于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