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文件, 在 ...
随机推荐
- 【Cubian】set up
源: http://mirrors.163.com/.help/debian.html https://lug.ustc.edu.cn/wiki/mirrors/help/debian 下载地址: h ...
- hdu 4291(矩阵+暴力求循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4291 思路:首先保留求出循环节,然后就是矩阵求幂了. #include<iostream> ...
- Android之背景颜色小知识(笔记)
一.ListView的item背景(自定义颜色) 通常情况下,ListView的item背景用的是图片资源,下面跟大家分享一下使用颜色资源,即自定义一种颜色,当item聚焦.按压.选择的时候,可以显示 ...
- nginx的allow和deny配置
转自:http://www.ttlsa.com/linux/nginx-modules-ngx_http_access_module/ 单看nginx模块名ngx_http_access_module ...
- onbeforeunload 适用DOM 0级,不适用 DOM 2级
你可以在控制台试下: window.addEventListener("beforeunload", function () { return 'ss'; }); 我这里 chro ...
- Maven的安装配置及初次创建项目与java单元测试工具JUnit
Maven 安装 1.把maven安装包解压到某个位置 2.配置M2_HOME环境变量指向这个位置 3.在path环境变量中添加;%M2_HOME%\bin 配置镜像 国内的阿里云镜 ...
- Python全栈day19(函数补充)
一,深浅拷贝 看拷贝列子day19-1.py s=[1,'zhangsan','lisi'] #s2是s的拷贝 s2=s.copy() #打印s2和s是一样的 print(s2) #修改s2 s2[0 ...
- (N)IO Frameworks in Java
(N)IO Frameworks in Java – Thread.currentThread.join() https://www.ashishpaliwal.com/blog/2008/10/ni ...
- dstat命令--检查linux系统性能
系统性能的相关工具:vmstat.netstat.iostat.ifstat. dstat命令可以查看虚拟内存.网络连接和接口.cpu活动.输入/输出设备等.
- Zipline Trading Calendars
Trading Calendars What is a Trading Calendar? 什么是交易日历? A trading calendar represents the timing info ...