Mybatis-Plus分页插件
迪丽瓦拉
2024-05-30 16:36:37
0

引言:

MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能

1.添加Configuration配置类

@Configuration
@MapperScan("com.atguigu.mybatisplus.mapper") //可以将主类中的注解移到此处public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(newPaginationInnerInterceptor(DbType.MYSQL));return interceptor;}
}

2.测试

@Test
public void testPage(){//设置分页参数,泛型查询此类Page page = new Page<>(1, 5);//第一参数为当前页,第二个参数为页包含多少数据userMapper.selectPage(page, null);//获取分页数据List list = page.getRecords();list.forEach(System.out::println);//输出对应真个的信息System.out.println("当前页:"+page.getCurrent());System.out.println("每页显示的条数:"+page.getSize());System.out.println("总记录数:"+page.getTotal());System.out.println("总页数:"+page.getPages());System.out.println("是否有上一页:"+page.hasPrevious());System.out.println("是否有下一页:"+page.hasNext());
}

3.自定义xml分页

mapper接口中的方法

Page selectPageVo(@Param("page") Page page, @Param("age")
Integer age);//结构必须保持一致

mapper的xml文件

id,username,age,email