欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2022-04-17 08:58:40|栏目:JAVA代码|点击:

使用pagehelper分页的原理是:

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

1.通过maven引入pagehelper依赖:

<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper</artifactId>
  <version>5.1.11</version>
</dependency>

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

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

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<Person> persons=userService.getAllUsersBypageHelper(); //因而获得的是分好页的结果集
     
    PageInfo<?> pageHelper=page.toPageInfo(); //获取页面信息的对象,里面封装了许多页面的信息 如:总条数,当前页码,需显示的导航页等等

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

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

  }

上一篇:解决mybatis无法给带有下划线属性赋值问题

栏    目:JAVA代码

下一篇:详解jdbc实现对CLOB和BLOB数据类型的操作

本文标题:MyBatis基于pagehelper实现分页原理及代码实例

本文地址:http://www.codeinn.net/misctech/199385.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有