2017年04月24日   码农之路   17,781 次浏览

刚刚解决了一个小异常:BindingException:Mapper method attempted to return null from a method with a primitive return type (int),接下来我们一起来剖析一下这个异常。

首先我们看到异常所在位置的源代码:

  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (method.getDeclaringClass() == Object.class) {
      return method.invoke(this, args);
    }
    final Class<?> declaringInterface = findDeclaringInterface(proxy, method);
    final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession);
    final Object result = mapperMethod.execute(args);
    if (result == null && method.getReturnType().isPrimitive() && !method.getReturnType().equals(Void.TYPE)) {
      throw new BindingException("Mapper method '" + method.getName() + "' (" + method.getDeclaringClass() + ") attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
    }
    return result;
  }

我们看到这个异常的抛出条件:result == null && method.getReturnType().isPrimitive(),进一步跟跟踪isPrimitive方法,可以看到以下注释:

     /**
      * <p> There are nine predefined {@code Class} objects to represent
      * the eight primitive types and void.  These are created by the Java
      * Virtual Machine, and have the same names as the primitive types that
      * they represent, namely {@code boolean}, {@code byte},
      * {@code char}, {@code short}, {@code int},
      * {@code long}, {@code float}, and {@code double}.
      */
      public native boolean isPrimitive();

好了,到这里我们就可以知道,如果mybatis的查询结果为null,并且返回值为boolean、int、long等java基本类型的时候就会报错了,为什么?因为这些基本类型无法赋值为null

知道了原因,那么解决方法就简单了:

1、把返回值改为基本类型对应的封装类型,如boolean改为Boolean、int改为Integer,这里需要注意要对返回结果为null进行判断和处理;

2、处理查询结果为null时的默认值,比如我这里用到的是sum()函数,当数据库没有记录里sum()查询结果则为null,我通过nvl(sum(num),0)就把为null时的默认值设置为0了,这个大家可以具体情况具体分析。另:刚在oracle试了下,count()函数可以正常返回0,sum()、min()、max()不可以正常返回,大家以后遇到类似的用法是就要注意不要再出类似错误了。

 

赞 赏
申明:除非注明,本站文章均为原创,转载请以链接形式标明本文地址。 如有问题,请于一周内与本站联系,本站将在第一时间对相关内容进行处理。
本文地址: http://www.yyjjssnn.cn/articles/756.html

>>> Hello World <<<

这篇内容是否帮助到你了呢?

如果你有任何疑问或有建议留给其他朋友,都可以给我留言。

目前有2条留言:

  • 1# 文刀木易:
    2017-09-25 10:35
    @aa

    如果你确定有数据返回的话,就要看你的SQL是否正确呢?先在工具里检查下SQL结果集是否有空值。

  • 2# aa:
    2017-09-24 17:11

    插入一天数据不是返回一吗,为什么int 不行

:wink: :twisted: :surprised: :smile: :smile9: :smile8: :smile7: :smile6: :smile5: :smile56: :smile55: :smile54: :smile53: :smile52: :smile51: :smile50: :smile4: :smile49: :smile48: :smile47: :smile46: :smile45: :smile44: :smile43: :smile42: :smile41: :smile40: :smile3: :smile39: :smile38: :smile37: :smile36: :smile35: :smile34: :smile33: :smile32: :smile31: :smile30: :smile2: :smile29: :smile28: :smile27: :smile26: :smile25: :smile24: :smile23: :smile22: :smile21: :smile20: :smile1: :smile19: :smile18: :smile17: :smile16: :smile15: :smile14: :smile13: :smile12: :smile11: :smile10: :smile0: :sad: :rolleyes1: :redface: :razz: :question: :neutral: :mrgreen: :mad: :lol: :idea: :exclaim: :evil: :eek: :cry: :cool: :confused: :biggrin: :arrow:

友情链接: 程序员刘杨 刘杨
Copyright 2003~2018 保留所有权利 | 网站地图
备案号:湘ICP备14001005号-2

湘公网安备 43011102001322号