论坛首页 Java企业应用论坛

hibernate+dwr+js+html 分页(2)

浏览 1930 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-10-20  
4、业务层Service
package com.linbs.usermanage.service;

import java.util.List;

import com.linbs.core.common.exception.BusinessException;
import com.linbs.usermanage.model.Placard;

public interface PlacardService {
       
        //取得查询条件下的公告数量
	int getPlacardLength(Placard placard);
        //分页查询公告
	List<Placard> getPlacardByPage(int startIndex , int length, Placard placardExample);
	
}

5、业务层Service实现类
package com.linbs.usermanage.service.impl;

import java.util.List;

import org.hibernate.HibernateException;

import com.linbs.core.common.exception.BusinessException;
import com.linbs.core.common.hibernate.BaseHibernateDAO;
import com.linbs.core.common.hibernate.BaseHibernateService;
import com.linbs.core.common.hibernate.HibernateCallBack;
import com.linbs.core.common.hibernate.HibernateProxy;
import com.linbs.usermanage.dao.PlacardDAO;
import com.linbs.usermanage.dao.impl.PlacardDAOImpl;
import com.linbs.usermanage.model.Placard;
import com.linbs.usermanage.service.PlacardService;

public class PlacardServiceImpl extends BaseHibernateService implements
		PlacardService {

	private PlacardDAO placardDAO = new PlacardDAOImpl();
		
	public List<Placard> getPlacardByPage(final int startIndex , final int length, final Placard placardExample){
		HibernateCallBack callBack = new HibernateCallBack(){
			public Object execute()throws HibernateException{
				return placardDAO.getPlacardByPage(startIndex, length, placardExample);
			}
		};
		return (List<Placard>)new HibernateProxy().run(callBack);
	}
	
	public int getPlacardLength(final Placard placard){
		HibernateCallBack callBack = new HibernateCallBack(){
			public Object execute()throws HibernateException{
				return new Integer(placardDAO.getPlacardLength(placard));
			}
		};
		return (Integer)new HibernateProxy().run(callBack);
	}
}

6、Page类
package com.linbs.usermanage.page;

public class Page {
	
	/* 当前页号 */
	private int currentPage = 1;

	/* 总页数 */
	private int totalPage;

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

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

	/* 当前页记录开始索引 */
	private int startIndex;

	/* 记录总数 */
	private int totalRows;

	/* 每页记录数默认为10 */
	private int pageSize = 12;

	/* 页号记录 */
	private int[] pages;
	
	
	public Page() {

	}

	public Page(int totalRows,int pageSize) {
		this.totalRows = totalRows;
		setPageSize(pageSize);
		if (totalRows % pageSize == 0) {
			totalPage = totalRows / pageSize;
		} else {
			totalPage = totalRows / pageSize + 1;
		}
		pages = new int[totalPage];
		for (int i = 0; i < totalPage; i++) {
			pages[i] = i + 1;
		}
		if (totalPage == 1||totalPage == 0) {
			hasPrePage = false;
			hasNextPage = false;
		}
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public boolean isHasPrePage() {
		return hasPrePage;
	}

	public void setHasPrePage(boolean hasPrePage) {
		this.hasPrePage = hasPrePage;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public int getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = (pageSize==0)?12:pageSize;
	}

	public int[] getPages() {
		return pages;
	}

	public void setPages(int[] pages) {
		this.pages = pages;
	}
	
	/**
	 * 返回到第一页
	 */
	public void first() {
		if(totalPage!=0)currentPage = 1;
		startIndex = 0;
		// 如果总记录数不足一页,则该页行数为记录数
		if(currentPage>1){
			hasPrePage = true;
		}else{
			hasPrePage = false;
		}
		if(currentPage<totalPage){
			hasNextPage = true;
		}else{
			hasNextPage = false;
		}
		if (totalRows < pageSize)
			pageSize = totalRows;
	}

	/**
	 * 上一页
	 */
	public void previous() {
		if (currentPage == 1) {
			return;
		}
		// 设置第一页的hasPrePage;
		--currentPage;
		if(currentPage>1){
			hasPrePage = true;
		}else{
			hasPrePage = false;
		}
		if(currentPage<totalPage){
			hasNextPage = true;
		}else{
			hasNextPage = false;
		}
		startIndex = (currentPage - 1) * pageSize;
	}

	/**
	 * 下一页
	 */
	public void next() {
		if (currentPage < totalPage) {
			++currentPage;
		}
		// 设置新的页面的行数
		if(currentPage>1){
			hasPrePage = true;
		}else{
			hasPrePage = false;
		}
		if(currentPage<totalPage){
			hasNextPage = true;
		}else{
			hasNextPage = false;
		}
		startIndex = (currentPage - 1) * pageSize;
		if(currentPage == totalPage)pageSize = totalRows - startIndex;
	}

	/**
	 * 最后一页
	 */
	public void last() {
		currentPage = totalPage;
		if(currentPage>1){
			hasPrePage = true;
		}else{
			hasPrePage = false;
		}
		if(currentPage<totalPage){
			hasNextPage = true;
		}else{
			hasNextPage = false;
		}
		startIndex = (currentPage - 1) * pageSize;
		pageSize = totalRows - startIndex;
	}

	/**
	 * 更新页面
	 */
	public void refresh(int currentPage) {
		this.currentPage = currentPage;
		if(currentPage>1){
			hasPrePage = true;
		}else{
			hasPrePage = false;
		}
		if(currentPage<totalPage){
			hasNextPage = true;
		}else{
			hasNextPage = false;
		}
		if ((totalRows - startIndex) < pageSize)
			pageSize = totalRows - startIndex;
		if (currentPage > totalPage) {
			last();
		}
	}
}
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics