SpringBoot使用Mybatis-Plus中分页出现total=0的情况解决

在使用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 navigationPage = new Page(page, size);
        navigationMapper.selectPage(navigationPage, null);
        System.out.println(navigationPage.getTotal());
        System.out.println(navigationPage.getRecords());
        Map map = new HashMap(16);

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

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

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/57ed9d0da3.html