SSM中PageHelper的使用方法

转载于for dream

第一步、导包(或者导入坐标)

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

第二步、配置到spring 和mybait 整合的配置文件中

1.整合到spring-mybatis中
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--配置mybatis 插件-->
<property name="plugins">
<set>
<!--配置pageHelper 分页插件-->
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<!--方言:-->
<prop key="helperDialect">mysql</prop>
</props>
</property>
</bean>
</set>
</property>
</bean>
2.整合到mybaits-config中(选一种)
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--reasonable:分页合理化参数,默认值为false。
当该参数设置为 true 时,pageNum<=0 时会查询第一页,
pageNum>pages(超过总数时),会查询最后一页。
默认false 时,直接根据参数进行查询。-->
<property name="reasonable" value="true"/>
</plugin>
</plugins>

第三步、方法调用

使用PageHelper.startPage 静态方法调用startPage :

特点:

  1. 静态方法,传递两个参数(当前页码,每页查询条数)
  2. 使用pageHelper 分页的时候,不再关注分页语句,查询全部的语句
  3. 自动的对PageHelper.startPage 方法下的第一个sql 查询进行分页

    PageHelper.startPage(1,5);

    //紧跟着的第一个select 方法会被分页

    List list = countryMapper.findAll();

也就是说再Service层PageHelper.startPage(1,5);语句后一定是紧跟查询语句。

Service层示例代码

public PageInfo findPage(int page,int pageSize){
PageHelper.startPage(page,pageSize);
List<Company> List=companyDao.selectAll();
PageInfo pageInfo = new PageInfo(list);
return pageInfo;
}

返回的信息就是pageInfo对象,该类是插件里的类,这个类里面的属性还是值得看一看

public class PageInfo<T> implements Serializable {
private static final long serialVersionUID = 1L;
//当前页
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size;
//由于startRow 和endRow 不常用,这里说个具体的用法
//可以在页面中"显示startRow 到endRow 共size 条数据"
//当前页面第一个元素在数据库中的行号
private int startRow;
//当前页面最后一个元素在数据库中的行号
private int endRow;
//总记录数
private long total;
//总页数
private int pages;
//结果集
private List<T> list;
//前一页
private int prePage;
//下一页
private int nextPage;
//是否为第一页
private boolean isFirstPage = false;
//是否为最后一页
private boolean isLastPage = false;
//是否有前一页
private boolean hasPreviousPage = false;
//是否有下一页
private boolean hasNextPage = false;
//导航页码数
private int navigatePages;
//所有导航页号
private int[] navigatepageNums;
//导航条上的第一页
private int navigateFirstPage;
//导航条上的最后一页
private int navigateLastPage;
}

第四步、在Controller中的使用是将Service层返回来的PageInfo对象放入request域中

在jsp页面中的取值根据PageInfo中的get方法,使用EL表达式取出保存的值。在html同理

最新文章

  1. JS中基本window对象操作
  2. Android百度地图开发(一)之初体验
  3. Animator角色重复受击播放问题
  4. iis7应用程序池自动关闭问题解决
  5. Lepus经历收获杂谈(一)——confirm features的小工具
  6. linux 备份日志文件
  7. Swing-布局管理器之GridLayout(网格布局)-入门
  8. 第五章 JavaScript对象及初识面向对象
  9. C# FTPClient--FTP操作帮助类,上传下载,文件,目录操作
  10. 试用VS2019正式版
  11. 英语口语练习系列-C15-心情不好
  12. 第一册:lesson thirty five。
  13. MySQL 约束、表连接、表关联、索引
  14. java利用Jsch实现在windows平台远程操作linux服务器
  15. 【Mysql】【Navicat For Mac】Navicat Premium for Mac v12.0.23 + macOS Sierra 10.12.6
  16. ios之归档demo
  17. 怎样让HTML 表格中内容自动换行??
  18. Linux主机添加路由和端口转发
  19. Aras Innovator DB备份与还原
  20. shell read 命令 (转)

热门文章

  1. 十八、Service的应用
  2. Transformer 结构分析
  3. python中展示json数据不换行(手动换行)
  4. django-environ学习
  5. Oracle:ORA-00911: invalid character解决办法
  6. 新建Maui工程运行到IiOS物理设备提示 Could not find any available provisioning profiles for iOS 处理办法
  7. Oracle数据库的导出和导入
  8. JUC学习笔记——共享模型之内存
  9. jquery实现复选框的全选与取消全选功能
  10. Xtrabackup使用帮助