记一篇MybatisPlus分页失效解决方案

刚才使用mybatisplus提供的Page分页时,一直失效,看网上说要加配置什么的,试了多种方案还是不行。最后自己结合github pagehelper实现了分页,目前还不知道有没有其他影响。我使用的是springboot项目,下面就开始编码吧。

一、环境搭建

springboot:2.0.6.RELEASE
mybatis-plus-status:3.4.2
pagehelper:1.3.0

  <parent>
        <groupId>com.longfor.c2groupId>
        <artifactId>cmo-expenseartifactId>
        <version>1.0.0-SNAPSHOTversion>
    parent>
        
        <dependency>
            <groupId>org.apache.velocitygroupId>
            <artifactId>velocity-engine-coreartifactId>
            <version>2.2version>
        dependency>

        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-generatorartifactId>
            <version>3.4.0version>
        dependency>

        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.4.2version>
        dependency>

        
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelper-spring-boot-starterartifactId>
            <version>1.3.0version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatisgroupId>
                    <artifactId>mybatisartifactId>
                exclusion>
                <exclusion>
                    <groupId>org.mybatisgroupId>
                    <artifactId>mybatis-springartifactId>
                exclusion>
            exclusions>
        dependency>

二、配置类


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

三、代码示例

 LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
 queryWrapper.in(Student::getId, Arrays.asList(ids));
 //输入页码和页数
 PageHelper.startPage(query.getPageNumber(), query.getPageSize());
 List<Student> students= studentService.getBaseMapper().selectList(queryWrapper);
 com.github.pagehelper.PageInfo<Student> pageInfo = new com.github.pagehelper.PageInfo<>(students);
 //students--分页后的数据 pageInfo.getTotal()--总条数
 return PageResponse.page(students, pageInfo.getTotal()); 

你可能感兴趣的:(java,mybatisplus,java,spring,boot,MybatisPlus,page,分页)