org.apache.ibatis.binding.BindingException: Mapper method 'attempted to return null from a method with a primitive return type (long).
一、问题描述
今天发现测试环境报出来一个数据库相关的错误 org.apache.ibatis.binding.BindingException: Mapper method 'attempted to return null from a method with a primitive return type (long).
二、问题根源
经过查询后发现,Mybatis 在查询id信息的时候返回类型为long ,没有留意long和Long的区别。
- Long是long的包装类,long是基本数据类型。
- Long可以为null,但基本类型long则不可以被赋值null。
当在数据库中查询没有查到这条记录,注意这里是根本没有这条记录,所以当然也不会返回id,对于这种情况Mybatis框架返回结果是null
@Select("select id from user where name = #{userName} and status = 1")
long getInitialPolicyIdByVer(@Param("userName") String name);
用long取承接null当然是不可以的
Long a = null;
long b = a;
因为会报java.lang.NullPointerException,而框架报出来的就是attempted to return null from a method with a primitive return type (long)
三、解决方案
方案一:将返回类型修改为Long就可以了,并在对应的Service层做相应的判断就可以了
public long getUserId(String userName) {
Long userId = userMapper.getUserId(userName);
if (userId == null) {
return 0;
}
return userId;
}
方案二:对于可以查询到记录的,只是在该记录中你需要的字段是null这种情况下,除了上述方法,还可以通过修改sql来解决。
select ifnull(id,0) from user where name = 'test' and status = 1;
select case id when null then 0 end from user where name = 'test' and status = 1;
但是站在专业的角度一般在设计数据库时,相关字段都会被设置为NOT NULL DEFAULT ''
org.apache.ibatis.binding.BindingException: Mapper method 'attempted to return null from a method with a primitive return type (long).的更多相关文章
- rg.apache.ibatis.binding.BindingException: Mapper method 'com.dao.Cameao.getOnlineDayRation attempted to return null from a method with a primitive return type (float)
本文为博主原创,未经允许不得转载: 异常展示如下: org.apache.ibatis.binding.BindingException: Mapper method 'com.dao.Cameao. ...
- org.apache.ibatis.binding.BindingException
1.异常提示: org.apache.ibatis.binding.BindingException: Mapper method 'com.artup.dao.WorksDao.selectWork ...
- springboot2 中Druid和ibatis(baomidou) 遇到org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.iflytek.pandaai.service.multi.mapper.TanancyMapper
调用mapper中任何方法都会出现类似的错误 org.apache.ibatis.binding.BindingException: Invalid bound statement (not foun ...
- IDEA+Maven+Mybatis 巨坑:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rao.mapper.UserMapper.findAll
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.rao.mapper.User ...
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.e3mall.search.mapper.ItemMapper.getItemList
java.lang.RuntimeException: org.apache.ibatis.binding.BindingException: Invalid bound statement (not ...
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.shen.mapper.UserMapper.findById
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.shen.mapper.Use ...
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.bw.mapper.BillMapper.getBillList at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:225
这个错误是没有找到映射文件 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.b ...
- ssm 出现 Method threw 'org.apache.ibatis.binding.BindingException' exception.Invalid bound statement (not found)……
运行数据库的增删改查时出现 500状态码 并且提示 Method threw 'org.apache.ibatis.binding.BindingException' exception.Invali ...
- 项目启动报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.wuhongyu.mapper.OrdersMapper.selectByExample
在用maven配置mybatis环境时出现此BindingExceptiony异常,发现在classes文件下没有mapper配置文件,应该是maven项目没有扫描到mapper包下的xml文件, 在 ...
随机推荐
- Mac 文件读写权限问题 OSError: Operation not permitted
Mac在OS X 10.11以后加入了Rootless功能,主要是限制了root权限,阻止用户对部分路径下的目录进行更改.受到限制的有以下目录: /System /bin /sbin /usr (ex ...
- [转]ASP.NET MVC 5 - 视图
在本节中,你要去修改HelloWorldController类,使用视图模板文件,在干净利索地封装的过程中:客户端浏览器生成HTML. 您将创建一个视图模板文件,其中使用了ASP.NET MVC 3所 ...
- Android API Guides---Bluetooth
Bluetooth Android平台包含蓝牙网络协议栈,它同意设备以无线方式与其他蓝牙设备进行数据交换的支持.应用程序框架提供了訪问通过Android蓝牙API的蓝牙功能.这些API使应用程序无线方 ...
- Linux命令之乐--find
find是命令行工具箱中最棒的命令之一. 列出当前目录及其子目录中的文件和文件夹. [root@LAMP WebRoot]# find . -print../index.jsp./upload.jsp ...
- asynDBCenter(不断跟新)
GameServer以前访问DBcenter时同步的,这样服务器都要等待DBcenter返回结果,经理在DBcenter和GameServer之间加了一个asynDBCenter,就实现了异步,感觉还 ...
- android签名,制作key
签名具体步骤: Apk签名首先要有一个keystore的签名用的文件. keystore是由jdk自带的工具keytool生成的.具体生成方式参考一下: 开始->运行->cmd->c ...
- PHP HTTP协议(报头/状态码/缓存)
一.HTTP协议介绍 1. #HTTP协议 # (1 建立在TCP/IP协议基础上 # (2 web开发数据传输依赖于http协议 # (3 http 协议全称是文 ...
- excel 使用技巧
计算两个日期的差值 1.计算当前日期与目标日期相差天数,下面例子中当前日期未2017/3/19 2.列增加数据条
- Zabbix监控介绍及安装配置
什么是zabbix zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵 ...
- Incompatible integer to pointer conversion assigning to 'NSInteger *' (aka 'int *') from 'NSInteger' (aka 'int')
遇到这样的问题: integer to pointer conversion assigning to 'NSInteger *' (aka 'int *') from 'NSInteger' (ak ...