TooManyResultsException: Expected one result to be returned by selectOne()

一、现象描述

MyBatis运行到查数据的时候报错:

org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 2
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:70)
	at com.queen.mybatis.MyBatisTest.test(MyBatisTest.java:45)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

二、解决方案

Debug进入源码发现,报错信息又源码中抛出来:

TooManyResultsException: Expected one result to be returned by selectOne()的照片 - 1

由错误提示可知,查询的结果是期望一条记录或者为空,但是返回的记过有2条。

那我们来分析一下造成这种问题是什么原因导致的呢?

比如:

  • 你调用的是selectOne方法,但是实际数据库根据条件查询到的有多条数据,那么这时候,你得考虑将selectOne方法换掉
  • 或者因为过滤条件不够精确导致查询数据变多,适当增加过滤条件增加唯一性。
  • 还有就是你确定根据这个条件只能查到一条数据,那么极有可能就是数据库数据问题,找出数据出错的原因,保证唯一性。

解决办法:

①将selectOne方法换成selectList

TooManyResultsException: Expected one result to be returned by selectOne()的照片 - 3

②或者将多余的数据清理掉

TooManyResultsException: Expected one result to be returned by selectOne()的照片 - 5

至此,我们关于MyBatis报错“TooManyResultsException: Expected one result (or null) to be returned by selectOne()”介绍完毕。想要了解更多关于MyBatis开发的教程,请参考http://www.marsitman.com/mybatis

博客地址:http://www.marsitman.com/exception/exception-mybatis-select-too-many.html
版权声明:本文为博主原创文章,允许转载,但转载必须标明出处。

 

 

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!



点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注