您当前的位置: 首页 >  mybatis

梁云亮

暂无认证

  • 4浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

MyBatisPlus分页的同时指定排序规则

梁云亮 发布时间:2020-07-29 15:17:47 ,浏览量:4

分页配置

MyBatis Plus要想使用分页,需要添加如下配置:

@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MybatisPlusConfig {

    /**
     * 分页插件
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor; //返回分布拦截器
    }

}
指定排序规则: 方法一
public PageBean listPage(int pageNum, int pageSize) {
    IPage page = new Page(pageNum,pageSize);
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.eq("state",1)
    .orderByDesc("level");
    IPage categoryIPage = categoryMapper.selectPage(page, queryWrapper);
    return PageBean.init(categoryIPage);
}
方法二(不建议)
public PageBean listPage(int pageNum, int pageSize) {
    IPage page = new Page(pageNum,pageSize);
    QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.eq("state",1)
    .orderBy("level")
    .last("desc");  // 手动把sql拼接到最后(有sql注入的风险,请谨慎使用)
    IPage categoryIPage = categoryMapper.selectPage(page, queryWrapper);
    return PageBean.init(categoryIPage);
}
关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.0606s