MyBatis基于pagehelper实现分页原理及代码实例
更新时间:2020年06月11日 11:11:38 作者:shouyaya
这篇文章主要介绍了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<?> 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);
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
idea2020.1版本git提交项目到github上的方法
这篇文章主要介绍了idea2020.1版本git提交项目到github上的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2020-06-06
Java使用try-with-resources实现自动解锁
项目中使用Redission分布式锁,每次使用都需要显示的解锁,很麻烦,Java 提供了 try-with-resources 语法糖,它不仅可以用于自动关闭流资源,还可以用于实现自动解锁,本文将介绍如何利用 try-with-resources 实现锁的自动释放,需要的朋友可以参考下2025-01-01


最新评论