jpa pageutil

package com.netsdar.school.ent;


import java.io.Serializable;
import javax.persistence.*;
import java.util.List;




/**
 * The persistent class for the departments database table.
 * 
 */
@Entity
@Table(name="departments")
public class Department implements Serializable {
private static final long serialVersionUID = 1L;


@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int did;


private String dname;


//bi-directional many-to-many association to Userinfo
    @ManyToMany
@JoinTable(
name="userdepartments"
, joinColumns={
@JoinColumn(name="did")
}
, inverseJoinColumns={
@JoinColumn(name="userid")
}
)
private List<Userinfo> userinfos;


    public Department() {
    }


public int getDid() {
return this.did;
}


public void setDid(int did) {
this.did = did;
}


public String getDname() {
return this.dname;
}


public void setDname(String dname) {
this.dname = dname;
}


public List<Userinfo> getUserinfos() {
return this.userinfos;
}


public void setUserinfos(List<Userinfo> userinfos) {
this.userinfos = userinfos;
}

}

package com.netsdar.school.dao;


import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


import com.netsdar.common.jpa.BaseJpaDao;
import com.netsdar.school.ent.Department;


@Repository
public class DepartmentDao extends BaseJpaDao<Department>{
@Transactional
public void delete(Department d){
this.em.remove(d);
}


}package com.netsdar.school.biz;


import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.netsdar.common.Page;
import com.netsdar.common.Result;
import com.netsdar.school.dao.DepartmentDao;
import com.netsdar.school.ent.Department;
  


@Service
public class DepartmentBiz {


private DepartmentDao departDao;
public DepartmentDao getDepartDao() {
return departDao;
}


@Autowired


public void setDepartDao(DepartmentDao departDao) {
this.departDao = departDao;
}






private EntityManager em;
@PersistenceContext
public void setEm(EntityManager em) {
this.em = em;
}



/*保存部门*/
@Transactional
public void save(Department d){
departDao.save(d);
}
/*删除一个部门*/


@Transactional
public void delete(int did){
Department d=this.getById(did);
this.departDao.delete(d);


}
/*依据id得到相对应的部门*/
public Department getById(int did){
return departDao.getByPk(Department.class, did);
}
public Department getDepartById(Department d){
return departDao.getByPk(Department.class,d.getDid());
}
/*更改部门信息*/
@Transactional
public void updateDepart(Department d){
departDao.update(d);
}
public Result queryDepartresult(Page page){
StringBuffer sql=new StringBuffer();
sql.append("from Department");
Result list=this.departDao.findForResult(page, sql.toString());
return list;
}
 
}

package com.netsdar.school.web;


import java.util.List;


import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import com.netsdar.common.Page;
import com.netsdar.common.Result;
import com.netsdar.school.biz.DepartmentBiz;
import com.netsdar.school.ent.Department;
import com.netsdar.school.ent.Group;




@Controller
public class DepartmentAction {


private DepartmentBiz departmentBiz;


public DepartmentBiz getDepartmentBiz() {
return departmentBiz;
}
@Autowired
public void setDepartmentBiz(DepartmentBiz departmentBiz) {
this.departmentBiz = departmentBiz;
}

/**
 * 列出所有部门
 * 
 */
@RequestMapping("/webadmin/departList.htm")
public String listDepart(HttpServletRequest request){
String currpage =request.getParameter("page");
if(currpage == null || currpage.equals("")){
currpage ="1";
}
Page page=new Page(5);
page.setCurrentPage(new Integer(currpage));
Result departList= departmentBiz.queryDepartresult(page);
request.setAttribute("departList",departList);
return "/webadmin/departList.vm";
}
/**
 * 删除部门
 * 
 */
@RequestMapping("/webadmin/deleteDepart.htm")
public String deleteDepart(HttpServletRequest request){
String did=request.getParameter("did");
if(did != null && !did.equals("")){
if(did.contains(",")){
String[] id=did.split(",");                                                                                                                                                                                                                                                                                                                                                                                                                        
for(int i=0;i<id.length;i++){
departmentBiz.delete(Integer.valueOf(id[i]));
}
}
else{
departmentBiz.delete(Integer.parseInt(did));
}
}
String currpage=request.getParameter("page");

return "/webadmin/departList.htm?page="+currpage;
}
/**
 * 跳转到部门增加页面
 * 
 */


@RequestMapping("/webadmin/addDepart.htm")
public String addDepart(HttpServletRequest request){
String currpage =request.getParameter("page");
request.setAttribute("page", currpage);

return "/webadmin/departDetail.vm";
}


/**
 * 增加用户
 */
@RequestMapping("/webadmin/saveDepart.htm")
public String savegroupFoard(HttpServletRequest request,Department departmentfrom){

departmentBiz.save(departmentfrom);
String currpage =request.getParameter("page");
if(currpage==null||currpage.equals("")){
currpage="1";
}


return "departList.htm?page="+currpage;
}
/**
 * 转到用户组修改页面
 */


@RequestMapping("/webadmin/viewDepart.htm")
public String updatedepartFoard(HttpServletRequest request,Department departfrom){
Department depart=departmentBiz.getDepartById(departfrom);
request.setAttribute("depart1",depart);
String currpage=request.getParameter("page");
request.setAttribute("page",currpage);
return "/webadmin/updateDepart.vm";
}


/**
 * 用户组修改
 */
@RequestMapping("/webadmin/updateDepart.htm")
public String updategroup(HttpServletRequest request,Department departfrom){

departmentBiz.updateDepart(departfrom);
String currpage =request.getParameter("page");


return "departList.htm?page="+currpage;
}




}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新闻列表</title>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
.STYLE1 {font-size: 12px}
.STYLE3 {font-size: 12px; font-weight: bold; }
.STYLE4 {
color: #03515d;
font-size: 12px;
}
a:link {
font-size: 12px;
color: #000000;
text-decoration: none;
}
a:visited {
font-size: 12px;
color: #000000;
text-decoration: none;
}
a:hover {
font-size: 12px;
color: #333333;
text-decoration: underline;
}
-->
</style>


<script>
var  highlightcolor='#c1ebff';
//此处clickcolor只能用win系统颜色代码才能成功,如果用#xxxxxx的代码就不行,还没搞清楚为什么:(
var  clickcolor='#51b2f6';
function  changeto(){
source=event.srcElement;
if  (source.tagName=="TR"||source.tagName=="TABLE")
return;
while(source.tagName!="TD")
source=source.parentElement;
source=source.parentElement;
cs  =  source.children;
//alert(cs.length);
if  (cs[1].style.backgroundColor!=highlightcolor&&source.id!="nc"&&cs[1].style.backgroundColor!=clickcolor)
for(i=0;i<cs.length;i++){
cs[i].style.backgroundColor=highlightcolor;
}
}


function  changeback(){
if  (event.fromElement.contains(event.toElement)||source.contains(event.toElement)||source.id=="nc")
return
if  (event.toElement!=source&&cs[1].style.backgroundColor!=clickcolor)
//source.style.backgroundColor=originalcolor
for(i=0;i<cs.length;i++){
cs[i].style.backgroundColor="";
}
}


function  clickto(){
source=event.srcElement;
if  (source.tagName=="TR"||source.tagName=="TABLE")
return;
while(source.tagName!="TD")
source=source.parentElement;
source=source.parentElement;
cs  =  source.children;
//alert(cs.length);
if  (cs[1].style.backgroundColor!=clickcolor&&source.id!="nc")
for(i=0;i<cs.length;i++){
cs[i].style.backgroundColor=clickcolor;
}
else
for(i=0;i<cs.length;i++){
cs[i].style.backgroundColor="";
}
}
</script>
<script type="text/javascript">
function zPage(){
var page = document.getElementById('zPage').value; 
var did= '$!{did}';
var totalPage = '$!{departList.page.totalPage}';
if(page <= totalPage&&page>0){
window.location.href="$!{request.contextPath}/webadmin/departList.htm?page="+page+"&did="+did;
}else{
alert("没有此页!");
}

}


function del(did,crrpage){
if(confirm("确定要删除此信息吗?")){

window.location.href="$!{request.contextPath}/webadmin/deleteDepart.htm?did="+did+"&page="+crrpage;
}
}



</script>
</head>


<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">  
  <tr>
  <td height="2"></td>
  </tr>
  <tr>
    <td height="30" background="$!{request.contextPath}/webadmin/tab/images/tab_05.gif">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="12" height="30"><img src="$!{request.contextPath}/webadmin/tab/images/tab_03.gif" width="12" height="30" /></td>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="46%" valign="middle"><table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="5%"><div align="center"><img src="$!{request.contextPath}/webadmin/tab/images/tb.gif" width="16" height="16" /></div></td>
                <td width="95%" class="STYLE1"><span class="STYLE3">当前位置</span>:系统设置管理平台>部门管理</td>
              </tr>
            </table></td>
            <td width="54%"><table border="0" align="right" cellpadding="0" cellspacing="0">
              <tr>
                <td width="60"><table width="90%" border="0" cellpadding="0" cellspacing="0">
                  <tr>
                    <td class="STYLE1"><div align="center"><img src="$!{request.contextPath}/webadmin/tab/images/22.gif" width="14" height="14" /></div></td>
                    <td class="STYLE1"><div align="center"><a href="$!{request.contextPath}/webadmin/addDepart.htm?page=$!{departList.page.currentPage}">新增</a></div></td>
                  </tr>
                </table>
                </td>
              </tr>
            </table></td>
          </tr>
        </table></td>
        <td width="16"><img src="$!{request.contextPath}/webadmin/tab/images/tab_07.gif" width="16" height="30" /></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td><form name="form1" action="" method="post"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="8" background="$!{request.contextPath}/webadmin/tab/images/tab_12.gif">&nbsp;</td>
        <td><table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="bce4ff" οnmοuseοver="changeto()"  οnmοuseοut="changeback()">
          <tr>
            <td width="5%" height="22" background="$!{request.contextPath}/webadmin/tab/images/bg.gif" bgcolor="#FFFFFF"><div align="center">
              &nbsp;
            </div></td>
            <td width="19%" height="22" background="$!{request.contextPath}/webadmin/tab/images/bg.gif" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">部门名称</span></div></td>
            <td height="22" background="$!{request.contextPath}/webadmin/tab/images/bg.gif" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">部门简介</span></div></td>
            <td width="20%" height="22" background="$!{request.contextPath}/webadmin/tab/images/bg.gif" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1"></span></div></td>
          </tr>
          #foreach($depart in $departList.list)
          <tr>

            <td height="20" bgcolor="#FFFFFF"><div align="center">
              <input type="checkbox" name="mm" value="$!{depart.id}" />
            </div></td>
            <td height="20" bgcolor="#FFFFFF"><div align="center" class="STYLE1">
              <div align="center">$!{depart.dname}</div>
            </div></td>
            <td height="20" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">$!{depart.content}</span></div></td>
            <td height="20" bgcolor="#FFFFFF"><div align="center"><span class="STYLE4"><img src="$!{request.contextPath}/webadmin/tab/images/edt.gif" width="16" height="16" /><a href="$!{request.contextPath}/webadmin/viewDepart.htm?did=$!{depart.did}&page=$!{departList.page.currentPage}">编辑</a>&nbsp; &nbsp;
            <img src="$!{request.contextPath}/webadmin/tab/images/del.gif" width="16" height="16" /><a href="javascript:del('$!{depart.did}','$!{departList.page.currentPage}')">删除</a></span></div></td>
          </tr>
          #end
         </table>
         </td>
        <td width="8" background="$!{request.contextPath}/webadmin/tab/images/tab_15.gif">&nbsp;</td>
      </tr>
    </table></form></td>
  </tr>
  <tr>
    <td height="35" background="$!{request.contextPath}/webadmin/tab/images/tab_19.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="12" height="35"><img src="$!{request.contextPath}/webadmin/tab/images/tab_18.gif" width="12" height="35" /></td>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td class="STYLE4">&nbsp;&nbsp;共有$!{departList.page.totalCount}条记录,当前第$!{departList.page.currentPage}/$!{departList.page.totalPage} 页</td>
            <td><table border="0" align="right" cellpadding="0" cellspacing="0">
                <tr>
<td width="40"><a href="$!{request.contextPath}/webadmin/departList.htm?page=1&did=$!{groupId1}"><img src="$!{request.contextPath}/webadmin/tab/images/first.gif" width="37" height="15" border="0" /></a></td>
                  <td width="45"><a href="$!{request.contextPath}/webadmin/departList.htm?page=$!{departList.page.previousPage}&did=$!{groupId1}"><img src="$!{request.contextPath}/webadmin/tab/images/back.gif" width="43" height="15" border="0" /></a></td>
                  <td width="45"><a href="$!{request.contextPath}/webadmin/departList.htm?page=$!{departList.page.nextPage}&did=$!{groupId1}"><img src="$!{request.contextPath}/webadmin/tab/images/next.gif" width="43" height="15" border="0" /></a></td>
                  <td width="40"><a href="$!{request.contextPath}/webadmin/departList.htm?page=$!{departList.page.totalPage}&did=$!{groupId1}"><img src="$!{request.contextPath}/webadmin/tab/images/last.gif" width="37" height="15" border="0" /></a></td>
                  
                 <td width="100"><div align="center"><span class="STYLE1">转到第
                    <input  type="text" name="page" size="4" id="zPage" style="height:12px; width:20px; border:1px solid #999999;" /> 
                    页 </span></div></td>
                  <td width="40"><a οnclick="zPage();"><img src="$!{request.contextPath}/webadmin/tab/images/go.gif" width="37" height="15" border="0" /></a></td>
                </tr>
            </table></td>
          </tr>
        </table></td>
        <td width="16"><img src="$!{request.contextPath}/webadmin/tab/images/tab_20.gif" width="16" height="35" /></td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>

package com.netsdar.common;


import java.util.List;


/**
 * 功能:对分页属性的封装
 * 
 * @author Administrator
 * 
 */
public class Page {


/** 是否有上一页 */
private boolean hasPrePage;


/** 是否有下一页 */
private boolean hasNextPage;


/** 分页大小 */
private int everyPage;


/** 总页数 */
private int totalPage;


/** 当前页 */
private int currentPage;


/** 记录起始数 */
private int beginIndex;


/** 下一页 */
private int nextPage;


/** 上一页 */
private int previousPage;


/** 总记录数 */
private long totalCount;


/** 基于数字显示的数组 */
@SuppressWarnings("rawtypes")
private List displayNum;


/** 跳转到下一个显示的数组 */
private int nextDisplayNum;


/** 跳转到上一个显示的数组 */
private int previousDisplayNum;




public int getNextDisplayNum() {
return nextDisplayNum;
}


public void setNextDisplayNum(int nextDisplayNum) {
this.nextDisplayNum = nextDisplayNum;
}


@SuppressWarnings("rawtypes")
public List getDisplayNum() {
return displayNum;
}


@SuppressWarnings("rawtypes")
public void setDisplayNum(List displayNum) {
this.displayNum = displayNum;
}


/**
* 默认的构造函数



*/
public Page() {


}


/**
* 用分页大小构造分页对象



* @param everyPage
*            分页大小
*/
public Page(int everyPage) {
this.everyPage = everyPage;
}


/**
* 完整的构造分页对象的方法

* @param hasPrePage
*            是否有上页


* @param hasNextPage
*            是否有下页


* @param everyPage
*            分页大小
* @param totalPage
*            总页数


* @param currentPage
*            当前页


* @param beginIndex
*            记录起始数


* @param previousPage
*            上页页数
* @param nextPage
*            下页页数
* @param totalCount
*            总记录数
*/


@SuppressWarnings("rawtypes")
public Page(boolean hasPrePage, boolean hasNextPage, int everyPage,
int totalPage, int currentPage, int beginIndex, int previousPage,
int nextPage, long totalCount, List displayNum,
int nextDisplayNum, int previousDisplayNum) {
this.totalCount = totalCount;
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
this.everyPage = everyPage;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.previousPage = previousPage;
this.nextPage = nextPage;
this.displayNum = displayNum;
this.nextDisplayNum = nextDisplayNum;
this.previousDisplayNum = previousDisplayNum;
}


/**
* @return 记录起始数.
*/
public int getBeginIndex() {
return beginIndex;
}


/**
* @param beginIndex
*            设置记录起始位置.
*/
public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
}


/**
* @return 当前页数.
*/
public int getCurrentPage() {
return currentPage;
}


/**
* @param currentPage
*            设置当前页页数.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}


/**
* @return 分页大小,即每页显示多少条记录.
*/
public int getEveryPage() {
return everyPage;
}


/**
* @param everyPage
*            设置分页大小.
*/
public void setEveryPage(int everyPage) {
this.everyPage = everyPage;
}


/**
* @return 是否有下页.
*/
public boolean getHasNextPage() {
return hasNextPage;
}


/**
* @param hasNextPage
*            设置是否有下页.
*/
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}


/**
* @return 是否有上页.
*/
public boolean getHasPrePage() {
return hasPrePage;
}


/**
* @param hasPrePage
*            设置是否有上页.
*/
public void setHasPrePage(boolean hasPrePage) {
this.hasPrePage = hasPrePage;
}


/**
* @return 总页数.
*/
public int getTotalPage() {
return totalPage;
}


/**
* @param totalPage
*            设置总页数.
*/
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}


/**
* @return 总记录数
*/
public long getTotalCount() {
return totalCount;
}


/**

* @param totalCount
*            设置总记录数.
*/
public void setTotalCount(long totalCount) {
this.totalCount = totalCount;
}


/**

* @return 下页页数
*/
public int getNextPage() {
return nextPage;
}


/**
* @param nextPage
*            设置下页页数.
*/
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}


/**

* @return 上页页数
*/
public int getPreviousPage() {
return previousPage;
}


/**

* @param previousPage
*            设置上页页数.
*/
public void setPreviousPage(int previousPage) {
this.previousPage = previousPage;
}


public int getPreviousDisplayNum() {
return previousDisplayNum;
}


public void setPreviousDisplayNum(int previousDisplayNum) {
this.previousDisplayNum = previousDisplayNum;
}


}

package com.netsdar.common;


import java.util.List;






public class Result<T> {

private Page page;

private List<T> list;


public Page getPage() {
return page;
}


public void setPage(Page page) {
this.page = page;
}


public List<T> getList() {
return list;
}


public void setList(List<T> list) {
this.list = list;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Result(Page page, List list) { 
       this.page = page; 
       this.list = list; 
   } 
}

package com.netsdar.utils;


import java.util.LinkedList;
import java.util.List;


import com.netsdar.common.Page;


public class PageUtil {

/**
* 用封装的分页对象创建分页的方法



* @param page
*            分页对象
* @param totalRecords
*            总记录数
* @return
*/
public static Page createPage(Page page, long totalRecords) {
return createPage(page.getEveryPage(), page.getCurrentPage(),
totalRecords);
}


/**
* 用传入的基本参数创建分页的方法



* @param everyPage
*            分页大小
* @param currentPage
*            当前页


* @param totalRecords
*            总记录数
* @return page
*/
public static Page createPage(int everyPage, int currentPage,
long totalRecords) {
/** 分页大小 */
everyPage = getEveryPage(everyPage);
/** 当前页 */
currentPage = getCurrentPage(currentPage);

if(currentPage < 1){
currentPage = 1;
}
/** 记录起始数 */
int beginIndex = getBeginIndex(everyPage, currentPage);
/** 总页数 */
int totalPage = getTotalPage(everyPage, totalRecords);

if(totalPage<1){
totalPage = 1;
}
/**应该是先算出当前页的页数,再算

    */
if(currentPage > totalPage){
currentPage=totalPage;
beginIndex = getBeginIndex(everyPage, currentPage);
}


/** 是否有下页 */
boolean hasNextPage = hasNextPage(currentPage, totalPage);
/** 是否有上页 */
boolean hasPrePage = hasPrePage(currentPage);
/** 上一页 */
int previousPage = getPreviousPage(currentPage);
/** 下一页 */
int nextPage = getNextPage(totalPage, currentPage);

int numLenth = 7;
/** 显示数字的集合 */
List<Integer> displayNum = getDisplayNum(currentPage, totalPage,numLenth);
/** 下一个跳转数 */
int middle = 0;


/*if(numLenth/2>displayNum.size() && displayNum.size()>0){
middle = displayNum.get((displayNum.size())/2);
}else{
middle = displayNum.get(numLenth/2);
}*/

/*try {
if(numLenth/2>displayNum.size() && displayNum.size()>0){

middle = displayNum.get((displayNum.size())/2);
}else{
middle = displayNum.get(numLenth/2);
}
} catch (Exception e) {
middle=1;
e.printStackTrace();
}*/


int nextDisplayNum = getNextDisplayNum(middle, totalPage,
numLenth);
/** 上一个跳转数 */
int previousDisplayNum = getPreviousDisplayNum(middle,numLenth);

return new Page(hasPrePage, hasNextPage, everyPage, totalPage,
currentPage, beginIndex, previousPage, nextPage, totalRecords,displayNum,
nextDisplayNum, previousDisplayNum);

}




private static int getPreviousDisplayNum(int currentPage, int numLenth) {
// TODO Auto-generated method stub
if(currentPage - numLenth > 0){
return currentPage - numLenth;
}else if(currentPage - (numLenth+1)/2 > 0 ){
return 1;
}
return 0;
}


private static int getNextDisplayNum(int currentPage, int totalPage,
int numLenth) {
// TODO Auto-generated method stub
if(currentPage + numLenth < totalPage){
return currentPage + numLenth;
}else if(currentPage + (numLenth+1)/2 < totalPage ){
return totalPage;
}
return 0;
}


/**
* 取得下页页数

* @param totalPage
*            总页数


* @param curpage
*            当前页


* @return int 下页页数
*/
private static int getNextPage(int totalPage, int curpage) {
if (totalPage == curpage)
return totalPage;
else
return curpage + 1;
}


/**
* 取得上页页数

* @param curpage
*            当前页


* @return int 上页页数
*/
private static int getPreviousPage(int curpage) {
if (curpage == 1)
return 1;
else
return curpage - 1;
}


/**
* 取得分页大小

* @param everyPage
*            分页大小值,类型为整型


* @return int 分页大小,即每页显示多少条记录
*/
private static int getEveryPage(int everyPage) {
// 如果分页为0,则默认为10
return everyPage == 0 ? 10 : everyPage;
}


/**
* 取得当前页数

* @param currentPage
*            当前页数
* @return int 当前页数
*/
private static int getCurrentPage(int currentPage) {
// 如果当前页为0 ,则默认为1
return currentPage == 0 ? 1 : currentPage;
}


/**
* 取得记录起始数



* @param everyPage
*            分页大小
* @param currentPage
*            当前页


* @return int 记录起始数


*/
private static int getBeginIndex(int everyPage, int currentPage) {
return (currentPage - 1) * everyPage;
}


/**
* 取得总页数



* @param everyPage
*            分页大小
* @param totalRecords
*            总记录数
* @return int 总页数


*/
private static int getTotalPage(int everyPage, long totalRecords) {
return (int) ((totalRecords+everyPage-1) / everyPage);
}


/**
* 判断是否有上页



* @param currentPage
*            当前页


* @return boolean 有上页则返回true,否则false;
*/
private static boolean hasPrePage(int currentPage) {
// 若当前页为1,则没有上页


return currentPage == 1 ? false : true;
}


/**
* 判断是否有下页



* @param currentPage
*            当前页


* @param totalPage
*            总页数


* @return boolean 有下页则返回true,否则false;
*/
private static boolean hasNextPage(int currentPage, int totalPage) {
// 若当前页为最后页,则没有下页


return currentPage == totalPage || totalPage == 0 ? false : true;
}


/** 按一定的基数显示数字页码 */
private static List<Integer> getDisplayNum(int currentPage, int totalPage,
int baseNum) {

LinkedList<Integer> nums = new LinkedList<Integer>();
int count = 1;
int half = (baseNum-1)/2;
nums.add(currentPage);
if(currentPage-1 < totalPage-currentPage){
for(int i=0 ; i<half ;i++){
int num = currentPage-i-1;
if(num < 1){
break;
}else{
nums.addFirst(num);
count++;
}
}
for(int j=0 ; count<baseNum ; j++){
int num = currentPage + j + 1;
if(num > totalPage){
return nums;
}else{
nums.addLast(num);
count++;
}
}
}else{
for(int j=0 ; j<half ; j++){
int num = currentPage + j + 1;
if(num > totalPage){
break;
}else{
nums.addLast(num);
count++;
}
}
for(int i=0 ;count<baseNum ;i++){
int num = currentPage-i-1;
if(num < 1){
return nums;
}else{
nums.addFirst(num);
count++;
}
}
}
return nums;
}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值