Springboot-mybatisplus-ページング コンポーネント IPage の障害問題を解決する
背景
mybatisplusのページングプラグインIPageは非常に便利です、@selectアノテーションベースでもXMLベースでもページングクエリを実現できますが、コードに変更があるかは分かりませんが、ページングは簡単ではありません-_- を使用する場合は、ページネーション条件が挿入されていないため、すべての結果が返されます。深く掘り下げずに解決策に直接進みます。
ページネーションインターセプターを追加する
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor page = new PaginationInterceptor();
page.setDbType(DbType.POSTGRE_SQL);//选择对应DB类型
return page;
}
}
IPage ページネーションの使用
マッパーは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;
}