SSM中PageHelper的使用方法
2024-09-08 16:41:25
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 :
特点:
- 静态方法,传递两个参数(当前页码,每页查询条数)
- 使用pageHelper 分页的时候,不再关注分页语句,查询全部的语句
- 自动的对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同理
最新文章
- JS中基本window对象操作
- Android百度地图开发(一)之初体验
- Animator角色重复受击播放问题
- iis7应用程序池自动关闭问题解决
- Lepus经历收获杂谈(一)——confirm features的小工具
- linux 备份日志文件
- Swing-布局管理器之GridLayout(网格布局)-入门
- 第五章	JavaScript对象及初识面向对象
- C# FTPClient--FTP操作帮助类,上传下载,文件,目录操作
- 试用VS2019正式版
- 英语口语练习系列-C15-心情不好
- 第一册:lesson thirty five。
- MySQL 约束、表连接、表关联、索引
- java利用Jsch实现在windows平台远程操作linux服务器
- 【Mysql】【Navicat For Mac】Navicat Premium for Mac v12.0.23 + macOS Sierra 10.12.6
- ios之归档demo
- 怎样让HTML 表格中内容自动换行??
- Linux主机添加路由和端口转发
- Aras Innovator DB备份与还原
- shell read 命令 (转)