Mybatis-plus IPage分页常见问题(坑)
观前提示:
本文所使用的IDEA版本为ultimate 2019.1,JDK版本为1.8.0_141。
1.TooManyResultsException
最近在使用Mybatis-plus的IPage插件分页时,出现了以下的莫名其妙的错误
Resolved [org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 6]
然后检查我写的Controller、Service、Mapper、Mapper.xml,结果还是一无所获,以下是我的Mapper和Mapper.xml(大致内容一致)
Mapper
public interface ExampleMapper { IPage<EntityDto> selectEntityAndPage(@Param("param") Entity param, Page<Entity> page); }
Mapper.xml的select部分
<select id="selectEntityAndPage" parameterType="cn.com.example.enetity.Entity " resultType="cn.com.example.dto.EntityDto"> select id, name from table </select>
百度了一下才发现了这个深坑
mybatis-plus 中page参数不在第一个位置,返回的结果集接收对象不被认为是一个集合,而放在第一位就没有问题。
所以我改写了Mapper参数的顺序
IPage<EntityDto> selectEntityAndPage(Page<Entity> page, @Param("param") Entity param);
问题解决。