本文主要介绍Mybatis分页插件PageHelper的使用方法和jQuery的Pagination分页插件的使用。
1 POM依赖
导入pagehelper-5.0.0.jar包。
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
2 Mybatis对PageHelper的配置
在spring-config.xml配置文件中加入<property name="plugins">,配置PageHelper插件,提供对Mybatis的分页支持。
<bean id="sstaMerSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations"
value="classpath*:com/mydomain/dao/**/*.xml"/>
<property name="dataSource" ref="sstaDS"/>
<!-- 配置分页插件 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=oracle
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>
3 PageHelper获取分页数据
3.1 通过PageHelper获取分页数据PageInfo
(1)在使用Dao进行查询之前,先调用语句:
PageHelper.startPage(pageNumber, pageSize);
告诉PageHelper对执行的Dao查询进行物理分页,分页的页码和每页大小。
(2)调用Dao查询后,将返回的list构造PageInfo:
PageInfo<UserInfo> pageInfo = new PageInfo<UserInfo>(list);
(3)最后返回分页数据pageInfo
示例代码:
/**
* 获取用户信息列表
*
* @param groupId
* @param status
* @param pageNumber
* @param pageSize
* @return
*/
public PageInfo<UserInfo> queryUserInfo(String groupId, String status,
int pageNumber, int pageSize) {
PageHelper.startPage(pageNumber, pageSize);
List<UserInfo> list = queryUserInfoDao.selectUserInfoList(groupId, status);
PageInfo<UserInfo> pageInfo = new PageInfo<UserInfo>(list);
return pageInfo;
}
3.2 PageInfo分页数据的使用
对返回的pageInfo获取其属性可以获取分页数据及其页码
数据:pageInfo.getList()
页码:pageInfo.getPageNum()
页大小:pageInfo.getPageSize()
数据大小:pageInfo.getSize()
4 jQuery的Pagination分页插件
前端展示可以使用jQuery的Pagination分页插件完成。
jquery.pagination.js下载地址:
https://plugins.jquery.com/tag/pagination/
4.1 jQuery Pagination使用方法
// 共多少条数据
var listSize = "${listSize}";
// 每页显示多少条数据
var pageSize = "${pageSize}";
// 当前页面
var pageNumber = "${pageNumber}";
$(".GridBackColor").tablesorter();
if (listSize != 0) {
$("#Pagination").pagination(listSize, {
// 每页显示多少条记录
items_per_page: pageSize,
// 当前页
current_page: pageNumber,
next_text: "下一页",
prev_text: "上一页",
// 连接分页主体,显示的条目数
num_edge_entries: 1,
// handlePaginationClick回调函数
callback: handlePaginationClick
});
}
4.2 分页数据展示
数据可以通过ajax方式加载,在前端进行展示。
// 通过回调加载分页数据,在前端展示
function handlePaginationClick(new_page_index, pagination_container) {
$.post(basePath + "/mydomain/userInfoData", {
groupId: groupId,
status: status,
pageNumebr: new_page_index
},
function(data, status) {
var jsonData = JSON.parse(data);
var tableBodyStr = "";
if (jsonData.length != 0) {
$.each(jsonData, function(index, n) {
tableBodyStr = tableBodyStr + '<td width="5%">' + (index + 1) +
'</td><td width="5%">' + jsonData[index].fundAcct +
'</td><td width="5%">' + jsonData[index].custNo +
'</td><td width="5%">' + jsonData[index].status +
'</td><td width="5%">' + jsonData[index].insertTime +
'</td></tr>';
});
$('#tableBodyDiv').html(tableBodyStr);
}
});
return false;
}