之前使用tkmybatis时候的分页代码

    final Integer pageNum = parameter.getPageNum();
        final Integer pageSize = parameter.getPageSize();

        final Map<String, Object> map = objectToMap(parameter);

        PageHelper.startPage(Optional.ofNullable(pageNum).orElse(1), Optional.ofNullable(pageSize).orElse(15));

        Page<TenantDTO> page = baseMapper.queryPage(map);

返回结果

{
  "list":[...] ,
    "total": 1,
    "pageSize": 15,
    "pageNum": 1
}

之前的maven依赖 

      <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>mybatis</artifactId>
                    <groupId>org.mybatis</groupId>
                </exclusion>
            </exclusions>
        </dependency>

解决方法

mybatis-plus中的依赖

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>

新增Configuration配置

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;

@Configuration
@EnableTransactionManagement
@MapperScan("com.h2.*.*.mapper*")
public class MybatisPlusConfiguration {


  /***
     * 兼容PageHelper分页插件,防止出现page.getPageNum,page.getTotal,page.getSize都返回0
     * https://blog.csdn.net/u012280292/article/details/99678037
     * @return
     */
    @Bean
    ConfigurationCustomizer mybatisConfigurationCustomizer() {
        return configuration -> configuration.addInterceptor(new PageInterceptor());
    }

}

解决方法转载自:https://blog.csdn.net/u012280292/article/details/99678037

posted on 2021-03-15 11:18  你不知道的浪漫  阅读(300)  评论(0编辑  收藏  举报