mybatis使用pagehelper实现分页

忘是亡心i 2023-07-07 03:34 36阅读 0赞

第一步导入依赖:建议去pagehelper官网下载最新版本的jar使用

  1. <dependency>
  2. <dependency>
  3. <groupId>com.github.jsqlparser</groupId>
  4. <artifactId>jsqlparser</artifactId>
  5. <version>3.1</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.github.pagehelper</groupId>
  9. <artifactId>pagehelper</artifactId>
  10. <version>5.1.11</version>
  11. </dependency>

第二步:去核心配置文件mybatis-config.xml中导入插件接口

  1. <plugins>
  2. <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
  3. </plugins>

第三步就可以在代码里面直接使用了
值得注意的是,要使用在查询语句之前。

  1. PageHelper.startPage(1,2);//第一个参数是页码,第二个是每页显示多少个记录
  2. List<Emp> emps = mapper.selectAll();
  3. for (Emp emp : emps){
  4. System.out.println(emp);
  5. }

ps拓展

还可以通过pageinfo这个类查看关于分页的一些信息

  1. List<Emp> emps = mapper.selectAll();
  2. PageInfo<Emp> pageInfo=new PageInfo<>(emps);
  3. System.out.println(pageInfo);

控制台;

  1. PageInfo{ pageNum=1, pageSize=2, size=2, startRow=1, endRow=2, total=7, pages=4,
  2. list=Page{ count=true, pageNum=1, pageSize=2, startRow=0, endRow=2, total=7,
  3. pages=4, reasonable=false, pageSizeZero=false}[Emp{ eid=1, ename='haihai'},
  4. Emp{ eid=2, ename='第3个'}], prePage=0, nextPage=2, isFirstPage=true,
  5. isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=8,
  6. navigateFirstPage=1, navigateLastPage=4, navigatepageNums=[1, 2, 3, 4]}

发表评论

表情:
评论列表 (有 0 条评论,36人围观)

还没有评论,来说两句吧...

相关阅读