mybatis pagehelper实现分页
jar包的版本一定要對應(yīng),不然會出現(xiàn)一系列的問題
下載jar包
? <properties> ?
??????? <!-- spring版本號 --> ?
??????? <spring.version>4.0.2.RELEASE</spring.version> ?
??????? <!-- mybatis版本號 --> ?
??????? <mybatis.version>3.3.0</mybatis.version> ?
??????? <!-- log4j日志文件管理包版本 --> ?
??????? <slf4j.version>1.7.7</slf4j.version> ?
??????? <log4j.version>1.2.17</log4j.version> ?
? </properties>?
??? <!-- 分頁插件 -->
??? <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
??? <groupId>com.github.pagehelper</groupId>
??? <artifactId>pagehelper</artifactId>
??? <version>4.0.3</version>
</dependency>
<dependency>
??? <groupId>com.github.miemiedev</groupId>
??? <artifactId>mybatis-paginator</artifactId>
??? <version>1.2.17</version>
</dependency>
========================================================
配置分頁過濾器
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 配置管理器 -->
<configuration>
?? ?<plugins>
?? ???? <plugin interceptor="com.github.pagehelper.PageHelper">
?? ???????? <!-- 4.0.0以后版本可以不設(shè)置該參數(shù) -->
?? ???????? <property name="dialect" value="mysql"/>
?? ???????? <!-- 該參數(shù)默認(rèn)為false -->
?? ???????? <!-- 設(shè)置為true時,會將RowBounds第一個參數(shù)offset當(dāng)成pageNum頁碼使用 -->
?? ???????? <!-- 和startPage中的pageNum效果一樣-->
?? ???????? <property name="offsetAsPageNum" value="true"/>
?? ???????? <!-- 該參數(shù)默認(rèn)為false -->
?? ???????? <!-- 設(shè)置為true時,使用RowBounds分頁會進(jìn)行count查詢 -->
?? ???????? <property name="rowBoundsWithCount" value="true"/>
?? ???????? <!-- 設(shè)置為true時,如果pageSize=0或者RowBounds.limit = 0就會查詢出全部的結(jié)果 -->
?? ???????? <!-- (相當(dāng)于沒有執(zhí)行分頁查詢,但是返回結(jié)果仍然是Page類型)-->
?? ???????? <property name="pageSizeZero" value="true"/>
?? ???????? <!-- 3.3.0版本可用 - 分頁參數(shù)合理化,默認(rèn)false禁用 -->
?? ???????? <!-- 啟用合理化時,如果pageNum<1會查詢第一頁,如果pageNum>pages會查詢最后一頁 -->
?? ???????? <!-- 禁用合理化時,如果pageNum<1或pageNum>pages會返回空數(shù)據(jù) -->
?? ???????? <property name="reasonable" value="true"/>
?? ???????? <!-- 3.5.0版本可用 - 為了支持startPage(Object params)方法 -->
?? ???????? <!-- 增加了一個`params`參數(shù)來配置參數(shù)映射,用于從Map或ServletRequest中取值 -->
?? ???????? <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默認(rèn)值 -->
?? ???????? <!-- 不理解該含義的前提下,不要隨便復(fù)制該配置 -->
?? ???????? <property name="params" value="pageNum=start;pageSize=limit;"/>
?? ???????? <!-- 支持通過Mapper接口參數(shù)來傳遞分頁參數(shù) -->
?? ???????? <property name="supportMethodsArguments" value="true"/>
?? ???????? <!-- always總是返回PageInfo類型,check檢查返回類型是否為PageInfo,none返回Page -->
?? ???????? <property name="returnPageInfo" value="check"/>
?? ???? </plugin>
?? ?</plugins>
</configuration>
=================================================================
將SqlMapConfig.xml分頁過濾器配置在sqlSessionFactory模板中
?<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> ?
??? <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> ?
??????? <property name="dataSource" ref="dataSource" /> ?
??????? <!-- 自動掃描mapping.xml文件 --> ?
??????? <property name="mapperLocations" value="classpath:com/cjt/mapping/*.xml"></property>
???? <property name="configLocation" value="classpath:SqlMapConfig.xml"></property> ?
??? </bean>?
=================================================================
list列表查詢此處省略(常用的查詢list方式)
=================================================================
controller中的代碼如下:
??? @SuppressWarnings("unchecked")
?? ?@RequestMapping(value="/queryProxy", method = {RequestMethod.POST,RequestMethod.GET })
?? ?public ModelAndView queryAllProxy(Model m,ProxyBusiness proxyBusiness,HttpServletRequest request,HttpServletResponse response){
?? ??? ?String pageNum = request.getParameter("pageNum");
?? ??? ?String pageSize =request.getParameter("pageSize");
?? ??? ?int num = 1;
?? ??? ?int size = 10;
?? ??? ?if (pageNum != null && !"".equals(pageNum)) {
?? ??? ??? ?num = Integer.parseInt(pageNum);
?? ??? ?}? ?
?? ??? ?if (pageSize != null && !"".equals(pageSize)) {
?? ??? ??? ?size = Integer.parseInt(pageSize);
?? ??? ?}
?? ??? ?PageHelper.startPage(num, size);
?? ??? ?List<ProxyBusiness> ProxyBusinessList = proxyBusinessService.queryAllproxy(proxyBusiness);//list方法調(diào)用
?? ??? ?PageInfo<ProxyBusiness> pagehelper = new PageInfo<ProxyBusiness>(ProxyBusinessList);
?? ??? ?ModelAndView modelAndView = new ModelAndView();
?? ??? ?modelAndView.addObject("pagehelper", pagehelper);
?? ??? ?modelAndView.setViewName("backend/proxyBusinessList");
?? ??? ?return modelAndView;
?? ?}
使用modelAndView返回頁面
========================================================================
展示信息的jsp頁面:
<%@ page language="java" contentType="text/html; charset=utf-8"
??? pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<% ?
??? String path = request.getContextPath(); ?
??? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; ?
%>
<base href="<%=basePath%>">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="<%=basePath%>css/page.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%=basePath%>js/jquery-3.1.1.min.js"></script>
<title>代理商列表</title>
<script type="text/javascript">
?? ?$(document).ready(function() {
?? ?});
?? ?function queryAllPerson(pageNum, pageSize) {
?? ??? ?$("#edit_area_div").load("<%=basePath%>backend/queryProxy?pageNum=" + pageNum + "&pageSize=" + pageSize);
?? ?}
</script>
</head>
<body>
<div class="container">
?? ??? ?<div class="row clearfix">
?? ??? ??? ?<div class="col-md-12 column">
?? ??? ??? ??? ?<div id="edit_area_div">
?? ??? ??? ??? ??? ?<table class="table" id="personList_table">
?? ??? ??? ??? ??? ??? ?<thead>
?? ??? ??? ??? ??? ??? ??? ?<tr>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>編號</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>姓名</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>公司名稱</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>代理品牌</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>網(wǎng)點情況</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>希望代理區(qū)域</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>電話</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>郵箱</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>申請時間</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>代理商加盟信息</td>
?? ??? ??? ??? ??? ??? ??? ?</tr>
?? ??? ??? ??? ??? ??? ?</thead>
?? ??? ??? ??? ??? ??? ?<tbody>
?? ??? ??? ??? ??? ??? ??? ?<c:forEach items="${pagehelper.list}" var="proxyBusiness"? varStatus="i">
?? ??? ??? ??? ??? ??? ??? ??? ?<tr>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>${i.count}</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>${proxyBusiness.proxyName}</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>${proxyBusiness.companyName}</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>${proxyBusiness.proxyBrand}</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>${proxyBusiness.netInfo}</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>${proxyBusiness.proxyArea}</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>${proxyBusiness.cellPhone}</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>${proxyBusiness.proxyEmail}</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>${proxyBusiness.createTime}</td>
?? ??? ??? ??? ??? ??? ??? ??? ?<td>${proxyBusiness.proxyJoinInfo}</td>
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<%-- <td><input type="text" id="id_${proxyBusiness.id }" name="id"
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?value="${proxyBusiness.id }" disabled /></td>
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<td><input type="text" id="name_${proxyBusiness.id }" name="name"
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?value="${proxyBusiness.name }" disabled /></td>
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<td><input type="text" id="age_${proxyBusiness.id }" name="age"
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?value="${proxyBusiness.age }" disabled /></td>
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<td><input type="text" id="birthday_${proxyBusiness.id }"
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?name="birthday" class="Wdate" value="${proxyBusiness.birthdayStr}"
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?onClick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})"
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?disabled readOnly /></td>
?? ??? ??? ??? ??? ??? ??? ??? ??? ?<td>
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?<button id="edit_btn" οnclick="editFun('${person.id }');">編輯</button>
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?<button id="modify" οnclick="modifyFun('${person.id }');">修改</button>
?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?<button id="modify" οnclick="deleteFun('${person.id }');">刪除</button>
?? ??? ??? ??? ??? ??? ??? ??? ??? ?</td> --%>
?? ??? ??? ??? ??? ??? ??? ??? ?</tr>
?? ??? ??? ??? ??? ??? ??? ?</c:forEach>
?? ??? ??? ??? ??? ??? ?</tbody>
?? ??? ??? ??? ??? ?</table>
?? ??? ??? ??? ??? ?<div id="page_div">
?? ??? ??? ??? ??? ??? ?<%@ include file="/WEB-INF/jsp/common/pagehelper.jsp"%>? //分頁的插件
?? ??? ??? ??? ??? ?</div>
?? ??? ??? ??? ?</div>
?? ??? ??? ?</div>
?? ??? ?</div>
?? ?</div>
</body>
</html>
======================================================================
分頁的插件jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!--?? -->
<!-- 頁數(shù) -->
<div class="message">
?? ?共<i class="blue">${pagehelper.total}</i>條記錄,當(dāng)前顯示第 <i
?? ??? ?class="blue">${pagehelper.pageNum}/${pagehelper.pages}</i> 頁
</div>
<div style="text-align:center;">
?? ?<ul class="pagination">
?? ??? ?<!-- <li><a href="#">«</a></li> -->
?? ??? ?<c:if test="${!pagehelper.isFirstPage}">
?? ??? ??? ?<li><a href="javascript:queryAllPerson(${pagehelper.firstPage}, ${pagehelper.pageSize});">首頁</a></li>
?? ??? ??? ?<li><a href="javascript:queryAllPerson(${pagehelper.prePage}, ${pagehelper.pageSize});">上一頁</a></li>
?? ??? ?</c:if>
?? ??? ?<c:forEach items="${pagehelper.navigatepageNums}" var="navigatepageNum">
?? ??? ?
?? ??? ??? ?<c:if test="${navigatepageNum==pagehelper.pageNum}">
?? ??? ??? ??? ?<li class="active"><a href="javascript:queryAllPerson(${navigatepageNum}, ${pagehelper.pageSize});">${navigatepageNum}</a></li>
?? ??? ??? ?</c:if>
?? ??? ??? ?<c:if test="${navigatepageNum!=pagehelper.pageNum}">
?? ??? ??? ??? ?<li><a href="javascript:queryAllPerson(${navigatepageNum}, ${pagehelper.pageSize});">${navigatepageNum}</a></li>
?? ??? ??? ?</c:if>
?? ??? ?</c:forEach>
?? ??? ?<c:if test="${!pagehelper.isLastPage}">
?? ??? ??? ?<li><a href="javascript:queryAllPerson(${pagehelper.nextPage}, ${pagehelper.pageSize});">下一頁</a></li>
?? ??? ??? ?<li><a href="javascript:queryAllPerson(${pagehelper.lastPage}, ${pagehelper.pageSize});">最后一頁</a></li>
?? ??? ?</c:if>
?? ??? ?<!-- <li><a href="#">»</a></li> -->
?? ?</ul>
</div>
============================================================================
css樣式:(用到的是bootstrap的樣式)
/*!
?* Bootstrap v3.3.5 (http://getbootstrap.com)
?* Copyright 2011-2015 Twitter, Inc.
?* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
?*/
/*! normalize.css v3.0.3 | MIT License | github.com/necolas/normalize.css */
==============================================================
效果:
?
?
總結(jié)
以上是生活随笔為你收集整理的mybatis pagehelper实现分页的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL数据库学习笔记(一)----M
- 下一篇: The Road to Ryu: Hi