温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MyBatis基于pagehelper实现分页原理详解

发布时间:2020-06-23 18:13:31 来源:亿速云 阅读:998 作者:清晨 栏目:编程语言

不懂MyBatis基于pagehelper实现分页原理?其实这个问题不难,下面让小编带着大家一起学习了解原理,希望大家阅读完这篇文章后大所收获。

使用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<&#63;> page=PageHelper.startPage(Integer.parseInt(num),5); //设置第几条记录开始,多少条记录为一页

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

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

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

  }

感谢你能够认真阅读完这篇文章,希望小编分享MyBatis基于pagehelper实现分页原理详解内容对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI