org.ptions.TooManyResultsEx。。。
在查阅测试环境业务⽇志中的ERROR级别的⽇志时,发现了有⼀个Mybatis相关的异常错误org.ptions.TooManyResultsException: Expected one result (or
null) to be returned by selectOne(), but found: 13。
⼀、分析:详细堆栈信息打印如下
2018-09-30 17:30:17.548 ERROR 14146 --- [DubboServerHandler-172.16.44.184:20882-thread-136] [c.a.dubbo.rpc.filter.ExceptionFilter :85] : [DUBBO] Got unchecked and undeclared exception which called by 172.16.44.184. service: net.batis.spring.MyBatisSystemException: nested exception is org.ptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 13
batis.anslateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
batis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy78.selectOne(Unknown Source)
batis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
at org.apache.ibatis.ute(MapperMethod.java:82)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy84.selectOneByExample(Unknown Source)
at net.lingyejun.mall.product.SkuInfoById(ProductSkuServiceImpl.java:95)
flect.GeneratedMethodAccessor138.invoke(Unknown Source)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at ansaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at ansaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$SkuInfoById(Unknown Source)
at net.lingyejun.mall.product.SkuLiveStatusInfo(ProductDetailServiceImpl.java:122)
flect.GeneratedMethodAccessor178.invoke(Unknown Source)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at ansaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at ansaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)java dubbo
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$SkuLiveStatusInfo(Unknown Source)
at com.alibaba.dubbomon.bytecode.Wrapper2.invokeMethod(Wrapper2.java)
at com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:47)
at com.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:76)
at com.fig.invoker.DelegateProviderMetaDataInvoker.invoke(DelegateProviderMetaDataInvoker.java:52)
at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56)
at com.alibaba.dubbo.validation.filter.ValidationFilter.invoke(ValidationFilter.java:58)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
at com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:62)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
at com.itor.support.MonitorFilter.invoke(MonitorFilter.java:75)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
at com.alibaba.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:42)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
at com.alibaba.dubbo.rpc.filter.AccessLogFilter.invoke(AccessLogFilter.java:155)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:72)
at com.alibaba.dubbo.rpc.protocol.dubbo.ply(DubboProtocol.java:103)
at com.hange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:96)
at com.hange.support.ived(HeaderExchangeHandler.java:172)
at com.ived(DecodeHandler.java:51)
at com.ansport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:80)
at urrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at urrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.ptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 13
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:81)
flect.GeneratedMethodAccessor183.invoke(Unknown Source)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.Method.invoke(Method.java:498)
batis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
... 62 common frames omitted
⼆、根源:条件为Null则会忽略
检查数据库查询那⾥的代码后发现并⽆异样,但是这个堆栈信息⼜将错误⾮常明晰地指出,⼀定是Mybatis的SelectOne查询sql,查出了多条记录,但是在数据库表中skuId和
status对应的是唯⼀⼀条记录。难不成?有些条件没有⽣效,执⾏sql时将skuId这⼀查询条件去掉后发现查询出来的的确是13条记录。
Example example = new Example(ProductSku.class);
// 设置查询条件
Example.Criteria criteria = ateCriteria();
criteria.andEqualTo("skuId", skuId);
criteria.andEqualTo("status", ProductConst.DataStatus.NORMAL);
ProductSku productSku = productSkuMapper.selectOneByExample(example);
没错,当Mybatis的Criteria中的查询条件为Null时,则会忽略掉这个查询条件。即,skuId为空Null的时候就没有带skuId这个条件,直接是where status = 0;所以查出了多条记
录,像这样的查询条件需要在最外层做判空处理,接⼝层若缺少判空处理,则会出现此问题。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论