spring+springMVC+MyBatis 分页功能代码封装

页面效果图展示:

spring+springMVC+MyBatis 分页功能代码封装_第1张图片

分页工具类:Pagination

package com.wlsq.kso.util;

import java.io.Serializable;  
import java.util.ArrayList;  
import java.util.List;  
  
/** 
 * 储存分页处理工具类 在调用此类的方法之前需设置总页数(即得先从数据库查询到相应数据的数据量) 
 *  
 * @author ahomeeye 
 * @version 1.0 
 */  
public class Pagination implements Serializable {  
  
    private static final long serialVersionUID = 1L;  
    private int start; // start表示当前页开始的记录数,start=每页行数*(当前页数-1)  
    private int end; // 当前页结束的记录行数  
    private int totalCount; // 总行数  
    private int rowsPerPage = 1; // 每页行数,默认10  
    private int currentPage; // 当前页数  
    private int pageListSize = 6;// 页码列表大小,默认9  
    private List pageNumList = new ArrayList();  
  
    public Pagination() {  
        start = 0;  
        end = 0;  
        currentPage = 1;  
        this.totalCount = 0;  
    }  
  
    public Pagination(int totalCount) {  
        start = 0;  
        end = 0;  
        currentPage = 1;  
        this.totalCount = totalCount;  
    }  
  
    public Pagination(int totalCount, int numPerPage) {  
        start = 0;  
        end = 0;  
        this.totalCount = totalCount;  
        currentPage = 1;  
        if (numPerPage > 0) {  
            rowsPerPage = numPerPage;  
        }  
    }  
  
    /** 
     * 执行翻页动作 
     *  
     * @param currentPage 
     *            要翻到的目标页码 
     * @return 返回翻页对象 
     */  
    public Pagination doPagination(int currentPage) {  
        gotoPage(currentPage);  
        return this;  
    }  
  
    // 设置起始数  
    public int getStart() {  
        start = rowsPerPage * (currentPage - 1);  
        return start;  
    }  
  
    // 得到起始数  
    public void setStart(int start) {  
        if (start < 0) {  
            this.start = 0;  
        } else if (start >= this.totalCount) {  
            this.start = this.totalCount - 1;  
        } else {  
            this.start = start;  
        }  
    }  
  
    // 设置当前页的最后一行的在总记录中的顺序(从0开始)  
    public void setEnd(int end) {  
        this.end = end;  
    }  
  
    // 得到当前页的最后一行的在总记录中的顺序(从0开始)  
    public int getEnd() {  
        if (rowsPerPage * currentPage > this.totalCount) {  
            end = this.totalCount - 1;  
        } else {  
            end = rowsPerPage * currentPage - 1;  
        }  
        return end;  
    }  
  
    // 以下4个方法供控制器(struts)调用  
  
    // 判断能否到第一页;只要能到上一页,肯定就有第一页  
    public boolean firstEnable() {  
        return previousEnable();  
    }  
  
    // 判断能否到上一页  
    public boolean previousEnable() {  
        return currentPage > 1;// 只要不是第一页,就能到上一页  
    }  
  
    // 判断能否到下一页  
    public boolean nextEnable() {  
        return currentPage * rowsPerPage < this.totalCount;  
    }  
  
    // 判断能否到最后一页;只要有下一页,就肯定有最后一页.  
    public boolean lastEnable() {  
        return nextEnable();  
    }  
  
    // 跳到第一页  
    public void firstPage() {  
        currentPage = 1;  
    }  
  
    // 跳到上一页  
    public void previousPage(int cPage) {  
        currentPage = (cPage - 1) > 0 ? (cPage - 1) : 1;  
    }  
  
    // 跳到下一页  
    public void nextPage(int cPage) {  
        currentPage = cPage + 1;  
        if (currentPage * rowsPerPage > this.totalCount) {  
            lastPage();  
        }  
    }  
  
    // 跳到最后一页  
    public void lastPage() {  
        if (this.totalCount % rowsPerPage == 0) {  
            currentPage = this.totalCount / rowsPerPage;  
        } else {  
            currentPage = this.totalCount / rowsPerPage + 1;  
        }  
    }  
  
    // 跳到指定的某一页  
    public void gotoPage(int pageNumber) {  
        if (pageNumber <= 1) {  
            currentPage = 1;  
        } else if (getTotalCount() < this.getRowsPerPage()) {  
            currentPage = 1;  
        } else if (pageNumber * rowsPerPage >= this.totalCount) {  
            lastPage();  
        } else {  
            currentPage = pageNumber;  
        }  
    }  
  
    // 设置总行数  
    public void setTotalCount(int totalCount) {  
        this.totalCount = totalCount;  
    }  
  
    // 得到总行数  
    public int getTotalCount() {  
        return totalCount;  
    }  
  
    // 设置每页行数  
    public void setRowsPerPage(int rowsPerPage) {  
        this.rowsPerPage = rowsPerPage;  
    }  
  
    // 得到每页行数  
    public int getRowsPerPage() {  
        return rowsPerPage;  
    }  
  
    // 得到总页数  
    public int getPages() {  
        if (this.totalCount % rowsPerPage == 0)  
            return this.totalCount / rowsPerPage;  
        else  
            return this.totalCount / rowsPerPage + 1;  
    }  
  
    // 得到当前页数  
    public int getCurrentPage() {  
        return currentPage;  
    }  
  
    // 设置当前页数  
    public void setCurrentPage(int currentPage) {  
        this.currentPage = currentPage;  
    }  
  
    public int getPageListSize() {  
        return pageListSize;  
    }  
  
    // 设置页码列表大小  
    public void setPageListSize(int pageListSize) {  
        this.pageListSize = pageListSize;  
    }  
  
    // 得到页面列表  
    public List getPageNumList() {  
        this.pageNumList.removeAll(this.pageNumList);// 设置之前先清空  
        int totalPage = getPages();  
        if (totalPage > this.pageListSize) {  
            int halfSize = this.pageListSize / 2;  
            int first = 1;  
            int end = 1;  
            if (this.currentPage - halfSize < 1) { // 当前页靠近最小数1  
                first = 1;  
                end = this.pageListSize;  
            } else if (totalPage - this.currentPage < halfSize) { // 当前页靠近最大数  
                first = totalPage - this.pageListSize + 1;  
                end = totalPage;  
            } else {  
                first = this.currentPage - halfSize;  
                end = this.currentPage + halfSize;  
            }  
            for (int i = first; i <= end; i++) {  
                this.pageNumList.add(i);  
            }  
        } else {  
            for (int i = 0; i < totalPage; i++) {  
                this.pageNumList.add(i + 1);  
            }  
        }  
  
        return pageNumList;  
    } 
}

分页样式和js文件(page.css/page.js)

page.js

$(function(){
	$(document).on("click",".page_num,.next_page,.prev_page,.first_page,.last_page",function(){
		var $self = $(this);
		if($self.parent().attr("class") == 'disabled'){
			return false;
		}
		$("#page_current").val($self.attr("data-pnum"));
		$("#page_form").submit();
	});
});

page.css

.pages{ width:100.5%; text-align:right; padding:10px 0; clear:both;}
.pages a,.pages b{ font-size:12px; font-family:Arial, Helvetica, 
sans-serif; margin:0 2px;}
.pages a,.pages b{ border:1px solid #5FA623; background:#fff; padding:2px 
6px; text-decoration:none}
.pages b,.pages a:hover{ background:#7AB63F; color:#fff;}


后台页面逻辑代码和数据库查询配置文件:

controller 文件

/*开发者查询列表*/
	@RequestMapping(value="/select.action" )	
	public  ModelAndView  Select(HttpServletRequest request){
		 ModelAndView modelAndView = new ModelAndView();
		 int pageSize = Integer
					.parseInt(request.getParameter("pageSize") == null ? "1"
							: request.getParameter("pageSize"));
		 int pageNum = Integer
 					.parseInt(request.getParameter("pageNum") == null ? "1"
							: request.getParameter("pageNum"));
			Map maps = new HashMap();
			maps.put("pageSize", pageSize);
			maps.put("pageNum", (pageNum-1) * pageSize);
			
	    Developer developer = new  Developer();
		List develpers = developerService.selectByDeveloper(maps);
		int count = developerService.selectCountDevelopers();
		Pagination page = new Pagination(count);
		page.setCurrentPage(pageNum);
		modelAndView.addObject("pnums", page.getPageNumList());
		modelAndView.addObject("currentPage", pageNum);
		modelAndView.addObject("pnext_flag", page.nextEnable());
		modelAndView.addObject("plast_flag", page.lastEnable());
		page.lastPage();
		modelAndView.addObject("last_page", page.getCurrentPage());
		modelAndView.addObject("count", count);
		modelAndView.addObject("pageCount", page.getPages());
		if(develpers != null){
			modelAndView.setViewName("backstage/home");
			modelAndView.addObject("developers", develpers);
		}
		return modelAndView;
	}

mybatis配置文件(service 文件名与mybatis 配置文件名一致,这是我们公司老大提供的)

  

项目页面源码:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>





统一认证平台管理后台




































	
	





	
	

开发者管理

新手指南: 新手指南手册点击此处!
近期开发者数据统计

2,500

开发者总数

500

新增开发者

243

活跃开发者

100

新增企业数

开发者+10
开发者编号 账户 唯一编号 真实姓名 性别 QQ 联系电话 邮箱 用户类型 公司名称 公司类型 操作
${count }条数据,${pageCount } 首页 首页 ${pa } ${pa } 尾页 尾页





你可能感兴趣的:(深蓝计划)