Springboot-mybatisplus-解决分页组件IPage失效问题
背景
mybatisplus的分页插件IPage很好用,不管是基于@select注解还是基于XML的都可以实现分页查询;
不知道代码有什么改动,用着用着就分页居然不好使了-_-,select时由于没有注入分页条件,导致将所有结果都返回了。没有深究直接上解决方案吧!
添加分页拦截器
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor page = new PaginationInterceptor();
page.setDbType(DbType.POSTGRE_SQL);//选择对应DB类型
return page;
}
}
IPage分页使用
mapper需要继承BaseMapper
@Repository
public interface XxxMapper extends BaseMapper<XxxMapper > {
Page<XxxBo> selectAllByPage(IPage<XxxBo> page,@Param("keyword") String keyword);
}
XML配置
<select id="selectAllByPage" resultMap="BaseResultMap">
select * from xx.xxx where enable=1
<if test="keyword != null">
and (id ~* #{keyword} or name ~* #{keyword} or code ~* #{keyword})
</if>
</select>
服务层调用
@Override
public Page<XxxBo> viewInfoPage(PageReq req) {
IPage<XxxBo> page = new Page<>(req.getPage().getPage(),req.getPage().getSize());
Page<XxxBo> list = xxxMapper.selectAllByPage(page,req.getKeyword());
return list;
}