解决Mybatis-Plus分页插件无效

1.导入依赖

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.4.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.3.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.7</version>
        </dependency>
    </dependencies>

2.创建一个配置类

//标明此类为Spring的配置类
@Configuration
//扫描所有mapper接口的实现,让这些mapper能够自动注入
@MapperScan(basePackages = "com.xiao.mp.mapper")
public class MybatisPlusConfig {

    @Bean("sqlSessionFactory")
    public SqlSessionFactory SqlSessionFactory (MybatisPlusInterceptor interceptor) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();;
        sqlSessionFactoryBean.setPlugins(interceptor);
        return sqlSessionFactoryBean.getObject();
    }
   
    @Bean
    public MybatisPlusInterceptor innerInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}

Mybatis-plus在没有添加分页插件时,自带的分页方法是不会生效的。在向ioc容器添加MybatisPlusInterceptor之后,还要通过Mybatis-PlusSqlSessionFactoryBean中的setPlugins()方法把分页插件设置上去,只有这样分页方法才能生效

3.测试方法

 @Test
    public void testPage() {
        IPage<Employee> page = employeeMapper.selectPage(new Page<>(1, 2,true), null);
        System.out.println("总页数:"+page.getPages());
        System.out.println("总记录数:"+page.getTotal());
        System.out.println("一页显示几条:"+page.getSize());
        List<Employee> employees = page.getRecords();
        employees.forEach(System.out::println);
    }

> Preparing: SELECT COUNT() FROM tbl_employee
> Parameters:
<
Columns: COUNT(
)
<
Row: 10
<== Total: 1
> Preparing: SELECT id,last_name,email,gender,age FROM tbl_employee LIMIT ?
> Parameters: 2(Long)
<
Columns: id, last_name, email, gender, age
<
Row: 2, Jerry, jerry@atguigu.com, 0, 25
<== Row: 3, Black, black@atguigu.com, 1, 30
<== Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@58be6e8]
总页数:5
总记录数:10
一页显示几条:2
Employee{id=2, lastName=Jerry, email=jerry@atguigu.com, gender=0, age=25}
Employee{id=3, lastName=Black, email=black@atguigu.com, gender=1, age=30}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值