上次我们搭建了SpringBoot+Mybatis+plus的普通工程,完成了简单的增删改查的工作,重复的代码我就不贴了。今天我们学习使用Mybatis-plus对数据进行分页展示
// 构建MybatisPlus配置类 所有有关MybatisPlus配置的工作都可以在这里进行
@Configuration
public class MybatisPlusConfig {
// 官方需要我们构建这么一个分页插件
@Bean
public PaginationInterceptor paginationInterceptor() {
// 如果不需要更改其默认行为 直接扔进容器即可
// return new PaginationInterceptor();
// 如果需要更改则在此处进行更改
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 默认单页显示的最大数量是500条数据 小于0则表示没有限制
paginationInterceptor.setLimit(200);
// 更改默认行为后扔进容器中
return paginationInterceptor;
}
}
@RestController
@RequestMapping("emp")
public class EmployeeControl {
@Resource
private EmployeeMapper employeeMapper;
@GetMapping("findPage")
public IPage<Employee> findPage(Integer pageNum, Integer pageSize) {
// mybatis-plus中的分页方法,该方法接收page分页对象及queryWrapper条件包装器对象
// IPage<T> selectPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);
// 将前端接收到的pageNum及pageSize用于构建page对象
// 由于该测试不需要根据条件查询 传Null即可
return employeeMapper.selectPage(new Page(pageNum, pageSize), null);
/**
发送:http://ip:端口号/emp/findPage?pageNum=0&pageSize=10
返回的结果:
{
"records": [
{
"id": 1178190765944045570,
"name": "李白",
"gender": "1",
"address": "华山之巅",
"createDatetime": 1569740277146,
"createBy": 1,
"updateDatetime": 1569740277146,
"updateBy": 1
},
{
"id": 1178193747657994242,
"name": "李四",
"gender": "0",
"address": "中关村",
"createDatetime": 1569738253786,
"createBy": 1,
"updateDatetime": 1569738253786,
"updateBy": 1
}
],
"total": 2,
"size": 10,
"current": 1,
"orders": [],
"searchCount": true,
"pages": 1
}
records就是我们要的数据结果集(由 SELECT * FROM employee LIMIT ?,? 得出)
total:总记录数(由SELECT COUNT(1) FROM employee 得出)
size:每页显示
current:当前页...
*/
}
}
我们通过Mybatis-Plus将简单的数据分页完成啦!