error:attempted to return null from a method with a primitive return type (int)

博主接到老大修改bug的任务,某模块数据无法提交,检查服务器发现报错‘attempted to return null from a method with a primitive return type (int)’。原因是MyBatis中resultType返回int类型,无查询结果返回null导致报错。解决方法一是将int改为integer类型,二是用mysql的ifnull和max函数将null转成0。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,起因

今天老大让改bug,说有个模块数据提交不了,让我看看。。。检查服务器发现这个报错。。attempted to return null from a method with a primitive return type (int)

2,报错原因

mybatis中resultType返回数据为int类型,而因为没有查询结果返回为null,int初始化为0,不能为空,所以报了这个错误。

3,解决方法

(1)可以通过修改返回数据类型,将int改为integer类型,integer初始化是可以为null的,所以可以解决该问题.

(2)可以使用mysql的ifnull函数和max函数将null转成0.如下:

select IFNULL(MAX(xspx),0)  from user where ID = #{id}

 

==> Preparing: UPDATE mb_advertisement set ads_status=3 where endtime < SYSDATE() and ads_status in (0, 2) ==> Parameters: ==> Parameters: null <== Updates: 0 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@20339173] 2025-03-18 11:36:03.854 [INFO ] [task-scheduler-5] [com.corpgovernment.advertisement.job.AdsScheduled:42] - end outOfDateJob! <== Total: 0 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4fc55b9b] 2025-03-18 11:36:06.342 [INFO ] [http-nio-9898-exec-5] [com.corpgovernment.common.aspect.LogAspect:81] - 当前请求requestId为null,重新初始化值,并记录当前方法名称:weiXinAuthLogin,requestId:20250318033606b493e2c42c42446196b1958ca0367abc 2025-03-18 11:36:06.343 [INFO ] [pool-2-thread-10] [com.corpgovernment.common.util.ThreadPoolService:57] - 调用weiXinAuthLogin方法日志打印: method=weiXinAuthLogin token= REQUEST_ID=20250318033606b493e2c42c42446196b1958ca0367abc 【类】 class com.corpgovernment.common.aspect.LogAspect 【信息】 【class】 com.corpgovernment.basic.controller.AuthWXLiteController 【method】 weiXinAuthLogin 【headerToken】 null 【输入参数】 {"code":"0b16IFll2hEDff4Q02ml2FEOAh16IFlB"} 【输出参数】 JSON.toJSONString(result) 【耗时】 38625毫秒 2025-03-18 11:36:06.343 [ERROR] [http-nio-9898-exec-5] [com.corpgovernment.basic.handler.UnifiedExceptionHandler:127] - Mapper method 'com.corpgovernment.basic.mapper.MbUserLoginMapper.getCorpTypeByCorpCode attempted to return null from a method with a primitive return type (int). org.apache.ibatis.binding.BindingException: Mapper method 'com.corpgovernment.basic.mapper.MbUserLoginMapper.getCorpTypeByCorpCode attempted to return null from a method with a primitive return type (int). at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:102) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58) at com.sun.proxy.$Proxy229.getCorpTypeByCorpCode(Unknown Source) at com.corpgovernment.basic.impl.AuthWxLiteServiceImpl.weixinAuthLogin(AuthWxLiteServiceImpl.java:67) at com.corpgovernment.basic.controller.AuthWXLiteController.weiXinAuthLogin(AuthWXLiteController.java:36) at com.corpgovernment.basic.controller.AuthWXLiteController$$FastClassBySpringCGLIB$$ee4b9aff.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:100) at com.corpgovernment.common.aspect.LogAspect.around(LogAspect.java:56) 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:498)
最新发布
03-19
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

younha66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值