mybatisplus分页total总数为0

mybatisplus分页total总数为0

背景:最近初始化新项目时,使用mybatisplus分页功能发现 records 有记录,但是 total 总是为0,
	于是开启了一顿“知识寻求”之路

SpringBoot版本

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.2</version>
    <relativePath/>
</parent>

Mybatis-Plus版本

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

Mybatis-Plus分页两种方式
1、XML

// Service层
Map<String, Object> res = new HashMap<>();
Page<Map> page = new Page(dto.getPageIndex(), dto.getPageSize(), true);
dao.getList(page, dto);
res.put("list", page.getRecords());
res.put("total", page.getTotal());

// Dao层
Page<Map> getList(Page<Map> page, @Param("dto") CommonDTO dto);
<select id="getList" resultType="java.util.Map">
    select * from TableName
    where 1=1
    <if test="dto.name != null and dto.name != ''">
        and name like concat('%', concat(#{dto.name},'%'))
    </if>
    order by create_time asc
</select>

这样就可以得到分页数据,最后的分页数据全在Page对象中,不重新封装成Map也可,只是为了更一目了然一点

2、本身自带的分页方法
在这里插入图片描述
第一个参数为Page分页对象,初始化传入pageIndex、pageSize即可
第二个参数为分页时查询的条件
二者传入即可得到分页数据

目前网上常见的说法总结

1、由于项目中存在其他分页插件,干扰了plus本身的分页,移除即可(未解决我的问题)

<dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.3</version>
</dependency>

2、plus分页配置(未解决我的问题)

plus版本3.4之前

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

plus版本3.4之后

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mybatisPlusInterceptor;
    }
}

3、分页配置+数据源注册(解决问题)

@Configuration
public class MybatisPlusConfig {

    @Bean(name = "mybatisPlusInterceptor")
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setOptimizeJoin(true);
        paginationInnerInterceptor.setDbType(DbType.POSTGRE_SQL);
        paginationInnerInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }

}
    @Bean(name = "SqlSqlSessionFactory")
    public SqlSessionFactory SqlSqlSessionFactory(@Qualifier("mybatisPlusInterceptor") MybatisPlusInterceptor mybatisPlusInterceptor) throws Exception{
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        // 分页,注册
        bean.setPlugins(mybatisPlusInterceptor);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
        return bean.getObject();
    }

Over

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值