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

【异常】BindingException: attempted to return null from a method with a primitive return type (int). 具体来说,这个方法的预期返回类型是基本数据类型int,但是MyBatis在尝试执行这个方法时却得到了一个null值。在你的代码中,com.xxx.ScenarioCardMapper.selectMyCount方法试图返回一个null值,但是这个方法的返回类型是int,这是基本类型,不能接受null值。如果数据库查询可能返回null,你可能需要更改该方法的返回类型为Integer,或者在返回前进行null检查。在相应的服务层代码中,也需要对返回的Integer值进行检查,以确定是否需要处理null的情况。 阅读详情

一、前言

      在往常敲代码的时候没有留意过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。

      从基本知识出发,能更加夯实自己的基础。

【异常】Mapper method ‘‘attempted to return null from a method with a primitive return type (int) 选择适合您具体情况的方法来处理这个问题,并根据需要调整您的代码。这样可以确保您的应用程序既符合业务需求,又能避免运行时错误。这可能是由于未预料到的数据状态或逻辑错误。,那么您需要确保当没有合适值返回时,提供一个默认值,如0。这个错误信息表明在使用MyBatis的Mapper接口。尝试从一个应该返回基本数据类型(在此例中是。是Java中的基本数据类型,它不能持有。:评估为什么您的方法试图返回。:如果您的业务逻辑允许返回。值,可以将方法的返回类型从。:如果您希望保持返回类型为。值,因此导致了此错误。 阅读详情

相关推荐

Mybatis中报错:attempted to return null from a method with a primitive return type (int)

其他文件就不在这里赘述了,有User类,自己抽取的SqlSessionUtils类,以及配置文件等等。

shengyin714959的博客 625

attempted to return null from a method with a primitive return type (int).NullPointerException

return null from a method with a primitive return type (int).(试图从具有原始返回类型(int)的方法返回null)本次报错的原因在于sql语句未查询到数据,返回为null。Ingeter是int的包装类,int的初值为0,Ingeter的初值为null。将dao层的返回类型改为Integer即可。

weixin_43709291的博客 3961

Nest的限流方案

多维度限流(如 IP、用户 ID、接口级别、请求类型)可配置化(支持不同环境和接口动态配置)可观测性(接入日志、监控系统)高性能和分布式支持(Redis 限流器)与鉴权鉴权链路集成(结合 JWT、API Token)以下是一个“标准// 优先按用户ID限流,否则按IP在main.ts维度建议限流存储推荐 Redis 支持多实例限流 Key 设计支持 IP + 用户ID + 路由等多种维度限流策略精细化配置,接口粒度调整异常处理自定义响应体、日志告警观测性。

Bruce__taotao的博客 1207

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

错误原因分析 本次报错的原因在于sql语句未查询到数据,返回为null。而我们定义的dao层方法是返回为int,就会出现如下这样的提示: return null from a method with a primitive return type (int).(试图从具有原始返回类型(int)的方法返回null) Ingeter是int的包装类,int的初值为0,Ingeter的初值为null 解决方式: 将dao层的返回类型改为Integer即可。 转载: https://blog.csdn.net/ir

weixin_45723046的博客 1万+

在 Ubuntu 上安装和配置 PostgreSQL 实录

本文介绍了在Ubuntu系统上安装和配置PostgreSQL数据库的完整步骤。主要内容包括:1)通过官方源或PPA源安装最新版本;2)修改配置文件(postgresql.conf和pg_hba.conf)以允许远程访问;3)设置postgres用户密码;4)创建新用户并配置权限;5)防火墙设置和重启服务;6)验证安装和远程连接方法。文章提供了详细的命令行操作指导,帮助用户完成从安装到远程访问的全部配置过程。

qq_58168857的博客 1220

AI智能体丨使用Coze一键生成中式养生视频,39 条作品涨粉 37.9 万!(附保姆级教程)

本文介绍如何利用扣子(Coze)AI智能体工作流高效制作中式养生短视频。该方案通过AI自动生成养生文案、古风插画素材、语音合成和视频剪辑,将传统制作流程从2-3小时缩短至1-2分钟。工作流包含文案创作、音频生成、图片素材制作和视频合成四大模块,采用大模型处理各环节内容,最终输出可直接导入剪映的草稿文件。特别适合希望快速批量制作文化氛围感强、兼具干货与视觉效果的养生内容创作者,效率提升近百倍。

李同学Lino的博客 3038

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

一、前言 在往常敲代码的时候没有留意过int和Integer的区别,今天在敲代码的时候,ORM框架使用的是Mybatis,一个简单的查询,返回查询的条数。当查询为null的时候,就报错了。 二、报的错误 试图从具有原始返回类型(int)的方法返回null org.apache.ibatis.binding.BindingException: Mapper method 'com.dmsdbj.it...

weixin_43060102的博客 617

手把手完整实现STM32+NB-IoT+MQTT+ThingsCloud云平台+手机APP(Android或iOS)及微信小程序及Web app 应用

本教程以远程温湿度显示及点灯作为示例。最终效果实现本地OLED显示屏、云平台和手机APP、微信小程序和Web App可远程查看温湿度数据,并可远程控制LED灯亮灭。以此完成基于MQTT协议的NB-IoT远程发布订阅数据,以此在本教程基础上可扩展开发更多的功能。

嵌入式阿齐Archie 2万+

【Mybatis报错】attempted to return null from a method with a primitive return type (int).

错误产生的场景 dao层接口如下 int getResourceDataNumsByTitle(String title); mapper.xml 中sql语句如下 <select id="getResourceDataNumsByTitle" parameterType="java.lang.String" resultType="java.lang.Integer"> ...

kun blog 5万+

手把手教你学PCIE(1.38)--基本概念:ATU(地址转换单元)

ATU(Address Translation Unit)是一种硬件单元,用于在PCIe系统中进行地址转换。它将设备的本地地址(外部地址)转换为系统内存或配置空间中的全局地址(内部地址),反之亦然。ATU的工作原理:通过地址匹配和转换,确保事务能够在不同的地址空间之间正确路由。配置寄存器:通过配置寄存器来设置地址转换规则。应用场景:地址空间隔离、访问控制、动态地址管理、虚拟化和多租户。

MHD0815的博客 2066

Y-Branch and a closed y-branch loop

双臂后面的区域,我们将它的双臂使用一个波导进行了连接,使得它本身形成了一个闭合回路,因此,该结构被称作。它们均是用来对经过它们的光束能量进行分割的一种器件。首先,我们知道一点,在光波导层面,今天给大家介绍一下,什么是。

u011699626的博客 641

mybatis下返回类型为int,结果为null时报tempted to return null from a method with a primitive return type (int).

背景了解: 从mysql数据库中查询数据,用Int接收,因为数据库没有数据所以返回null,于是运行时报以下错误,提取关键的信息“attempted to return null from a method with a primitive return type (int).”,翻译成中文大概意思是“”尝试从具有基本返回类型(Int)的方法返回null“返回int的方法想要返回null,不符合...

邓商羽的博客 3968

STC单片机 普通 I/O 口中断功能介绍和使用

STC32单片机 普通 I/O 口中断功能介绍和使用

perseverance51博客 3051

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 = "selectMaxAgeBySex" resultType="int"> select max(age) from user where sex = '女'; </select...

expect521的博客 2万+

一文读懂 SAP ICF Services:从 SICF 激活到 ABAP OData V2/V4 安全与实战

本文系统化介绍了ABAP系统中ICF服务的关键概念及实践要点。主要内容包括:ICF服务的核心安全机制,需显式激活才能响应HTTP请求;在SICF中正确激活服务的操作步骤;OData V2/V4服务所需的不同ICF节点配置,如V4统一入口节点/sap/opu/odata4;典型问题如default_host未激活会导致HOST_INACTIVE错误;安全实践包括CSRF防护机制和权限对象(S_ICF_ADM)配置;以及实际案例分析和常见ICF服务节点的功能说明。文章为ABAP开发者与运维人员提供了ICF服务管

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 486

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

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

一、前言 在往常敲代码的时候没有留意过int和Integer的区别,今天在敲代码的时候,ORM框架使用的是Mybatis,一个简单的查询,返回查询的条数。当查询为null的时候,就报错了。 二、报的错误 试图从具有原始返回类型(int)的方法返回null attempted to return null from a method with a primitive...

给自己一个smile 2290

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Mapper method ‘xxx‘ attempted to return null from a method with a primitive return type (int)

select 查询不到数据就会返回null ,但是int 是不接受null的,所以出现这个情况。将返回值改成Integer。

m0_62645012的博客 1618

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

attempted to return null from a methodwith a primitive return type (int).及count(*)返回null的解决

首先是报出 Mapper method 'com.xxx.mapper.BrandMapper.selectTotalCount attempted to return null from a method with a primitive return type (int). 的错误,在CSDN上搜索相关的内容,发现是将mapper.xml和mapper.java中的变量设置为int,后改为Interger后控制台不会报出上述错误,但是selectTotalCount返回的值始终是null。

qq_51364134的博客 2697

java integer a报错_【java】Mybatis返回int类型为空时报错 attempted to return null from a method with a primitive r...

一、前言在往常敲代码的时候没有留意过int和Integer的区别,今天在敲代码的时候,ORM框架使用的是Mybatis,一个简单的查询,返回查询的条数。当查询为null的时候,就报错了。二、报的错误试图从具有原始返回类型(int)的方法返回nullorg.apache.ibatis.binding.BindingException: Mapper method 'com.dmsdbj.itoo.b...

weixin_28738165的博客 335

Mybatis报错attempted to return null from a method with a primitive return type (int)(已解决)

一、出现这个bug的原因 出现这个bug的原因是因为MyBatis的Mapper.xml文件的查询语句返回的结果是Integer类型,而在对应的接口里面的方法里返回参数的类型是int。如果这个查询语句查询出来的结果是null,则会报这个错误,最简单的解决办法就是返回值的类型统一用Integer类型,或者在sql语句里面用ifnull()的函数,当查询结果是null时,给个默认值0。 二、错误示范 ...

weixin_43830606的博客 6213

关于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)的方法返回n...

qq502948261的博客 1192
上一篇: 【微信】微信消息定时推送
下一篇: 【DB】MySQL傻瓜安装,超简单
你个佬六
你个佬六 新星创作者: Java技术领域 新星创作者: Java技术领域 领域专家: 后端开发技术领域 领域专家: 后端开发技术领域
博客等级 码龄12年 1万+粉丝 422原创
评论 13
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

打赏作者

你个佬六

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

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

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

打赏作者

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

抵扣说明:

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

余额充值