转载大神

https://blog.csdn.net/qq_33624284/article/details/72828977

  1. 把插件jar包导入项目(具体上篇有介绍http://blog.csdn.net/qq_33624284/article/details/72821811
  2. spring-mybatis.xml文件中配置
  1. <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
  2. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  3. <property name="dataSource" ref="dataSource" />
  4. <!-- 自动扫描mapping.xml文件 -->
  5. <property name="mapperLocations" value="classpath:com/wl/goldatg/mapping/*.xml"></property>
  6. <!-- 配置分页插件 -->
  7. <property name="plugins">
  8. <array>
  9. <bean class="com.github.pagehelper.PageHelper">
  10. <property name="properties">
  11. <value>
  12. dialect=mysql
  13. reasonable=true
  14. </value>
  15. </property>
  16. </bean>
  17. </array>
  18. </property>
  19. </bean>

## 下面我们就可以使用 ##

  • 新建sql语句,返回list结果集
  1. <select id="selectByList" resultMap="BaseResultMap">
  2. select
  3. <include refid="Base_Column_List" />
  4. from user_t
  5. </select>
  • service层
  1. public List<User> selectByList() {
  2. return this.userDao.selectByList();
  3. }
  • 1
  • 2
  • 3
  • Controller类
  1. @RequestMapping("/userList")
  2. public String userList(@RequestParam(required=true,defaultValue="1") Integer page,HttpServletRequest request,Model model){
  3. //PageHelper.startPage(page, pageSize);这段代码表示,程序开始分页了,page默认值是1,pageSize默认是10,意思是从第1页开始,每页显示10条记录。
  4. PageHelper.startPage(page, 3);
  5. List<User> userList = userService.selectByList();
  6. PageInfo<User> p=new PageInfo<User>(userList);
  7. model.addAttribute("page", p);
  8. model.addAttribute("userList",userList);
  9. return "showUser";
  10. }

PageInfo这个类是插件里的类,非常方便的调用,分页再次提高性能:

  1. //当前页
  2. private int pageNum;
  3. //每页的数量
  4. private int pageSize;
  5. //当前页的数量
  6. private int size;
  7. //排序
  8. private String orderBy;
  9. //由于startRow和endRow不常用,这里说个具体的用法
  10. //可以在页面中"显示startRow到endRow 共size条数据"
  11. //当前页面第一个元素在数据库中的行号
  12. private int startRow;
  13. //当前页面最后一个元素在数据库中的行号
  14. private int endRow;
  15. //总记录数
  16. private long total;
  17. //总页数
  18. private int pages;
  19. //结果集
  20. private List<T> list;
  21. //第一页
  22. private int firstPage;
  23. //前一页
  24. private int prePage;
  25. //下一页
  26. private int nextPage;
  27. //最后一页
  28. private int lastPage;
  29. //是否为第一页
  30. private boolean isFirstPage = false;
  31. //是否为最后一页
  32. private boolean isLastPage = false;
  33. //是否有前一页
  34. private boolean hasPreviousPage = false;
  35. //是否有下一页
  36. private boolean hasNextPage = false;
  37. //导航页码数
  38. private int navigatePages;
  39. //所有导航页号
  40. private int[] navigatepageNums;

使用PageInfo这个类,你需要将查询出来的list放进去:

  1. List<User> userList = userService.selectByList();
  2. PageInfo<User> p=new PageInfo<User>(userList);
  • 1
  • 2

页面使用非常方便,直接贴代码:

  1. 控制层
  1. model.addAttribute("page", p);
  • 1
  1. 前台页面
  1. <body>
  2. <center>
  3. <table width="200" border="1">
  4. <tr>
  5. <th scope="col">序号</th>
  6. <th scope="col">ID</th>
  7. <th scope="col">姓名</th>
  8. <th scope="col">密码</th>
  9. <th scope="col">年龄</th>
  10. </tr>
  11. <c:forEach begin="0" step="1" items="${userList}" var="list" varStatus="userlist">
  12. <tr>
  13. <td>${userlist.count}</td>
  14. <td>${list.id}</td>
  15. <td>${list.userName}</td>
  16. <td>${list.password}</td>
  17. <td>${list.age}</td>
  18. </tr>
  19. </c:forEach>
  20. </table>
  21. <p>一共${page.pages}页</p>
  22. <a href="userList?page=${page.firstPage}">第一页</a>
  23. <a href="userList?page=${page.nextPage}">下一页</a>
  24. <a href="userList?page=${page.prePage}">上一页</a>
  25. <a href="userList?page=${page.lastPage}">最后页</a>
  26. </center>
  27. </body>

最后结果显示:(关键点${page.prePage}简单又快捷) 

mybatis pageHelper 分页插件使用的更多相关文章

  1. mybatis pagehelper分页插件使用

    使用过mybatis的人都知道,mybatis本身就很小且简单,sql写在xml里,统一管理和优化.缺点当然也有,比如我们使用过程中,要使用到分页,如果用最原始的方式的话,1.查询分页数据,2.获取分 ...

  2. 关于Spring+mybatis+PageHelper分页插件PageHelper的使用策略

    把插件jar包导入项目(具体上篇有介绍http://blog.csdn.net/qq_33624284/article/details/72821811) spring-mybatis.xml文件中配 ...

  3. mybatis PageHelper分页插件 和 LRU算法缓存读取数据

    分页: PageHelper的优点是,分页和Mapper.xml完全解耦.实现方式是以插件的形式,对Mybatis执行的流程进行了强化,添加了总数count和limit查询.属于物理分页. 一.首先注 ...

  4. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  5. Mybatis的分页插件PageHelper

    Mybatis的分页插件PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper  文档地址:http://git.oschina. ...

  6. SpringBoot集成MyBatis的分页插件 PageHelper

    首先说说MyBatis框架的PageHelper插件吧,它是一个非常好用的分页插件,通常我们的项目中如果集成了MyBatis的话,几乎都会用到它,因为分页的业务逻辑说复杂也不复杂,但是有插件我们何乐而 ...

  7. SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页

    SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...

  8. Mybatis之分页插件pagehelper的简单使用

    最近从家里回来之后一直在想着减肥的事情,一个月都没更新博客了,今天下午没睡午觉就想着把mybatis的分页插件了解一下,由于上个月重新恢复了系统,之前创建的项目都没了,又重新创建了一个项目. 一.创建 ...

  9. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

随机推荐

  1. mysql七:视图、触发器、事务、存储过程、函数

    阅读目录 一 视图 二 触发器 三 事务 四 存储过程 五 函数 六 流程控制 一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名 ...

  2. PS 滤镜— —图像偏移

    clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...

  3. 国内镜像pip

    建议非清华大学校内的使用这个镜像: http://e.pypi.python.org/simple(这也是一个http://pypi.v2ex.com/simple),清华校内的就使用这个:http: ...

  4. P2936(BZOJ3396) [USACO09JAN]全流Total Flow[最大流]

    题 裸题不多说,在网络流的练习题里,你甚至可以使用暴力. #include<bits/stdc++.h> using namespace std; typedef long long ll ...

  5. 【LeetCode】070. Climbing Stairs

    题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...

  6. C#如何把XSD中HexBinary类型序列化uint类型

    xml schema中有hexBinary类型, 我们在实现C#的Serialization时,一般默认把hexBinary映射为byte[],但是有些情况我们需要把 hexBinary映射为uint ...

  7. ceph安装对象网关

    1.概述 安装3个网关节点分别是:controller-03.controller-04和controller-05,使用ceph gw自带的Civetweb提供服务,前端使用nginx作为前端代理. ...

  8. hadoop学习笔记411

    安装hadoop  1. 免秘钥  使用hadoop用户  ssh-keygen -t rsa  cp id_rsa.pub authorized_keys  cat id_rsa.pub>&g ...

  9. 【242】◀▶IEW-Unit07

    Unit 7 Education: Schools I.句子基本结构在写作中的运用 主谓宾 主系表 主谓 主谓宾宾 主谓宾补 1.主语: 1)位于句首 2)名词 例句:应该建立相关法律 Laws an ...

  10. HDU 5546 Ancient Go (搜索)

    题意: Alice和Bob正在下古代围棋,规则如下: 棋盘有8×8个格子,棋子下在棋盘的交叉点上,故可以有9×9个落子的位置 Alice执黑棋Bob执白棋轮流落子 与棋子直线相连的空白交叉点叫做气.当 ...