栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MyBatis基于pagehelper实现分页原理及代码实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MyBatis基于pagehelper实现分页原理及代码实例

使用pagehelper分页的原理是:

通过MyBatis的插件原理(类似web里的filter拦截器),在mapper配置文件将pagehelper注册为MyBatis的插件,从而进行分页

1.通过maven引入pagehelper依赖:



  com.github.pagehelper
  pagehelper
  5.1.11

2.在MyBatis的mapper配置文件将pagehelper注册为MyBatis的插件

   
    
  

3.pagehelper的用法:

private void selectAllUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {

    String num=request.getParameter("num");

    if(null==num)
    {
      num="1";
    }

    // Page PageInfo

    Page page=PageHelper.startPage(Integer.parseInt(num),5); //设置第几条记录开始,多少条记录为一页

    //通过userService获取user的信息,其sql语句为"select * from user" 但因pagehelp已经注册为插件,所以pagehelp会在原sql语句上增加limit,从而实现分页
    List persons=userService.getAllUsersBypageHelper(); //因而获得的是分好页的结果集
     
    PageInfo pageHelper=page.toPageInfo(); //获取页面信息的对象,里面封装了许多页面的信息 如:总条数,当前页码,需显示的导航页等等

    request.setAttribute("persons",persons);
    request.setAttribute("pagehelper",pageHelper);

    request.getRequestDispatcher("/persons.jsp").forward(request,response);

  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/133774.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号