1. 程式人生 > >SSM時遇到的異常(5)——TooManyResultsException: Expected one result (or null) to be returned by selectOne(),

SSM時遇到的異常(5)——TooManyResultsException: Expected one result (or null) to be returned by selectOne(),

JDK1.7+Tomcat7.0+Eclipse+SSM 

嚴重: Servlet.service() for servlet [SpringMVC] in context with path [/MVCQ] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 2] with root cause
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:63)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:355)
	at com.sun.proxy.$Proxy14.selectOne(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:160)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:95)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:40)
	at com.sun.proxy.$Proxy15.empByNameAndPwd(Unknown Source)
	at com.qiu.service.EmpService.login(EmpService.java:56)
	at com.qiu.service.EmpService$$FastClassBySpringCGLIB$$f54dc904.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
	at com.qiu.service.EmpService$$EnhancerBySpringCGLIB$$b12e93a5.login(<generated>)
	at com.qiu.controller.LoginController.login(LoginController.java:189)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

我都被弄得莫名其妙!!!太多Result導致異常???最終,我換個瀏覽器,頁面正常。。。。。。

當然報錯,不可能,無緣無故 T_T !!!

1.一開始

因為,

程式碼(list和find沒有合併,也就是"查詢"和"列表"不在同一個方法):

2.接著

(前提:在合併之前,login頁面和EmpList頁面都是執行正常,只是“當年齡輸入框中沒有輸入值時,點選查詢按鈕會立即報400”這樣的問題)

所以為了不報400、方便,試著將“list”和“find”方法,合併:

擴充套件:SpringMVC的controller向jsp傳遞資料的五種方式詳解
【ModelMap、Map與Model效果差不多,但還是存在區別

3.再接著

合併之後,我想著先從"登入"頁面轉向"員工列表"頁面,所以我先重新整理,從login.jsp進入,

當我點選“登入”按鈕後,就報這樣的異常!!!接著,把改了的程式碼都“撤消”!!!還是繼續這樣的報錯!!!再接著,我重啟Tomcat幾次,再以相同的方式進去,錯誤繼續顯示出來!!!我重啟Eclipse幾次,再以相同的方式進去,錯誤繼續顯示出來!!!

 再再接著,我只能,URL欄,輸入“http://localhost:8080/MVCQ/emp/list”,並沒有跳出異常!!!login頁面沒錯,list()方法轉向EmpList頁面也沒錯,

我就納悶了,怎麼會這樣,在該瀏覽器(2345加速瀏覽器),換了視窗,再以相同的方式進去,錯誤繼續顯示出來!!!我百度,搜的這些文章,都可以參考,

(1)Spring mvc+mybatis框架下的TooManyResultsException異常【將方法Bean型別改為List<Bean>型別】

(2)

MyBatis報TooManyResultsException: Expected one result to be returned by selectOne()異常【將selectOne方法換成selectList或將多餘(指empName或name相同的)的資料清理掉】

或者

mybatis中的一個異常:org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to

或者

MyBatis的TooManyResultsException異常的解決辦法

可是對於我,也就只能看看,卻不能解決我的問題,因為最後:我換其他瀏覽器(谷歌),以相同的方式進去,正常顯示EmpList頁面!!!

SelectOne原始碼學習(異常中顯示的selectOne()函數了解一波)

Mybatis selectOne方法執行sql有資料返回為null【select標籤增加了resultMap屬性】

4.最後的最後

400異常我已解決 點選藍色字,立馬轉向對應文章