在使用Mybatis-Plus进行分页时,发现获取的数据总数total=0,这种里有里有两个值得注意的地方:
一、需要对分页进行配置:
二、目前对于Mybatis-Plus中分页配置出现了改变,高版本和低版本的配置不同:
1.低版本:

@Configuration
public class MybatisConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

2.高版本:

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
    return interceptor;
}

分页接口示例:

  @GetMapping("/pages/{page}/{size}")
    public ResultVo pageNa(@PathVariable("page") int page,
                           @PathVariable("size") int size) {

        //1.创建page对象
        //调用方法时候,底层封装,把分页所有数据封装到navigationPage对象里面
        Page<Navigation> navigationPage = new Page<>(page, size);
        navigationMapper.selectPage(navigationPage, null);
        System.out.println(navigationPage.getTotal());
        System.out.println(navigationPage.getRecords());
        Map<String, Object> map = new HashMap<>(16);

        map.put("total", navigationPage.getTotal());
        map.put("rows", navigationPage.getRecords());

        return ResultVo.ok().data(map);
    }
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐