mapper method 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的情况。 阅读详情

一、场景和背景

工具:mybatis + pgsql

dao代码:

int selectMaxAgeBySex(String sex); 查询性别是女生的最大年龄

sql:

<select id = "selectMaxAgeBySex" resultType="int">
	select max(age) from user where sex = '女';
</select>

二、错误原因

如果数据表user中没有性别是女的用户数据,那么pgsql返回的函数max值是null,此时把null赋值给了resultType=“int”,报错信息:

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

三、解决

pgsql数据库的max返回值有两种,null和数值。
当返回的数据是nu l l空对象时,即意味着把null赋值给int。如果用程序实现将会报空指针异常,例如:

package com.test.boke;

public class TestJava {
	public static void main(String[] args) {
		Integer integer = null;
		int num = integer;
		System.out.println("num = " + num);
	}
}

console输出:
Exception in thread "main" java.lang.NullPointerException
at com.test.boke.TestJava.main(TestJava.java:6)

所以不可以使用resultType="int"作为返回类型。正确的代码是:
dao代码:

Integer selectMaxAgeBySex(String sex); 查询性别是女生的最大年龄

sql:

<select id = "selectMaxAgeBySex" resultType="java.lang.Integer">
	select max(age) from user where sex = '女';
</select>
解决Mapper method ‘com.xxx‘ attempted to return null from a method with a primitive return type (xxx) 全向详细解决Mapper method 'com.xxx.mapper.AppP ageButtonMapper.updateSeq attempted to return null from a method with a primitive return type (int) (long) (float) (double) (char) (boolean) (short) (byte)的方法 阅读详情

相关推荐

【异常】Mapper method ‘‘attempted to return null from a method with a primitive return type (int)

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

奶爸的编程之路,也就一周冷个三天 434

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

RandLA-Net 调通代码1:Windows 11 + Tensorflow 2.4 + S3DIS数据集【踩坑+填坑】

TF不上进啊,学学pytorch

累了就要打游戏 2486

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万+

VMware Workstation Pro 17 高效安装与激活指南

本文详细介绍了VMware Workstation Pro 17的高效安装与激活方法,包括系统要求、安装步骤、常见问题解决方案及性能优化技巧。通过专业的指导,帮助用户快速掌握这款强大的虚拟机软件,提升工作效率。

weixin_29322855的博客 410

基于matlab的心电信号预处理

这是前段时间做的一个课程设计,做的比较简单,没有考虑到太细,只是初步地达到了想要的效果。这次设计主要是对心电信号进行预处理,将其信号中包含的一些干扰滤除或者抑制掉。 一、心电信号 (1)心电信号的特性   人体心电信号是非常微弱的生理低频电信号,通常最大的幅值不超过5mV,信号频率在0.05~100Hz之间。心电信号是通过安装在人体皮肤表面的电极来拾取的。由于电极和皮肤组织之间会发生极化现象

i_weimoli的博客 6万+

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

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

m0_62645012的博客 1618

AVL(Athena Vortex Lattice)如何编写与应用(一)

前两个是弦向的涡个数和以cos函数分布的间距参数(设为1即可)后两个可以在这里声明也可以写到后面的每一个section中,是展向的涡个数和以sin函数分布的间距参数。这个文件是用来建立飞机模型的文件,是一定要有的文件,其他的.run和.mass并不是必须要有的,甚至mass文件并没有看到有人在使用。Sref、Cref、Bref是飞机的参考面积参考弦长和参考的展长,我的理解就是所设计飞机的S,c,b这三个参数。———————————————以上是飞机的基本信息———————————————

dawangaa的博客 1522

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

resultType 限定的类型是 Integer ,所以框架抛了次异常。问题找到了,为了解决此异常,那就再聚合函数sum() 做文章,通过IFNULL() 函数进行为空 补0 就不会出现此异常了。通过其他函数 eg: group by 出现为空的情况也可以通过IFNULL() 进行改造。再结合异常信息,就可以发现,是因为通过SQL语句查询出来的数据为空,但Mybatis。

xu990128638的专栏 624

别再手动备份了!用Python脚本批量导出华为/华三交换机配置(附完整代码)

本文详细介绍了如何使用Python脚本实现华为/华三交换机配置的自动化备份,大幅提升网络运维效率。通过paramiko库建立SSH连接,结合pandas处理设备清单,实现配置的批量导出与规范化存储,解决传统手动备份耗时易错的问题。

weixin_28737603的博客 457

运动控制软件:Siemens Sinamics二次开发_(6).通信接口与协议详解

PROFIBUSPROFINETModbus TCPCANopenEtherCAT每种通信接口都有其特定的应用场景和优势。选择合适的通信接口可以显著提高系统的性能和可靠性。在选择通信协议时,需要根据具体的应用场景和需求综合考虑。每种通信协议都有其特定的优势和局限性,因此在设计运动控制系统时,应选择最适合的协议,以确保系统的性能、可靠性、成本和维护性。通过以上对比和分析,希望开发者能够更好地理解和选择适合的通信接口和协议,从而优化运动控制系统的开发和应用。

2401_87715305的博客 909

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)错误 本人是在进行插入操作的时候发生的错误,相应的删除和修改都是同样的道理 <select id="saveEmpInfo" resultType="int"> INSERT INTO emp(eId,eName,did) VALUES(#{eId},#{eName},#{did})

qq_43518782的博客 886

从被秒封到稳过Cloudflare!Canvas/WebGL/WebRTC多维度浏览器指纹隐身全实战

反爬和反反爬,本质上是一场持续的博弈,没有100%的隐身,只有不断升级的伪装。网上很多教程只给了个基础的噪声代码,却没讲透反爬的检测逻辑,导致你用了还是被封。这套方案的核心,不是简单的API重写,而是站在反爬的角度,模拟真实用户的所有特征,保证所有指纹的一致性、合理性、完整性。目前这套方案我用了半年,跑了上千万次请求,稳过国内国外99%的主流反爬平台,没有出现过一次因为指纹关联导致的批量封禁。大家有什么更狠的反爬场景,或者更好的伪装技巧,欢迎评论区一起交流。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1200

[Mybatis异常]Mapper method '...attempted to return null from a method with a primitive return type(int

Mapper method '...attempted to return null from a method with a primitive return type(int) 这是一个mybtis的映射雨具错误,原因是数据库中没有你想要查询的这个值 造成的原因是因为这样写的映射语句 因为没有结果,所以无法返回数值 这里我们要搞明白resultType和resultMap的关系 ...

INZT__的博客 8599

芒果YOLOv12改进13:独家原创改进最新 CFNet 核心结构|即插即用 | 首发改进显著提升检测性能,小目标检测涨点必备

芒果YOLOv12改进13:独家原创改进最新 CFNet 核心结构|即插即用 | 首发改进显著提升检测性能,小目标检测涨点必备

包括YOLOv5、YOLOv7、YOLOv8等模型改进 298

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

【errors】Mapper method attempted to return null from a method with a primitive return type (int).

问题 严重: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Mapper meth...

cxxdcyy 554

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

idea报错:Mapper method ……attempted to return null from a method with a primitive return type (int).

执行方法时却试图返回。

你若盛开,清风自来。 756

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

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

今天做一个密码修改的功能,发现报错:Mapper method attempted to return null from a method with a primitive return type (int). 然后折腾了一下午,发现是xml标签的问题, 如上标签, 1.先是标签对应,如需要更新,则使用<update></update>。 2.需要设置parameterType,resultType不需要也可以但是dao层的返回值需要是Integer或int类型。 ..

清风思雨的博客 1425

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)

qq_51869479的博客 1222

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

关键词 SpringMVC SSM Mybatis eclipse 报错内容 Mybatis:Mapper method attempted to return null from a method with a primitive return type (int) 原因 Mapper.xml的标签用错了。。。(低级错误,轻喷。。。)   比如本来应该: 插入语句用<insert></insert>,更新语句用<update></update>

向来萧瑟也无畏 309

Mapper method attempted to return null from a method with a primitive return type (int)错误解决

原因 Mapper.xml的标签用错了 比如本来应该: 插入语句用<insert></insert>,更新语句用<update></update>,删除语句用<delete></delete> 结果全用成<select></select> 解决 用回对应的标签就行。 ...

m0_56215275的博客 1477

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

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

lixianhe的博客 894

# Mapper method ‘...attempted to return null from a method with a primitive return type(int)SS整合错误

@Mapper method '…attempted to return null from a method with a primitive return type(int) 欢迎使用Markdown编辑器 你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。 新的改变 我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点

m0_46780117的博客 708

异常:Mapper method ... attempted to return null from a method with a primitive return type (int).

异常:Mapper method ... attempted to return null from a method with a primitive return type (int).

HCS11的博客 1204

Mapper method ‘xxx.dao.xxx‘ attempted to return null from amethod with a primitive return type (int

一、问题 报错结果: 二、造成错误代码 本人造成的错误是在dao/StudentDao.xml文件造成的,也就是持久层的dao配置文件写错了,下面是本人的错误代码: <select id="insertStudent"> insert into student1(name, age) values (#{name}, #{age}) </select> 造成错误的原因,这是新手容易犯的错误,将插入数据标签写成查询数据的标签,导致错误 三、解决方法...

Erudite_x的博客 979

Mysql中的笔试和面试---delete attempted to return null from a method with a primitive return type (int)问题解决

抛出问题 今天服务上线后,检查线上日志,发现如下错误: org.apache.ibatis.binding.BindingException: Mapper method 'org.system.mapper.child.ChmOrganizationExaminationMapper.delete attempted to return null from a method with a ...

FeelTouch Labs 905
上一篇: Java中的问号与冒号 ? : 表达式
下一篇: resultType="int" 和 resultType="java.lang.Integer"
脾气很倔呢
博客等级 码龄10年 192粉丝 309原创
评论 9
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值