分页思路图
-
引入pageHelper的jar包,pageHelper5.x版本要 改为PageInterceptor和helperDialect
-
在sqlSessionFactoryBean配置依赖
<!-- 配置sqlSessionFactoryBean整合mybatis-->
<bean id="sessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<!-- 指定mapper.xml配置文件配置,多个-->
<property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/>
<!-- 装配数据源-->
<property name="dataSource" ref="dataSource"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<props>
<!-- 配置数据库方言,告诉PageHelper当前使用的数据库-->
<prop key="dialect">mysql</prop>
<!-- 页码的合理化修正-->
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
- 编写sql语句,通过关键字搜索(模糊查询)
<!-- 通过关键字搜索(模糊查询)
这样搜索很慢,后来用到ElasticSearch
-->
<select id="selectAdminByKeywords" resultMap="BaseResultMap">
select id, login_acct, user_pswd, user_name, email, create_time
from t_admin
where login_acct like concat("%",#{keyword},"%") or
user_name like concat("%",#{keyword},"%") or
email like concat("%",#{keyword},"%")
</select>
- Service服务层 getPageInfo()方法
/**
* 使用PageInfo开启分页,封装到pageInfo对象中
* @param keyword
* @param pageNum
* @param pageSize
* @return
*/
@Override
public PageInfo<Admin> getPageInfo(String keyword, Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize); // 开启分页功能,体现了PageHelper的非侵入式设计
List<Admin> list = adminMapper.selectAdminByKeywords(keyword); // list是一个Page类型,Page继承了ArrayList
return new PageInfo<>(list); // 封装到PageInfo对象中
}
- Handler控制层
/**
* 分页,带搜索参数和不带的两种情况适配
* @param keyword
* @param pageNum
* @param pageSize
* @param modelMap
* @return
*/
@RequestMapping("/admin/get/page")
public String getPageInfo(
@RequestParam(value = "keyword",defaultValue = "") String keyword, // 请求时未携带keyword时是空字符串
@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize",defaultValue = "6") Integer pageSize, // 每页显示6条
ModelMap modelMap
){
PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);
modelMap.addAttribute(TongchouConstant.ATTR_NAME_PAGE_INFO,pageInfo);
return "admin-page";
}
- admin-page.jsp
- 分页条动态计算变更
①. 引入pagination.css和pagination.js文件
<%@include file="/WEB-INF/include-head.jsp" %>
<link rel="stylesheet" href="/css/pagination.css">
<script src="/jquery/jquery.pagination.js"></script>
②.替换html代码
<tfoot>
<tr>
<td colspan="6" align="center">
<div id="pagination" class="pagination"></div>
</td>
</tr>
</tfoot>
③.添写js代码
<script>
$(function () {
initPagination(); // 对分页条进行初始化操作
});
function initPagination() {
var totalRecord = ${requestScope.pageInfo.total}; // 获取总记录数
var properties = {
num_edge_entries:2, // 边缘页数
num_display_entries:4, // 主体页数
callback:pageSelectCallback,
items_per_page:${requestScope.pageInfo.pageSize},// 每页显示的数量
current_page:${requestScope.pageInfo.pageNum-1}, // 内部源码从0开始,但pageNum从1开始
prev_text:"上一页",
next_text:"下一页"
};
$("#paginationId").pagination(totalRecord,properties);
}
function pageSelectCallback(pageIndex,jQuery) { // 点击页码跳转,pageIndex默认从0开始
var pageNum = pageIndex+1; // 根据pageIndex计算
window.location.href = "admin/get/page?pageNum="+pageNum; // 前往的页面
return false; // 超链接默认行为变false不跳转
}
</script>
④.带关键字表单查询
<form action="admin/get/page" method="post" class="form-inline" role="form" style="float:left;">
<div class="form-group has-feedback">
<div class="input-group">
<div class="input-group-addon">查询条件</div>
<input name="keyword" class="form-control has-success" type="text" placeholder="请输入查询条件">
</div>
</div>
<button type="submit" class="btn btn-warning">
<i class="glyphicon glyphicon-search"></i> 查询
</button>
</form>
小bug,查询后的分页条点击其他页码跳转失效
window.location.href = "admin/get/page?pageNum="+pageNum+"&keyword"+${param.keyword}; // 前往的页面