mapper method attempted to return null from a method with a primitive return type (int)
一、场景和背景
工具: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)相关推荐
- 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 ...
- 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)错误 本 ...
- 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 ...
- 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 ...
- 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 ...
- Mapper method attempted to return null from a method
org.apache.ibatis.binding.BindingException: Mapper method 'share.pmc.modular.ed.mapper.DeviceMapper. ...
- 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 ...
- attempted to return null from a method with a primitive return type (int).
错误产生的场景 dao层接口如下 int getResourceDataNumsByTitle(String title); mapper.xml 中sql语句如下 <select id=&qu ...
- 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). 解决 ...
最新文章
- linux 杀毒软件 clamav安装使用
- Jenkins FTP 上传
- Java 比较两个版本号的大小 (通用)
- ILSpy-替代.Net Reflector的工具
- redis的5种数据结构和基本操作
- TCP压测工具 终极 全新版
- Ubuntu18.04安装后检测不到集成声卡问题
- “舌战群儒”的技术分析
- w10恢复出厂设置_w10电脑恢复出厂设置 w10电脑恢复出厂设置的方法
- 东方证券万字报告:微信视频号进入稳定的发展期
- 电子邮件客户端程序设计与实现
- 32 任意项级数敛散性的判别法
- ViewPager中呈现前一页和后一页的内容
- 【数据分析】【MySQL】快速入门+案例+代码+命令整理+GIF实操演示
- *皮亚诺关于公理4的一段语录解析 皮亚诺读后之六
- InputStream转byte[]
- 《ucore lab1 练习5》实验报告
- JAVA 接口(interface)
- Python接口测试实战1(下)- 接口测试工具的使用
- SQL分组查询,结果只取最新记录