使用mybatis-plus或者实现登录功能时出现One record is expected

使用mybatis-plus或者实现登录功能时出现One record is expected(完结)

2022-11-06 20:52:48.282 ERROR 22396 --- [nio-9000-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: One record is expected, but the query result is multiple records] with root cause

com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: One record is expected, but the query result is multiple records
	at com.baomidou.mybatisplus.core.toolkit.ExceptionUtils.mpe(ExceptionUtils.java:49) ~[mybatis-plus-core-3.5.1.jar:3.5.1]
	at com.baomidou.mybatisplus.core.mapper.BaseMapper.selectOne(BaseMapper.java:176) ~[mybatis-plus-core-3.5.1.jar:3.5.1]
	at java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:627) ~[na:1.8.0_161]
	at com.baomidou.mybatis

出现这个原因是因为通过执行查询语句后,数据库中存在两条数据,但实际只需要一条数据

解决方法:

(1):清楚数据库中重复的用户名
(2):在springboot的业务层,对查询用户进行异常处理
(3):将查找到的用户封装到一个list集合中

你可能感兴趣的:(mybatis,java,数据库,spring,boot)