SpringBoot之Mybatis-plus分页查询

1. 添加配置类

import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@Configuration
@MapperScan("com.mine.mpdemo1010.dao")
public class MybatisPlusConfig {

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

2.编写分页测试类

    @Test
    public void testSelectPage() {
        Page<User> page = new Page<>(1,5);
        userMapper.selectPage(page, null);

        page.getRecords().forEach(System.out::println); // 每页数据list集合
        System.out.println(page.getCurrent());  // 当前页
        System.out.println(page.getPages());    // 总页数
        System.out.println(page.getSize());     //每页数据显示记录数
        System.out.println(page.getTotal());    //总记录数
        System.out.println(page.hasNext());     // 是否有下一页
        System.out.println(page.hasPrevious()); //是否有上一页
    }