Springboot mybatisplus如何解决分页组件IPage失效问题
作者:草青工作室
这篇文章主要介绍了Springboot mybatisplus如何解决分页组件IPage失效问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
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;
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
