mybatis使用pagehelper实现分页
第一步导入依赖:建议去pagehelper官网下载最新版本的jar使用
<dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
第二步:去核心配置文件mybatis-config.xml中导入插件接口
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
第三步就可以在代码里面直接使用了
值得注意的是,要使用在查询语句之前。
PageHelper.startPage(1,2);//第一个参数是页码,第二个是每页显示多少个记录
List<Emp> emps = mapper.selectAll();
for (Emp emp : emps){
System.out.println(emp);
}
ps拓展
还可以通过pageinfo这个类查看关于分页的一些信息
List<Emp> emps = mapper.selectAll();
PageInfo<Emp> pageInfo=new PageInfo<>(emps);
System.out.println(pageInfo);
控制台;
PageInfo{ pageNum=1, pageSize=2, size=2, startRow=1, endRow=2, total=7, pages=4,
list=Page{ count=true, pageNum=1, pageSize=2, startRow=0, endRow=2, total=7,
pages=4, reasonable=false, pageSizeZero=false}[Emp{ eid=1, ename='haihai'},
Emp{ eid=2, ename='第3个'}], prePage=0, nextPage=2, isFirstPage=true,
isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=8,
navigateFirstPage=1, navigateLastPage=4, navigatepageNums=[1, 2, 3, 4]}
还没有评论,来说两句吧...