mp框架提供了物理分页插件,我们下面来看下如何实现:
首先配置一个PaginationInterceptor的bean;
package com.java1234.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* MybatisPlus配置类
* @author java1234_小锋
* @site www.java1234.com
* @company Java知识分享网
* @create 2020-09-07 14:03
*/
@Configuration
public class MybatisPlusConfig {
/**
* mybatis-plus分页插件
* @return
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
带分页查询:
/**
* 查找薪水大于3200 带分页
* sql: select * from t_employee where salary>3200
*/
@Test
public void selectByQueryWrapperWithPage(){
QueryWrapper<Employee> queryWrapper=new QueryWrapper();
queryWrapper.gt("salary",3200);
Page<Employee> page = new Page<>(1, 3);
Page<Employee> employeePage = employeeMapper.selectPage(page, queryWrapper);
System.out.println("总记录数:"+employeePage.getTotal());
System.out.println("总页数:"+employeePage.getPages());
System.out.println("当前页数据:"+employeePage.getRecords());
}