Springboot-mybatisplus-solve the failure problem of paging component IPage
background
The paging plug-in IPage of mybatisplus is very useful, whether it is based on @select annotation or XML-based, paging query can be realized; I don’t know if
there is any change in the code, and paging is not easy to use when using it -_-, because there is no Pagination conditions are injected, causing all results to be returned. Go directly to the solution without delving into it!
Add pagination interceptor
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor page = new PaginationInterceptor();
page.setDbType(DbType.POSTGRE_SQL);//选择对应DB类型
return page;
}
}
IPage pagination use
mapper needs to inherit BaseMapper
@Repository
public interface XxxMapper extends BaseMapper<XxxMapper > {
Page<XxxBo> selectAllByPage(IPage<XxxBo> page,@Param("keyword") String keyword);
}
XML configuration
<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>
service layer call
@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;
}