mybatis 异常: Invalid bound statement (not found),TooManyResultsException:Expected one result (or null) to be returned by selectOne(), but found

时间:2021-01-31 20:56:49

 Invalid bound statement (not found): cn.com.dao.UserDao.selectByUser
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:178)
    at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:38)
    at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:49)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:42)
    at $Proxy22.selectByUser(Unknown Source)

Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for cn.com.dao.UserDao.selectByUser

    at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:775)
    at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:615)
    at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:608)
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:176)

    ... 34 more

 

我检查了半天也没看出什么问题来,后来把mapper的xml select 语句重写。问题竟然好了。

出了这种问题:

1.

<mapper namespace="cn.com.dao.UserDao">  

mapper的namespace写的不对!

 

2.

UserDao的方法在UserDao.xml中没有,然后执行UserDao的方法会报此错

3. 删除配置文件里中文的注释,保存.

4. 如果你确认没有以上问题,请任意修改下对应的xml文件,比如删除一个空行,保存.问题解决...

 

2019-01-02 01:57:14,854 [http-nio-8081-exec-5] ERROR com.cloud.data.platform.service.impl.PortraitKpiServiceImpl - nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 13
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 13
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77) ~[mybatis-spring-1.3.1.jar!/:1.3.1]
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446) ~[mybatis-spring-1.3.1.jar!/:1.3.1]
at com.sun.proxy.$Proxy111.selectOne(Unknown Source) ~[na:na]
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166) ~[mybatis-spring-1.3.1.jar!/:1.3.1]
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82) ~[mybatis-3.4.4.jar!/:3.4.4]
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59) ~[mybatis-3.4.4.jar!/:3.4.4]
at com.sun.proxy.$Proxy116.queryOffLightsDuration(Unknown Source) ~[na:na]
at com.sengled.cloud.data.platform.service.impl.PortraitKpiServiceImpl.getOnDurationKpi(PortraitKpiServiceImpl.java:133) ~[data-platform-service-1.0.jar!/:1.0]
at sun.reflect.GeneratedMethodAccessor431.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_141]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_141]

...
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) ~[mybatis-spring-1.3.1.jar!/:1.3.1]
... 114 common frames omitted

 java用一个实体或者一个map接收数据,哪知数据表却返回了多条数据而引发的TooManyResultsException,根据业务需要,只要一条数据,SQL应使用sum函数或者加group by 来聚合成一条数据!