关于mybatis的报错 attempted to return null from a method with a primitive return type (int)
https://blog.csdn.net/kisscatforever/article/details/77801060
一、前言
在往常敲代码的时候没有留意过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)
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
三、解决方案
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>
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
*/
public Integer selectSumCountCapacity(List<String> roomIds ,String roomTypeId){
if (roomIds==null||roomTypeId==""){
logger.debug("查询未安排考场的教室总容量,已使用的房间id为空,将查询所有的房间容量");
}
return roomDao.selectSumCountCapacity(roomIds, roomTypeId);
}
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的区别
以前一直没有思考,为啥要有一个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。
从基本知识出发,能更加夯实自己的基础。
关于mybatis的报错 attempted to return null from a method with a primitive return type (int)相关推荐
- sql报错解决方案:attempted to return null from a method with a primitive return type (int)
问题现象 idea执行查询数量语句控制台报错:attempted to return null from a method with a primitive return type (int). 解决 ...
- Mybatis中的attempted to return null from a method with a primitive return type (int).异常
错误截图: org.apache.ibatis.binding.BindingException: Mapper method 'com.shop_demo.dao.AdminDAO.getAdmin ...
- Mybatis:Mapper method attempted to return null from a method with a primitive return type (int)
Mybatis中 Mapper method attempted to return null from a method with a primitive return type (int)错误 本 ...
- Mybatis异常错误:Mapper method attempted to return null from a method with a primitive return type (int)
在mybatis搭建时出现错误:Mapper method attempted to return null from a method with a primitive return type (i ...
- error:attempted to return null from a method with a primitive return type (int)
1,起因 今天老大让改bug,说有个模块数据提交不了,让我看看...检查服务器发现这个报错..attempted to return null from a method with a primiti ...
- mapper method attempted to return null from a method with a primitive return type (int)
一.场景和背景 工具:mybatis + pgsql dao代码: int selectMaxAgeBySex(String sex); 查询性别是女生的最大年龄 sql: <select id ...
- attempted to return null from a method with a primitive return type (int).
错误产生的场景 dao层接口如下 int getResourceDataNumsByTitle(String title); mapper.xml 中sql语句如下 <select id=&qu ...
- Mybatis-增删改查踩坑- attempted to return null from a method with a primitive return type (int).
错误 在编写插入数据时一直报如下错误: attempted to return null from a method with a primitive return type (int). 原来的代码 ...
- insertRole attempted to return null from a method with a primitive return type
练习myBatis,向数据库插入一条数据,期望返回1,结果报错. 多次查找,结果是Mapper对应的xml文件中sql语句的节点写成select了,应该是insert
最新文章
- puppet yum模块、配置仓储、mount模块
- TCP/IP协议三次握手流程
- Metaphors for a richer understanding of software development -- Code complete reading notes(2)
- python序列切片
- Java【第九篇】异常处理
- oracle日志存放默认位置,oracle——数据库日志存放位置
- oracle 序列赋值变量,Oracle变量的定义、赋值及使用
- 【Pytorch】LeNet的pytorch写法
- 深入浅出JMS(一)——JMS简要
- 毕设题目:Matlab手势识别
- WPS无法用backspace删除空白页怎么办?
- ppt中的流程图怎么整体移动_教你两招,把复杂的流程图PPT排版简单化
- 计算机科学家的名言警句,【实用】励志的格言70句
- 从新手到高手c++全方位学习 pdf + 视频教程 共18章
- 基于飞凌i.MX6Q-C核心板搭建3D相机
- ai人工智能开发_人工智能使Web开发的面貌发生变化
- FS2222可调过压过流芯片IC,40V耐压过压保护可调OVP可调OCP
- java对接海康威视SDk实现红外DL/T抓图
- 基于Springboot的民宿管理平台
- Windows共享内存解析
热门文章
- 读书笔记 | 为什么从世界500强CEO、政界要员,到著名演员都用这个方法来提高效率?
- 08_NandFlash驱动
- EmWin学习课堂_小白EmWin_EmWin快速入门_EmWin动态内存,显示和触摸屏_EmWin基础配置
- Mac 远程连接 Windows 桌面工具 Parallels Client 使用教程
- 如何解决打印CAD图纸不完整,有部分打印不出来
- 小番茄(Visual Assist X)常用快捷键
- OkHttp框架从入门到放弃,解析图片使用Picasso裁剪,二次封装OkHttpUtils,Post提交表单数据
- 51单片机 LED点亮、闪烁以及流水灯实现
- 一种Android分包策略推荐
- lm358集成电路参数资料 原厂资料 pdf datasheet 免费下载