【异常】mybatis-plus:分页TooManyResultsException: Expected one result (or null) to be returned by selectOn
当mybatis-plus所提供的接口不能满足业务需要的时候,需要自定义sql语句,有时候会用到分页查询,当使用
/**
* 分页查询我的评论
*
* @param uid
* @return
*/
IPage<MyCommentDto> queryMyComment( @Param("uid") Long uid,Page<MyCommentDto> page);
这个mapper接口去分页的时候,会出现如下错误
nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null)
to be returned by selectOne(), but found: 20]
我们需要将page放到参数的第一个位置,就能解决该问题。如下:
/**
* 分页查询我的评论
*
* @param uid
* @return
*/
IPage<MyCommentDto> queryMyComment(Page<MyCommentDto> page, @Param("uid") Long uid);
**还有一个原因是配置文件中 自定义sqlSessionFactory
将其中的 SqlSessionFactoryBean 换成MybatisPlus官方文档里的MybatisSqlSessionFactoryBean就可以了
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.ssm.entity"/>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
</bean>
<bean class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.ssm.entity"/>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>