您的位置:首页 > 编程语言 > Java开发

[置顶] 【java】Mybatis返回int类型为空时报错 attempted to return null from a method with a primitive return type (int)

2017-09-02 14:49 471 查看

一、前言

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

二、报的错误

试图从具有原始返回类型(int)的方法返回null

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).

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)


三、解决方案

Mybatis的mapper文件的返回类型resultType为Integer:

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

<!--查询未安排考场的教室总容量-王雷-2017年9月1日15:09:25-->
<select id="selectSumCountCapacity"  resultType="Integer">
SELECT
IFNULL( SUM(room_capacity),0)
FROM
t_room
<if test="roomTypeId !=null">
WHERE
roomType_id = #{roomTypeId}
</if>
<if test="roomIds !=null and roomIds.size()>0">
AND id NOT IN (

<foreach collection="roomIds" item="item" separator=",">
#{item}
</foreach>

)
</if>
</select>


Service之间的返回值也是Ingeger:

/**
* 查询未安排考场的教室总容量-王雷-2017年9月1日15:09:25
* @param roomIds
* @param roomTypeId
* @return
*/
public Integer selectSumCountCapacity(List<String> roomIds ,String roomTypeId){
if (roomIds==null||roomTypeId==""){
logger.debug("查询未安排考场的教室总容量,已使用的房间id为空,将查询所有的房间容量");
}

return roomDao.selectSumCountCapacity(roomIds, roomTypeId);
}


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

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


四、int和integer的区别

以前一直没有思考,为啥要有一个int还要有一个integer。实际上:

1. Ingeter是int的包装类,int的初值为0,Ingeter的初值为null;

2.初始化的时候,int i =1;Integer i= new Integer(1);(要把integer 当做一个类看);但由于有了自动装箱和拆箱使得对Integer类也可使用:Integer i= 1;    

3.int 是基本数据类型(面向过程留下的痕迹,不过是对java的有益补充),Integer 是一个类,是int的扩展,定义了很多的转换方法

4.Integer是对象,用一个引用指向这个对象,而int是基本类型,直接存储数值。

五、小结

java中的数据类型分为基本数据类型和复杂数据类型,int是基本数据类型,integer是复杂数据类型,复杂基本类型也就是一个类,所以初始化为null。

从基本知识出发,能更加夯实自己的基础。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐