mapper selectOne 数据库存在多条数据时会出现TooManyResultsException

This issue has been tracked since 2020-08-25.

mp版本:3.3.2

复现步骤: 使用mapper的selectOne进行查询,当数据库存在多条满足queryWrapper数据时会出现TooManyResultsException

2020-08-25 11:46:05 [http-nio-8081-exec-1] ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] -Servlet.service() for servlet [dispatcherServlet] in context with path [] 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:80)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426)
	at com.sun.proxy.$Proxy68.selectOne(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:159)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:108)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:96)
	at com.sun.proxy.$Proxy69.selectOne(Unknown Source)
	at com.etekcity.it.storageservice.service.impl.ApplicationConfigServiceImpl.selectOne(ApplicationConfigServiceImpl.java:33)
	at com.etekcity.it.storageservice.service.impl.ApplicationConfigServiceImpl$$FastClassBySpringCGLIB$$9e4e08c6.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:687)
	at com.etekcity.it.storageservice.service.impl.ApplicationConfigServiceImpl$$EnhancerBySpringCGLIB$$1b905e18.selectOne(<generated>)
	at com.etekcity.it.storageservice.controller.BucketController.delete(BucketController.java:70)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)

qmdx wrote this answer on 2020-08-25

selectOne 必须保证结果唯一 wrapper.last("LIMIT 1") 限制结果集

xiaoshengaimm wrote this answer on 2021-02-04

说实话。。。为什么selectOne了,还要加Limit 1呢,不应该自动加么????

huayanYu wrote this answer on 2021-02-05

@xiaoshengaimm 你调用原生selectOne查出来多条一样抛异常,你应该明确知道有一条才调

fan1607 wrote this answer on 2022-05-12

@xiaoshengaimm 你调用原生selectOne查出来多条一样抛异常,你应该明确知道有一条才调

或者考虑加一个justSelectOne方法,让用户可以在知道可能出现多条记录的情况下取一条

More Details About Repo
Owner Name baomidou
Repo Name mybatis-plus-doc
Full Name baomidou/mybatis-plus-doc
Language JavaScript
Created Date 2016-08-29
Updated Date 2022-12-02
Star Count 612
Watcher Count 56
Fork Count 2720
Issue Count 1

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date