一、在bean中加入分頁(yè)類BaseBean
1寓盗、currentPage表示當(dāng)前頁(yè)
2灌砖、pageSize表示一頁(yè)的數(shù)據(jù)展示數(shù)量
public class BaseBeen {
private int currentPage =1;//當(dāng)前頁(yè)
private int pageSize=5;//一頁(yè)的展示數(shù)量
private int start;
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
}
二、讓圖書類BookBean繼承于BaseBean
三傀蚌、新建一個(gè)util包周崭,在里面添加分頁(yè)工具類Paging:
public class Paging {
public static String getPage(BaseBeen baseBean, int count, String url, Map<String,String[]> map) {
int pageSize = baseBean.getPageSize(); //獲取每頁(yè)的數(shù)量
int pageCount = count%pageSize==0?count/pageSize:count/pageSize+1;//計(jì)算總頁(yè)數(shù)
if(pageCount==0){ //如果總頁(yè)數(shù)是0,就沒(méi)有分頁(yè)
return "";
}
//獲取當(dāng)前頁(yè)碼
int currentPage = baseBean.getCurrentPage();
//如果當(dāng)前頁(yè)大于總頁(yè)數(shù), 將當(dāng)前頁(yè)設(shè)置為最后一頁(yè)
if(currentPage>pageCount){
currentPage=pageCount;
}
//如果當(dāng)前頁(yè)小于等于0,將當(dāng)前頁(yè)設(shè)置為1
if(currentPage<=0){
currentPage=1;
}
//計(jì)算分頁(yè)的起始位置
int start = (currentPage-1)*pageSize; // 計(jì)算開(kāi)始位置
//將計(jì)算好的起始位置存到 Javabean,便于之后查詢數(shù)據(jù)
baseBean.setStart(start);
//用戶如果帶有參數(shù), name我們拼接的url上頁(yè)也應(yīng)該帶著參數(shù), 這樣點(diǎn)擊的時(shí)候, 請(qǐng)求后臺(tái)時(shí),才能把參數(shù)帶過(guò)去, 獲取的結(jié)果,也是條件篩選的結(jié)果
StringBuilder sb = new StringBuilder(url);
sb.append("?"); //將原來(lái)的url和?拼接到一起, 形成一個(gè)頭
//遍歷request中的參數(shù), 將key和value拼接成請(qǐng)求 key=value&
for (Map.Entry<String, String[]> entry : map.entrySet()) {
String key = entry.getKey();
//因?yàn)橄旅嬉呀?jīng)拼接了當(dāng)前頁(yè)面,那么,currentPage就不需要在這里拼接了
if(!"currentPage".equals(key)){
String[] values = entry.getValue();
//請(qǐng)求時(shí),可能有一個(gè)參數(shù)名對(duì)應(yīng)多個(gè)值的情況
for (String value : values) {
sb.append(key.concat("=").concat(value).concat("&"));
}
}
}
url = sb.toString(); //將拼接好的StringBuilder轉(zhuǎn)成字符串,已備使用
//拼接按鈕
sb = new StringBuilder();
//如果當(dāng)前頁(yè)是第一頁(yè), 首頁(yè)和上一頁(yè)都不能點(diǎn)擊
if(currentPage==1){
sb.append("<button><a >首頁(yè)</a></button>");
sb.append("<button><a >上一頁(yè)</a></button>");
}else{
sb.append("<button><a href='"+url+"currentPage=1'>首頁(yè)</a></button>");
sb.append("<button><a href='"+url+"currentPage="+(currentPage-1)+"'>上一頁(yè)</a></button>");
}
//遍歷拼接頁(yè)碼
if(pageCount<=7){
for (int i = 1; i <= pageCount; i++) {
if(i==currentPage){
sb.append("<button><a >"+i+"</a></button>");
}else{
sb.append("<button><a href='"+url+"currentPage="+i+"'>"+i+"</a></button>");
}
}
}else{
//在多頁(yè)面情況下會(huì)有省略好,我們使用保持按鈕的總數(shù)是9個(gè), 當(dāng)前頁(yè)的前后兩個(gè)顯示顯示出來(lái),當(dāng)前頁(yè)的前后第三個(gè)用省略號(hào)代替
//但是,但用戶點(diǎn)擊到第一頁(yè)時(shí), 后面之后兩個(gè)按鈕和一個(gè)省略號(hào),總數(shù)不足9個(gè), 當(dāng)前頁(yè)前面不夠了, 之后再后面多顯示幾個(gè)按鈕
int n = 2;
if(currentPage==1||currentPage==pageCount){
n = 5;
}else if(currentPage==2||currentPage==pageCount-1){
n = 4;
}else if(currentPage==3||currentPage==pageCount-2){
n = 3;
}
for (int i = 1; i <= pageCount; i++) {
if(i==currentPage){
sb.append("<button><a >"+i+"</a></button>");
}else if(i>=currentPage-n&&i<=currentPage+n){
sb.append("<button><a href='"+url+"currentPage="+i+"'>"+i+"</a></button>");
}else if(i==currentPage-(n+1)||i==currentPage+(n+1)){
sb.append("<button><a >...</a></button>");
}
}
}
//如果當(dāng)前頁(yè)時(shí)最后一頁(yè),那么下一頁(yè)和尾頁(yè)都不能點(diǎn)擊
if(currentPage==pageCount){
sb.append("<button><a >下一頁(yè)</a></button>");
sb.append("<button><a >尾頁(yè)</a></button>");
}else{
sb.append("<button><a href='"+url+"currentPage="+(currentPage+1)+"'>下一頁(yè)</a></button>");
sb.append("<button><a href='"+url+"currentPage="+pageCount+"'>尾頁(yè)</a></button>");
}
//拼接跳轉(zhuǎn)頁(yè)面按鈕和js代碼
sb.append("<input id='pageNum' type='number'/><button onclick='toPage()'>跳轉(zhuǎn)</button>");
sb.append("<script>function toPage(){" +
"location.href='"+url+"currentPage='+document.getElementById('pageNum').value"+
"}</script>");
sb.append("<span>共 "+pageCount+" 頁(yè)</span>");
return sb.toString();
}
}
四、在BookMapper.xml添加SQL語(yǔ)句喳张,獲取所有圖書數(shù)量以及分頁(yè)查詢圖書
<!--查詢?nèi)繄D書(進(jìn)行展示),以及分頁(yè)展示-->
<select id="getAllBooks" resultType="com.qianfeng.bean.BookBean">
select * from springmvcbook limit #{start},#{pageSize}
</select>
<!--獲取圖書數(shù)量,便于分頁(yè)-->
<select id="getBookCount" resultType="int">
select count (1) from springmvcbook
</select>
五续镇、dao接口編寫
/*查詢?nèi)繄D書(進(jìn)行展示),以及分頁(yè)展示*/
List<BookBean> getAllBooks(BookBean bookBean);
/*獲取圖書數(shù)量,用于分頁(yè)*/
int getBookCount();
六销部、service層編寫
public List<BookBean> getAllBooks(BookBean bookBean){
return bookDao.getAllBooks(bookBean);
}
public int getBookCount(){
return bookDao.getBookCount();
}
七摸航、在controller里的查詢所有圖書展示的方法代碼里加入分頁(yè)代碼
1、想要點(diǎn)擊下一頁(yè)時(shí)數(shù)據(jù)變化舅桩,一定要將注意執(zhí)行順序酱虎,即分頁(yè)的這幾行代碼要放在展示代碼之上。原因是如果順序不對(duì)就會(huì)在執(zhí)行分頁(yè)之前就執(zhí)行了查詢語(yǔ)句擂涛,start不變读串,查的一直是limit(0,5)
2、向bookService.getAllBook(bookBean)中放bookBean,是為了 獲取start恢暖,pageSize排监,currentPage分頁(yè),因?yàn)閎ookBean繼承于BaseBeen
/*
* 查詢所有圖書展示
* */
@RequestMapping("/book")
public String getAllBooks(HttpSession session, HttpServletRequest request,BookBean bookBean){
/*
* 想要點(diǎn)擊下一頁(yè)時(shí)數(shù)據(jù)變化杰捂,一定要將注意執(zhí)行順序舆床,
* 即分頁(yè)的這幾行代碼要放在展示代碼之上。原因是如果順序不對(duì)就會(huì)在執(zhí)行分頁(yè)之前就
* 執(zhí)行了查詢語(yǔ)句嫁佳,start不變挨队,查的一直是limit(0,5)
* */
int count = bookService.getBookCount();
String paging = Paging.getPage(bookBean,count,"/book",request.getParameterMap());
session.setAttribute("paging",paging);
/*
* 向bookService.getAllBook(bookBean)中放bookBean,是為了
* 獲取start蒿往,pageSize盛垦,currentPage分頁(yè),因?yàn)閎ookBean
* 繼承于BaseBeen
* */
List<BookBean> list = bookService.getAllBooks(bookBean);
session.setAttribute("list",list);
return "book.jsp";
}
八瓤漏、將分頁(yè)按鈕放在頁(yè)面展示
<div id="paging">${paging}</div>
效果圖如下: