一、场景和背景

工具: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 attempted to return null from a method with a primitive return type (int)相关推荐

  1. MyBatis异常报告: Mapper method ‘com.dao.UserMapper.insert attempted to return null from a method with ……

    错误名称:Mapper method 'com.dao.UserMapper.insert attempted to return null from a method with a primitiv ...

  2. 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)错误 本 ...

  3. Mapper method ‘com.xxx.dao.ActivityDao.update attempted to return null from a method with a primiti

    500异常报告: Mapper method 'com.xxx.dao.ActivityDao.update attempted to return null from a method with a ...

  4. mybatis异常Mapper method attempted to return null from a method with a primitive re

    org.apache.ibatis.binding.BindingException: Mapper method  attempted to return null from a method wi ...

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

  6. Mapper method attempted to return null from a method

    org.apache.ibatis.binding.BindingException: Mapper method 'share.pmc.modular.ed.mapper.DeviceMapper. ...

  7. Mybatis中的attempted to return null from a method with a primitive return type (int).异常

    错误截图: org.apache.ibatis.binding.BindingException: Mapper method 'com.shop_demo.dao.AdminDAO.getAdmin ...

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

    错误产生的场景 dao层接口如下 int getResourceDataNumsByTitle(String title); mapper.xml 中sql语句如下 <select id=&qu ...

  9. sql报错解决方案:attempted to return null from a method with a primitive return type (int)

    问题现象 idea执行查询数量语句控制台报错:attempted to return null from a method with a primitive return type (int). 解决 ...

最新文章

  1. linux 杀毒软件 clamav安装使用
  2. Jenkins FTP 上传
  3. Java 比较两个版本号的大小 (通用)
  4. ILSpy-替代.Net Reflector的工具
  5. redis的5种数据结构和基本操作
  6. TCP压测工具 终极 全新版
  7. Ubuntu18.04安装后检测不到集成声卡问题
  8. “舌战群儒”的技术分析
  9. w10恢复出厂设置_w10电脑恢复出厂设置 w10电脑恢复出厂设置的方法
  10. 东方证券万字报告:微信视频号进入稳定的发展期
  11. 电子邮件客户端程序设计与实现
  12. 32 任意项级数敛散性的判别法
  13. ViewPager中呈现前一页和后一页的内容
  14. 【数据分析】【MySQL】快速入门+案例+代码+命令整理+GIF实操演示
  15. *皮亚诺关于公理4的一段语录解析 皮亚诺读后之六
  16. InputStream转byte[]
  17. 《ucore lab1 练习5》实验报告
  18. JAVA 接口(interface)
  19. Python接口测试实战1(下)- 接口测试工具的使用
  20. SQL分组查询,结果只取最新记录

热门文章

  1. 小红书日常实习一面面经
  2. arcsde mysql_怎么在SQLServer数据库创建多个ArcSDE服务
  3. Oracle 常规ArcSDE操作
  4. 20161027 记录
  5. 洛谷P4107 [HEOI2015]兔子与樱花
  6. JVM虚拟机详解(三)类加载器的分类
  7. 计算机的基础学科是什么,【讨论】计算机科学与技术是否可以算作‘基础学科’呢?...
  8. TP-四种url访问的方式
  9. SDK 和 API 的区别
  10. 坑爹的CY7C68013A调试指南