org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1,actual 0 1

本文详细解析了Spring中使用JdbcTemplate的queryForObject方法时遇到的数据查询异常问题,介绍了该异常产生的原因及其解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  spring中使用JdbcTemplate的queryForObject方法,当查不到数据时会抛出如下异常:

	org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
	org.springframework.dao.support.DataAccessUtils.(DataAccessUtils.java:71)
	org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:729)

        查看DataAccessUtils的源码,找到requiredSingleResult方法:

  1. public static <T> T requiredSingleResult(Collection<T> results) throws IncorrectResultSizeDataAccessException {  
  2.         int size = (results != null ? results.size() : 0);  
  3.         if (size == 0) {  
  4.             throw new EmptyResultDataAccessException(1);  
  5.         }  
  6.         if (results.size() > 1) {  
  7.             throw new IncorrectResultSizeDataAccessException(1, size);  
  8.         }  
  9.         return results.iterator().next();  
  10.     }  

        发现当查询的result为空时,size赋值为0,并抛出EmptyResultDataAccessException异常,此为原因所在。猜测Spring这样设计可能是为了防止用户不对空值做出判断,保证了程序的健壮性。同时,当results的size大于1时同样会抛出异常,以保证返回单一的Object。


两种方案:

  1. 用queryForLis或者query(可以使用in关键字查询同时可以自定义查询数据映射bean)t方法替换queryForObject或者queryForMap,因为这两个方法必须要有值,不能为空。    List<ZwMultColumnAccountVo> vos = baseDao.getNamedParameterJdbcTemplate().query(sql, parameterSource, new BeanPropertyRowMapper(ZwMultColumnAccountVo.class));

  2. 把这个异常捕获,用try/catch,如下


向左转 | 向右转

从orcale更换到mysql,springbatch运行偶尔会报错: org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0 at org.springframework.dao.support.DataAccessUtils.nullableSingleResult(DataAccessUtils.java:97) at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:784) at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:809) at org.springframework.batch.core.repository.dao.JdbcJobExecutionDao.synchronizeStatus(JdbcJobExecutionDao.java:308) at org.springframework.batch.core.repository.support.SimpleJobRepository.update(SimpleJobRepository.java:166) at sun.reflect.GeneratedMethodAccessor147.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:366) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:99) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) at com.sun.proxy.$Proxy85.update(Unknown Source) at sun.reflect.GeneratedMethodAccessor147.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
03-28
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值