Mybatis中使用分页后显示Expected one result (or null) to be returned by selectOne()
问题描述
nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result
这个说的是你的sql语句查询到了多条记录,而你用于接收数据类型只能接收一条记录。只有map、list才能接收多条数据。而我出现该错误是因为我想用Pageinfo<实体类>的方法去接收sql查询返回的集合,但是pageInfo只能接收一条数据。
问题解决办法
PageInfo需要与PageHelper 一起运用,需要先将调用PageHelper ,然后用List集合接收返回的多条数据,最后返回 new PageInfo<>(userInfo)即可解决该问题 。但是调用接口时想要获取该数据还需要传入page和limit两个参数。
PageHelper.startPage(pageObj.getPage(), pageObj.getLimit(), true);
List<UserInfo> userInfo= userMapper.getUserInfo(username,age);
return new PageInfo<>(userInfo);