mybatis中查询结果为空时不同返回类型对应返回值

时间:2020-08-12 15:33:16 类型:JAVA
字号:    

  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


<