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

一、环境搭建

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

  <parent>
        <groupId>com.longfor.c2</groupId>
        <artifactId>cmo-expense</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
        <!-- mybatisplus 生成工具 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.0</version>
        </dependency>

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

        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                </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()); 
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐