Spring中EmptyResultDataAccessException异常的处理方法
来源:互联网 发布:淘宝直通车收费贵 编辑:程序博客网 时间:2024/05/16 00:39
Spring中使用JdbcTemplate的queryForObject方法,当查不到数据时会抛出如下异常:
org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0org.springframework.dao.support.DataAccessUtils.(DataAccessUtils.java:71)org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:729)
查看DataAccessUtils的源码,找到requiredSingleResult方法:
public static <T> T requiredSingleResult(Collection<T> results) throws IncorrectResultSizeDataAccessException { int size = (results != null ? results.size() : 0); if (size == 0) { throw new EmptyResultDataAccessException(1); } if (results.size() > 1) { throw new IncorrectResultSizeDataAccessException(1, size); } return results.iterator().next(); }
发现当查询的result为空时,size赋值为0,并抛出EmptyResultDataAccessException异常,此为原因所在。猜测Spring这样设计可能是为了防止用户不对空值做出判断,保证了程序的健壮性。同时,当results的size大于1时同样会抛出异常,以保证返回单一的Object。
- Spring中EmptyResultDataAccessException异常的处理方法
- Spring中EmptyResultDataAccessException异常产生的原理及处理方法
- Spring-JdbcTemplate的queryForObject方法EmptyResultDataAccessException异常产生的原理及处理方法
- Spring EmptyResultDataAccessException异常的源码分析和解决方法
- Spring 中异常处理方法的总结
- Spring MVC的异常统一处理方法
- spring中对异常的统一处理
- Spring Boot & Spring MVC 异常处理的N种方法
- Spring Boot & Spring MVC 异常处理的 N 种方法
- PreparedStatement中setString方法的异常处理
- android中一些异常的处理方法
- java中异常处理的方法
- Java中常用的异常处理方法
- spring mvc3中异常处理
- spring mvc3中异常处理
- Delphi 中自定义异常及异常处理的一般方法
- dwr的基本使用,异常处理,和spring集成方法
- 使用Spring MVC统一异常处理的几个方法
- 分群源程序
- JAVA配置文件解析
- 在SQL Server Management Studio中可以运行作业但是用T-SQL运行则失败
- 云计算相关软件介绍
- mysql里面的时间函数
- Spring中EmptyResultDataAccessException异常的处理方法
- shell逻辑运算
- C++中虚函数的实现背后
- ubuntu下通过ufw配置nat
- errno perror strerror strerror_r
- centso5下使用coreseek(sphinx)建立搜索引擎
- /etc/profile 续
- HDU 1702 stack queue 的应用 大水题
- oracle control_files的创建