当前位置:网站首页>IPage can display data normally, but total is always equal to 0

IPage can display data normally, but total is always equal to 0

2022-07-05 07:01:00 Emmmmm_ one

Problem description : The code uses Mybatis-Plus Automatic paging of ,getRecords() correct , however getTotal() Always equal to 0

No problem with code logic , So look for Mybatis-Plus Configuration problems

Try the best way :

1.  Whether the configuration class is added @Configuration

2.  The statistical SQL Automatically optimized , Inaccurate statistics lead to ---> close Mybatis-Plus Automatic optimization of

Page<?> page = new Page<>(currentPage, size);    //  Open paging 
page.setOptimizeCountSql(false);    //  close mybatisPlus Automatic optimization of 

    This method is inefficient , You can write it yourself count sql

page.setSearchCount(false);
page.setTotal(mapper.listCount(sql));

Reference article : MyBatis-Plus Of IPage Pagination total Incorrect question

                    MybatisPlus Automatic paging ,total Inaccurate questions

3. Set the type of database

PaginationInnerInterceptor page = new PaginationInnerInterceptor();
page.setDbType(DbType.MYSQL);
return page;

4.  Check mybatis-plus-boot-starter edition

    3.4.0 The version has an update to this part , If it is an old version upgrade , Paging failure will occur , meanwhile idea Will prompt PaginationInterceptor obsolete , The new version uses MybatisPlusInterceptor

  In the end, I put PaginationInnerInterceptor Instead of MybatisPlusInterceptor solve

The successful code is as follows :

@Configuration
public class MybatisPlusConfig {

    /**
     *  Implement paging configuration 
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());   //     Register optimistic lock plugin 
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());    //  Paging plug-ins 
        return interceptor;
    }
}

This part of the code comes from :Page Pagination records There's data , however total=0, terms of settlement

原网站

版权声明
本文为[Emmmmm_ one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050651137879.html