Mybatis返回int类型为空时报错 attempted to return null from a method with a primitive return type (int)

古城微笑少年丶 2022-05-22 23:55 213阅读 0赞

转载自:https://blog.csdn.net/kisscatforever/article/details/77801060

一、前言

  1. 在往常敲代码的时候没有留意过intInteger的区别,今天在敲代码的时候,ORM框架使用的是Mybatis,一个简单的查询,返回查询的条数。当查询为null的时候,就报错了。

二、报的错误

  1. 试图从具有原始返回类型(int)的方法返回null
  2. org.apache.ibatis.binding.BindingException: Mapper method 'com.dmsdbj.itoo.basicInfo.dao.RoomDao.selectSumCountCapacity attempted to return null from a method with a primitive return type (int).
  3. at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:93) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59) at com.sun.proxy.$Proxy35.selectSumCountCapacity(Unknown Source) at com.dmsdbj.itoo.basicInfo.service.impl.PlaceManageServiceImpl.selectSumCountCapacity(PlaceManageServiceImpl.java:858) at com.dmsdbj.itoo.basicInfo.service.impl.PlaceManageServiceImpl$$FastClassBySpringCGLIB$$c31f6b08.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:669) at com.dmsdbj.itoo.basicInfo.service.impl.PlaceManageServiceImpl$$EnhancerBySpringCGLIB$$49d6b6e3.selectSumCountCapacity(<generated>) at com.dmsdbj.itoo.basicInfo.facade.impl.PlaceManageFacadeImpl.selectSumCountCapacity(PlaceManageFacadeImpl.java:439) at com.dmsdbj.itoo.basicInfo.facade.test.PlaceManageFacadeTest.testselectSumCountCapacity_false(PlaceManageFacadeTest.java:342) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

三、解决方案

  1. Mybatismapper文件的返回类型resultTypeInteger

返回类型设置为封装类型Integer而不是基本类型int。

  1. <!--查询未安排考场的教室总容量-王雷-2017年9月1日15:09:25-->
  2. <select id="selectSumCountCapacity" resultType="Integer">
  3. SELECT
  4. IFNULL( SUM(room_capacity),0)
  5. FROM
  6. t_room
  7. <if test="roomTypeId !=null">
  8. WHERE
  9. roomType_id = #{roomTypeId}
  10. </if>
  11. <if test="roomIds !=null and roomIds.size()>0">
  12. AND id NOT IN (
  13. <foreach collection="roomIds" item="item" separator=",">
  14. #{item}
  15. </foreach>
  16. )
  17. </if>
  18. </select>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

    Service之间的返回值也是Ingeger:

    /* 查询未安排考场的教室总容量-王雷-2017年9月1日15:09:25 @param roomIds @param roomTypeId @return /

    1. public Integer selectSumCountCapacity(List<String> roomIds ,String roomTypeId){
    2. if (roomIds==null||roomTypeId==""){
    3. logger.debug("查询未安排考场的教室总容量,已使用的房间id为空,将查询所有的房间容量");
    4. }
    5. return roomDao.selectSumCountCapacity(roomIds, roomTypeId);
    6. }
  • 1

  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

    另外:若遇到该问题,可使用MySQL的IFNULL函数和MAX函数,将返回的NULL值转换为0。例如,可将上述SQL语句改为:

    SELECT IFFULL(MAX(name),0) AS name FROM user WHERE id = #{id}

  • 1

四、int和integer的区别

  1. 以前一直没有思考,为啥要有一个int还要有一个integer。实际上:
  2. 1. Ingeterint的包装类,int的初值为0Ingeter的初值为null
  3. 2.初始化的时候,int i =1Integer i= new Integer(1);(要把integer 当做一个类看);但由于有了自动装箱和拆箱使得对Integer类也可使用:Integer i= 1;    
  4. 3.int 是基本数据类型(面向过程留下的痕迹,不过是对java的有益补充),Integer 是一个类,是int的扩展,定义了很多的转换方法
  5. 4.Integer是对象,用一个引用指向这个对象,而int是基本类型,直接存储数值。

五、小结

  1. java中的数据类型分为基本数据类型和复杂数据类型,int是基本数据类型,integer是复杂数据类型,复杂基本类型也就是一个类,所以初始化为null
  2. 从基本知识出发,能更加夯实自己的基础。

发表评论

表情:
评论列表 (有 0 条评论,213人围观)

还没有评论,来说两句吧...

相关阅读