一、问题描述

今天发现测试环境报出来一个数据库相关的错误 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).的更多相关文章

  1. 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. ...

  2. org.apache.ibatis.binding.BindingException

    1.异常提示: org.apache.ibatis.binding.BindingException: Mapper method 'com.artup.dao.WorksDao.selectWork ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. ssm 出现 Method threw 'org.apache.ibatis.binding.BindingException' exception.Invalid bound statement (not found)……

    运行数据库的增删改查时出现 500状态码 并且提示 Method threw 'org.apache.ibatis.binding.BindingException' exception.Invali ...

  9. 项目启动报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.wuhongyu.mapper.OrdersMapper.selectByExample

    在用maven配置mybatis环境时出现此BindingExceptiony异常,发现在classes文件下没有mapper配置文件,应该是maven项目没有扫描到mapper包下的xml文件, 在 ...

随机推荐

  1. GitHub搭建个人网站续

    来源:http://www.cnblogs.com/crazyacking/p/4678976.html http://www.cnblogs.com/crazyacking/category/716 ...

  2. HttpServletRequest和HttpServletResponse简介

    http://blog.csdn.net/tong_xinglong/article/details/12972819

  3. Ubuntu 16.04 LTS nodejs+pm2+nginx+git 基础安装及配置环境(未完,未整理)

    -.Ubuntu 安装nodejs 以下内容均在命令行,完成,首先你要去你电脑的home目录:cd ~. [sudo] apt-get update [sudo] apt-get upgrade ap ...

  4. Java千百问_05面向对象(011)_引用传递和值传递有什么差别

    点击进入_很多其它_Java千百问 1.什么是值传递 值传递,是将内存空间中某个存储单元中存放的值,传送给还有一个存储单元.(java中的存储单元并不是物理内存的地址,但具有相关性) 比如: //定义 ...

  5. Visual Studio 32位64位的问题和如何编译32位64位工程的问题

    Visual Studio自身没有32位和64位的分别,对于某一个特定的版本只有一个版本安装文件(即不存在32位版本的VS2015安装文件和64位版本的VS2015安装文件) 对于自己开发的工程,编译 ...

  6. ios8推送问题

    博文转载至  http://blog.csdn.net/cerastes/article/details/39546625 ios8push推送通知适配 ios8推送问题 registerForRem ...

  7. Java四种会话跟踪技术

    1.cookie:2.session:3.隐藏表单域:4.URL重写

  8. handlebears使用

    Handlebars 文档笔记: http://www.ghostchina.com/handlebars-wen-dang-bi-ji/ Handlebars模板引擎中的each嵌套及源码浅读: h ...

  9. maven仓库加载本地包依赖

    如果有个jar包是我们自己打的,怎么放到maven中呢? 首先在项目里面新建一个lib目录,如果有lib目录则不需要新建,然后放自己的jar包进去,maven的pom.xml配置是: <depe ...

  10. js根据数组对象中某个元素合并数组

    一个数组,根据数组中某个元素,合并数组 // 需要被合并的数组,把Index相同的数组合并 const arr = [{id:0,name:'张三'}, {id:0,name:'李四'}, {id:1 ...