hibernate實現分頁

(1)jsp頁面代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ include file="/taglibs.jsp"%>
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>管理產品信息</title>
<link rel="stylesheet" type="text/css" href="../style/main.css"/>

</head>
<script type="text/javascript">

function startLink(productId){
if(confirm("你確信要啓用該產品嗎?")){
productListForm.action="startProduct.action?proId="+productId;
productListForm.submit();
return true;
}else{
return false;
}
}

function stopLink(productId){
if(confirm("你確信要停用該產品嗎?")){
productListForm.action="stopProduct.action?proId="+productId;
productListForm.submit();
return true;
}else{
return false;
}
}

function deleteLink(productId){
if(confirm("你確信要刪除該產品嗎?")){
productListForm.action="deleteProduct.action?proId="+productId;
productListForm.submit();
return true;
}else{
return false;
}
}

function goPagelink(){
var go = document.getElementById("goPage").value;
document.location="toAppointPage.action?goPage="+go;
return true;
}

</script>
<body>
<div style="color:red">
<s:fielderror></s:fielderror>
</div>
<s:form action="productList" name="productListForm" method="post" theme="simple">
<div class="mainbox list">
<div class="morearea">
<a href="<%=request.getContextPath()%>/product/toAddProduct.action">發佈產品信息</a>
</div>
<h1>管理產品信息</h1>
<table border="0" cellspacing="0" cellpadding="0" class="table" bordercolor="#cccccc">
<tbody>
<tr>
<th width="10%">編號</th>
<th width="25%">產品名稱</th>
<th width="20%">創建時間</th>
<th width="20%">結束時間</th>
<th width="25%">操作</th>
</tr>
<c:forEach items="${cfProducts}" var="cfProduct">
<tr style=" width : 747px;">
<td><c:out value="${cfProduct.proId}"/></td>
<td><c:out value="${cfProduct.proTitle}"/></td>
<td><fmt:formatDate value="${cfProduct.proCreatTime}" pattern="yyyy-MM-dd HH:mm"/></td>
<td><fmt:formatDate value="${cfProduct.proEndTime}" pattern="yyyy-MM-dd HH:mm"/></td>
<td class="operate">
<a href="<%=request.getContextPath()%>/product/toUpdateProduct.action?proId=${cfProduct.proId}">修改</a>
<s:if test="cfProduct.proIsLock==0">
<span>|</span>
<a οnclick="startLink('${cfProduct.proId}')">啓用</a>
</s:if>
<s:else>
<span>|</span>
<a οnclick="stopLink('${cfProduct.proId}')" class="operate">停止</a>
</s:else>
<span>|</span>
<a οnclick="deleteLink('${cfProduct.proId}')" class="operate">刪除</a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="5">
<div class="page">
<span class="disabled">第<s:text name="page.currentPage"></s:text>頁</span>
<span class="disabled">共<s:text name="page.totalPage"></s:text>頁</span>
<s:if test="page.hasPrePage">
<a href="<%=request.getContextPath()%>/product/toAppointPage.action?goPage=1">首頁</a>
<a href="<%=request.getContextPath()%>/product/toAppointPage.action?goPage=${page.currentPage-1}">上一頁</a>
</s:if>
<s:else>
<span class="disabled">首頁</span>
<span class="disabled">上一頁</span>
</s:else>
<s:if test="page.hasNextPage">
<a href="<%=request.getContextPath()%>/product/toAppointPage.action?goPage=${page.currentPage+1}">下一頁</a>
<a href="<%=request.getContextPath()%>/product/toAppointPage.action?goPage=${page.totalPage}">尾頁</a>
</s:if>
<s:else>
<span class="disabled">下一頁</span>
<span class="disabled">尾頁</span>
</s:else>
<span class="disabled">到</span>
<s:textfield name="goPage" id="goPage"></s:textfield>
<span class="disabled">頁</span>
<a οnclick="goPagelink()">Go</a>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</s:form>
</body>
</html>
(2)struts配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="product" extends="struts-default">
<action name="toAddProduct" class="productAction" method="toAddProduct">
<result>/product/product_add.jsp</result>
</action>

<action name="addProduct" class="productAction" method="addProduct">
<!--
<interceptor-ref name ="fileUpload" >
<param name ="allowedTypes" >
image/bmp,image/png,image/gif,image/jpeg,image/jpg
</param >
</interceptor-ref >
<interceptor-ref name ="defaultStack" />
-->
<result name="success" type="redirect">product/productList.action</result>
<result name="input">/product/product_add.jsp</result>
</action>

<action name="productList" class="productAction" method="listProducts">
<result>/product/product_list.jsp</result>
</action>

<action name="toUpdateProduct" class="productAction" method="toUpdateProduct">
<result name="success">/product/product_updata.jsp</result>
</action>

<action name="updateProduct" class="productAction" method="updateProduct">
<!--
<interceptor-ref name ="fileUpload" >
<param name ="allowedTypes" >
image/bmp,image/png,image/gif,image/jpeg,image/jpg
</param >
</interceptor-ref >
<interceptor-ref name ="defaultStack" />
-->
<result name="success" type="redirect">product/productList.action</result>
<result name="input">/product/product_updata.jsp</result>
</action>

<action name="startProduct" class="productAction" method="startProduct">
<result name="success" type="redirect">product/productList.action</result>
<result name="input">/product/product_list.jsp</result>
</action>

<action name="stopProduct" class="productAction" method="stopProduct">
<result name="success" type="redirect">product/productList.action</result>
<result name="input">/product/product_list.jsp</result>
</action>

<action name="toAppointPage" class="productAction" method="toAppointPage">
<result name="success">/product/product_list.jsp</result>
</action>

<action name="deleteProduct" class="productAction" method="deleteProduct">
<result name="success" type="redirect">product/productList.action</result>
<result name="input">/product/product_list.jsp</result>
</action>

</package>
</struts>

(3)action類
/**
* <p>ProductAction</p>
*
* 版權 (c) 2009
*
* <p>CFSW</p>
*
* 文件歷史
* 日期 作者 描述
* 2009-10-09 xiangcheng.yue 創建
*
*/
package product.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import product.beans.CfProduct;
import product.beans.CfProductclass;
import product.service.ProductService;
import util.IdGenerator;

import com.opensymphony.xwork2.ActionSupport;
import common.beans.Page;
import common.util.ConstantUtil;
import common.util.PageUtil;

/**
* 產品的查看,發佈,修改,刪除的Action.
*
* @author xiangcheng.yue
* @version 1.0
*/
public class ProductAction extends ActionSupport {

private static final long serialVersionUID = 1L;

private static final int BUFFER_SIZE = 160 * 1024;
/** 產品Service。*/
private ProductService productService;
/** 產品對象。*/
private CfProduct cfProduct;
/** 企業對象。 */
//private CfCorporation cfCorporation;
/** 產品對象List。*/
private List<CfProduct> cfProducts;
/** 產品Id。*/
private String proId;
/** 產品修改頁面是否修改產品圖片標誌,"0"表示沒有修改,"1"表示修改。 */
private String updatePicFlag = "0";
/** 產品發佈及修改頁面是否修改企業信息的標誌,"0"表示沒有修改,"1"表示修改。 */
//private String updateCorporationFlag = "0";
/** 產品類型List。 */
private List<CfProductclass> productclassList;
/** 圖片對象,爲實現產品圖片的上傳及顯示。 */
//private Picture pic = new Picture();
/** 分頁對象,爲實現產品List的分頁。 */
private Page page = new Page();
/** 跳轉頁面。 */
private int goPage;
/** 爲實現產品圖片的上傳及顯示。 */
private File myFile;
private String contentType;
private String fileName;
private String imageFileName;
private String caption;

/**
* @since 2009-10-09
* 初始化產品List頁面,並按照條件對List進行分頁顯示。
* @return String SUCCESS 顯示產品List頁面
*/
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

public String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}

/**
* @since 2009-10-09
* 初始化產品List頁面,並按照條件對List進行分頁顯示。
* @return String SUCCESS 顯示產品List頁面
*/
public String listProducts() {
cfProducts = productService.findAll();
page.setEveryPage(ConstantUtil.EVERYPAGE_SIZE);
page = PageUtil.createPage(page,cfProducts.size());
goPage = 1;
cfProducts = productService.getListByPage(CfProduct.class,page);
return SUCCESS;
}

/**
* @since 2009-10-09
* 確定要發佈產品信息後,對新發布的產品的進行保存。
* @return String SUCCESS 返回產品List頁面
*/
public String toAddProduct() {
cfProduct = new CfProduct();
productclassList = productService.findSortOfCfProductsort();
return SUCCESS;
}

/**
* @since 2009-10-09
* 確定要發佈產品信息後,對新發布的產品的進行保存。
* @return String SUCCESS 返回產品List頁面
*/
public String addProduct() {
cfProduct.setProId(IdGenerator.createId());
cfProduct.setProCreatTime(new Date());
imageFileName = new Date().getTime() + getExtention(fileName);
String picURl = ServletActionContext.getServletContext().getRealPath( "/uploadimages" ) + "\\" + imageFileName;
File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "/uploadimages" ) + "/" + imageFileName);
copy(myFile, imageFile);
cfProduct.setProPicUrl(picURl);
productService.saveProduct(cfProduct);
return SUCCESS;
}

/**
* @since 2009-10-09
* 確定要修改產品信息後,初始化要修改的產品對象。
* @return String SUCCESS 跳轉到產品修改頁面
*/
@SuppressWarnings("unchecked")
public String toUpdateProduct() {
cfProduct = productService.findById(this.getProId());
productclassList = productService.findSortOfCfProductsort();
//cfCorporation = productService.findCfCorporationById(cfProduct.getProCorId());
return SUCCESS;
}

/**
* @since 2009-10-09
* 修改產品信息後,對修改了的產品的信息進行保存。
* @return String SUCCESS 跳轉到產品List頁面,並對該頁面進行刷新。
*/
public String updateProduct() {
if("0" == updatePicFlag){
productService.saveProduct(cfProduct);
return SUCCESS;
}
productService.updateProduct(cfProduct);
return SUCCESS;
}

/**
* @since 2009-10-09
* 確定啓用產品時,對產品的是否停用標誌位進行修改。
* @return String SUCCESS 刷新List頁面。
*/
public String startProduct() {
cfProduct = productService.findById(this.getProId());
cfProduct.setProIsLock(1);
productService.updateProduct(cfProduct);
return SUCCESS;
}

/**
* @since 2009-10-09
* 確定停用產品時,對產品的是否停用標誌位進行修改。
* @return String SUCCESS 刷新List頁面。
*/
public String stopProduct() {
cfProduct = productService.findById(this.getProId());
cfProduct.setProIsLock(0);
productService.updateProduct(cfProduct);
return SUCCESS;
}

/**
* @since 2009-10-09
* 點擊分頁標籤時到確定頁。
* @return String SUCCESS 指定確定頁的數據,並刷新List頁面。
*/
public String toAppointPage(){
page.setCurrentPage(this.goPage);
page = PageUtil.createPage(page);
cfProducts = productService.getListByPage(CfProduct.class,page);
return SUCCESS;
}


/**
* @since 2009-10-09
* 點擊產品List頁面,刪除鏈接。
* @return String SUCCESS 把要刪除的數據刪除標誌位設爲1,並刷新List頁面。
*/
public String deleteProduct(){
productService.deleteProduct(this.getProId());
return SUCCESS;
}

public void setCfProduct(CfProduct cfProduct) {
this.cfProduct = cfProduct;
}

public CfProduct getCfProduct() {
return cfProduct;
}

public List<CfProduct> getCfProducts() {
return cfProducts;
}

public void setCfProducts(List<CfProduct> cfProducts) {
this.cfProducts = cfProducts;
}

public ProductService getProductService() {
return productService;
}

public void setProductService(ProductService productService) {
this.productService = productService;
}

public String getProId() {
return proId;
}

public void setProId(String proId) {
this.proId = proId;
}

public static long getSerialVersionUID() {
return serialVersionUID;
}

public String getUpdatePicFlag() {
return updatePicFlag;
}

public void setUpdatePicFlag(String updatePicFlag) {
this.updatePicFlag = updatePicFlag;
}

public Page getPage() {
return page;
}

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

public List<CfProductclass> getProductclassList() {
return productclassList;
}

public void setProductclassList(List<CfProductclass> productclassList) {
this.productclassList = productclassList;
}

public int getGoPage() {
return goPage;
}

public void setGoPage(int goPage) {
this.goPage = goPage;
}

public void setMyFileContentType(String contentType) {
this.contentType = contentType;
}

public void setMyFileFileName(String fileName) {
this.fileName = fileName;
}

public void setMyFile(File myFile) {
this.myFile = myFile;
}

public String getImageFileName() {
return imageFileName;
}

public String getCaption() {
return caption;
}

public void setCaption(String caption) {
this.caption = caption;
}

public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}

}

(4)Service類及接口

接口類
/**
* <p>ProductService</p>
*
* 版權 (c) 2009
*
* <p>CFSW</p>
*
* 文件歷史
* 日期 作者 描述
* 2009-10-09 xiangcheng.yue 創建
*
*/
package product.service;

import java.util.List;
import common.beans.Page;

import product.beans.CfProduct;
import product.beans.CfProductclass;

public interface ProductService {
public void saveProduct(CfProduct cfProduct);
public List<CfProduct> findAll();
public CfProduct findById(String proId);
public List<CfProductclass> findAllRootNode();
public List<CfProductclass> findAllChildNodeByParentNode(String parentNode);
public List<CfProductclass> findSortOfCfProductsort();
//public CfCorporation findCfCorporationById(String cfCorporationId);
public void updateProduct(CfProduct cfProduct);
@SuppressWarnings("unchecked")
public List<CfProduct> getListByPage(Class pojoClass,Page page);
public void deleteProduct(String proId);
}


實現類
/**
* <p>ProductServiceImpl</p>
*
* 版權 (c) 2009
*
* <p>CFSW</p>
*
* 文件歷史
* 日期 作者 描述
* 2009-10-09 xiangcheng.yue 創建
*
*/
package product.service;

import java.util.ArrayList;
import java.util.List;

import common.beans.Page;

import corp.corporation.dao.CfCorporationDAO;

import product.beans.CfProduct;
import product.beans.CfProductclass;
import product.dao.CfProductDAO;

public class ProductServiceImpl implements ProductService {
/** 產品Dao。*/
private CfProductDAO cfProductDAO;
/** 企業Dao。*/
private CfCorporationDAO cfCorporationDAO;

/**
* @since 2009-10-09
* 保存產品對象。
* @param CfProduct cfProduct
* @return void 保存CfProduct對象。
*/
public void saveProduct(CfProduct cfProduct) {
cfProductDAO.save(cfProduct);
}

/**
* @since 2009-10-09
* 得到所有的產品List。
* @return List<CfProduct> 所有的產品List,即刪除標誌位爲0的產品。
*/
@SuppressWarnings("unchecked")
public List<CfProduct> findAll() {
return cfProductDAO.findAll();
}

/**
* @since 2009-10-09
* 通過Id得到產品對象。
* @param String proId
* @return CfProduct 。
*/
public CfProduct findById(String proId) {
return cfProductDAO.findById(proId);
}

/**
* @since 2009-10-09
* 得到所有根產品類型,即父節點字段爲空的數據。
* @return List<CfProductclass> 所有的父節點爲空的產品類型。
*/
public List<CfProductclass> findAllRootNode() {
return cfProductDAO.findAllRootNode();
}

/**
* @since 2009-10-09
* 通過產品類型表中的父節點得到該父節點的所有子節點。
* @param String parentNode
* @return List<CfProductclass> 該父節點的所有子節點的List。
*/
public List<CfProductclass> findAllChildNodeByParentNode(String parentNode) {
return cfProductDAO.findAllChildNodeByParentNode(parentNode);
}

/**
* @since 2009-10-09
* 通過產品類型表中的父節點得到該父節點的所有子節點。
* @param String parentNode
* @return List<CfProductclass> 該父節點的所有子節點的List。
*/
@SuppressWarnings("unchecked")
public List<CfProductclass> findSortOfCfProductsort() {
List<CfProductclass> allRootNoteList = this.findAllRootNode();
List<CfProductclass> classOfCfCfProductclassList = new ArrayList();
if (allRootNoteList != null && allRootNoteList.size() > 0) {
for (CfProductclass rootNode : allRootNoteList) {
classOfCfCfProductclassList.add(rootNode);
List<CfProductclass> clildNodeOfOneParentList = this
.findAllChildNodeByParentNode(rootNode.getPsId());
if (clildNodeOfOneParentList != null
&& clildNodeOfOneParentList.size() > 0) {
List<CfProductclass> formatClildNodeOfOneParentList = new ArrayList();
for (CfProductclass clildNode : clildNodeOfOneParentList) {
clildNode.setPsName("--" + clildNode.getPsName());
formatClildNodeOfOneParentList.add(clildNode);
}
classOfCfCfProductclassList
.addAll(formatClildNodeOfOneParentList);
}
}
}
return classOfCfCfProductclassList;
}

/**
* @since 2009-10-09
* 通過企業Id得到企業對象。
* @param String cfCorporationId
* @return CfCorporation 該Id對應的企業對象。
*/
// @SuppressWarnings("unchecked")
// public CfCorporation findCfCorporationById(String cfCorporationId){
// return cfCorporationDAO.findById(cfCorporationId);
// }

/**
* @since 2009-10-09
* 通過class名和Page對象得到分頁的數據。
* @param Class pojoClass
* @param Page page
* @return CfCorporation 要分頁顯示的數據的List。
*/
@SuppressWarnings("unchecked")
public List<CfProduct> getListByPage(Class pojoClass,Page page){
return cfProductDAO.getListByPage(pojoClass, page);
}

/**
* @since 2009-10-09
* 更新產品對象。
* @param CfProduct cfProduct
* @return viod 更新要更新的產品對象。
*/
public void updateProduct(CfProduct cfProduct){
cfProductDAO.attachDirty(cfProduct);
}

public void deleteProduct(String proId){
CfProduct cfProduct = cfProductDAO.findById(proId);
cfProduct.setProIsRecyle(1);
cfProductDAO.attachDirty(cfProduct);
}

public CfProductDAO getCfProductDAO() {
return cfProductDAO;
}

public void setCfProductDAO(CfProductDAO cfProductDAO) {
this.cfProductDAO = cfProductDAO;
}

public CfCorporationDAO getCfCorporationDAO() {
return cfCorporationDAO;
}

public void setCfCorporationDAO(CfCorporationDAO cfCorporationDAO) {
this.cfCorporationDAO = cfCorporationDAO;
}


}

(5)Dao類

package product.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import common.beans.Page;

import product.beans.CfProduct;
import product.beans.CfProductclass;

/**
* Data access object (DAO) for domain model class CfProduct.
* @see com.alu.test.model.CfProduct
* @author MyEclipse Persistence Tools
*/

public class CfProductDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(CfProductDAO.class);
//property constants
public static final String PRO_TITLE = "proTitle";
public static final String PRO_STITLE = "proSTitle";
public static final String PRO_MODEL = "proModel";
public static final String PRO_IS_FLAG = "proIsFlag";
public static final String PRO_MATERIAL = "proMaterial";
public static final String PRO_ORIGIN = "proOrigin";
public static final String PRO_TRADEMARK = "proTrademark";
public static final String PRO_SPEC = "proSpec";
public static final String PRO_TYPE = "proType";
public static final String PRO_PRICE = "proPrice";
public static final String PRO_PIC_URL = "proPicUrl";
public static final String PRO_CLASS_ID = "proClassId";
public static final String PRO_NAVI_CONTENT = "proNaviContent";
public static final String PRO_CONTENT = "proContent";
public static final String PRO_COR_ID = "proCorId";
public static final String PRO_CLICK = "proClick";
public static final String PRO_ORDER_ID = "proOrderId";
public static final String PRO_IS_LOCK = "proIsLock";
public static final String PRO_IS_RECYLE = "proIsRecyle";
public static final String PRO_IS_TIPS = "proIsTips";
public static final String PRO_IS_TIPS_READ = "proIsTipsRead";
public static final String PRO_IS_INDEX_FLAG = "proIsIndexFlag";
public static final String PRO_IS_RECOMMEND = "proIsRecommend";


protected void initDao() {
//do nothing
}

public void save(CfProduct transientInstance) {
log.debug("saving CfProduct instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

public void delete(CfProduct persistentInstance) {
log.debug("deleting CfProduct instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}

public CfProduct findById( java.lang.String id) {
log.debug("getting CfProduct instance with id: " + id);
try {
CfProduct instance = (CfProduct) getHibernateTemplate()
.get(CfProduct.class, id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}


public List findByExample(CfProduct instance) {
log.debug("finding CfProduct instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}

public List findByProperty(String propertyName, Object value) {
log.debug("finding CfProduct instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from CfProduct as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}

public List findByProTitle(Object proTitle) {
return findByProperty(PRO_TITLE, proTitle);
}

public List findByProSTitle(Object proSTitle) {
return findByProperty(PRO_STITLE, proSTitle);
}

public List findByProModel(Object proModel) {
return findByProperty(PRO_MODEL, proModel);
}

public List findByProIsFlag(Object proIsFlag) {
return findByProperty(PRO_IS_FLAG, proIsFlag);
}

public List findByProMaterial(Object proMaterial) {
return findByProperty(PRO_MATERIAL, proMaterial);
}

public List findByProOrigin(Object proOrigin) {
return findByProperty(PRO_ORIGIN, proOrigin);
}

public List findByProTrademark(Object proTrademark) {
return findByProperty(PRO_TRADEMARK, proTrademark);
}

public List findByProSpec(Object proSpec) {
return findByProperty(PRO_SPEC, proSpec);
}

public List findByProType(Object proType) {
return findByProperty(PRO_TYPE, proType);
}

public List findByProPrice(Object proPrice) {
return findByProperty(PRO_PRICE, proPrice);
}

public List findByProPicUrl(Object proPicUrl) {
return findByProperty(PRO_PIC_URL, proPicUrl);
}

public List findByProClassId(Object proClassId) {
return findByProperty(PRO_CLASS_ID, proClassId);
}

public List findByProNaviContent(Object proNaviContent) {
return findByProperty(PRO_NAVI_CONTENT, proNaviContent);
}

public List findByProContent(Object proContent) {
return findByProperty(PRO_CONTENT, proContent);
}

public List findByProCorId(Object proCorId) {
return findByProperty(PRO_COR_ID, proCorId);
}

public List findByProClick(Object proClick) {
return findByProperty(PRO_CLICK, proClick);
}

public List findByProOrderId(Object proOrderId) {
return findByProperty(PRO_ORDER_ID, proOrderId);
}

public List findByProIsLock(Object proIsLock) {
return findByProperty(PRO_IS_LOCK, proIsLock);
}

public List findByProIsRecyle(Object proIsRecyle) {
return findByProperty(PRO_IS_RECYLE, proIsRecyle);
}

public List findByProIsTips(Object proIsTips) {
return findByProperty(PRO_IS_TIPS, proIsTips);
}

public List findByProIsTipsRead(Object proIsTipsRead) {
return findByProperty(PRO_IS_TIPS_READ, proIsTipsRead);
}

public List findByProIsIndexFlag(Object proIsIndexFlag) {
return findByProperty(PRO_IS_INDEX_FLAG, proIsIndexFlag);
}

public List findByProIsRecommend(Object proIsRecommend) {
return findByProperty(PRO_IS_RECOMMEND, proIsRecommend);
}

public List findAll() {
log.debug("finding all CfProduct instances");
try {
String queryString = "from CfProduct t where t.proIsRecyle = 0";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

public CfProduct merge(CfProduct detachedInstance) {
log.debug("merging CfProduct instance");
try {
CfProduct result = (CfProduct) getHibernateTemplate()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

public void attachDirty(CfProduct instance) {
log.debug("attaching dirty CfProduct instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public void attachClean(CfProduct instance) {
log.debug("attaching clean CfProduct instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public static CfProductDAO getFromApplicationContext(ApplicationContext ctx) {
return (CfProductDAO) ctx.getBean("CfProductDAO");
}


@SuppressWarnings("unchecked")
public List<CfProductclass> findAllRootNode(){
log.debug("finding all CfProductclass of rootNode");
try {
String queryString = "from CfProductclass t where t.psParentId is null";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find rootNode from CfProductclass failed", re);
throw re;
}
}

@SuppressWarnings("unchecked")
public List<CfProductclass> findAllChildNodeByParentNode(String parentNode){
log.debug("finding all ChildNode By ParentNode from CfProductclass");
try {
String queryString = "from CfProductclass t where t.psParentId =:parentNode";
Query query = this.getSession().createQuery(queryString);
query.setParameter("parentNode", parentNode);
return query.list();
} catch (RuntimeException re) {
log.error("find rootNode from CfProductclass failed", re);
throw re;
}
}

@SuppressWarnings("unchecked")
public List<CfProduct> getListByPage(Class pojoClass,Page page){
log.debug("saving CfProduct instance");
String querySentence = "from " + pojoClass.getName() + " as t where t.proIsRecyle = 0";
Query query = getSession().createQuery(querySentence);
query.setFirstResult(page.getBeginIndex())
.setMaxResults(page.getEveryPage());
return query.list();
}
}

(6)page類及pageUtil類

page類

package common.beans;

/**
* @author xiangcheng.yue
*
*/
public class Page{

/** imply if the page has previous page */
private boolean hasPrePage;

/** imply if the page has next page */
private boolean hasNextPage;

/** the number of every page */
private int everyPage;

/** the total page number */
private int totalPage;

/** the number of current page */
private int currentPage;

/** the begin index of the records by the current query */
private int beginIndex;


/** The default constructor */
public Page(){

}

/** construct the page by everyPage
* @param everyPage
* */
public Page(int everyPage){
this.everyPage = everyPage;
}

/** The whole constructor */
public Page(boolean hasPrePage, boolean hasNextPage,
int everyPage, int totalPage,
int currentPage, int beginIndex){
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
this.everyPage = everyPage;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
}

/**
* @return
* Returns the beginIndex.
*/
public int getBeginIndex(){
return beginIndex;
}

/**
* @param beginIndex
* The beginIndex to set.
*/
public void setBeginIndex(int beginIndex){
this.beginIndex = beginIndex;
}

/**
* @return
* Returns the currentPage.
*/
public int getCurrentPage(){
return currentPage;
}

/**
* @param currentPage
* The currentPage to set.
*/
public void setCurrentPage(int currentPage){
this.currentPage = currentPage;
}

/**
* @return
* Returns the everyPage.
*/
public int getEveryPage(){
return everyPage;
}

/**
* @param everyPage
* The everyPage to set.
*/
public void setEveryPage(int everyPage){
this.everyPage = everyPage;
}

/**
* @return
* Returns the hasNextPage.
*/
public boolean getHasNextPage(){
return hasNextPage;
}

/**
* @param hasNextPage
* The hasNextPage to set.
*/
public void setHasNextPage(boolean hasNextPage){
this.hasNextPage = hasNextPage;
}

/**
* @return
* Returns the hasPrePage.
*/
public boolean getHasPrePage(){
return hasPrePage;
}

/**
* @param hasPrePage
* The hasPrePage to set.
*/
public void setHasPrePage(boolean hasPrePage){
this.hasPrePage = hasPrePage;
}

/**
* @return Returns the totalPage.
*
*/
public int getTotalPage(){
return totalPage;
}

/**
* @param totalPage
* The totalPage to set.
*/
public void setTotalPage(int totalPage){
this.totalPage = totalPage;
}

}

pageUtil類
package common.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import common.beans.Page;

/**
* @author xiangcheng.yue
*
*/
public class PageUtil {

@SuppressWarnings("unused")
private static final Log logger = LogFactory.getLog(PageUtil.class);

public static Page createPage(Page page){
return createPage(page.getEveryPage(), page.getCurrentPage(), page.getTotalPage());
}

/**
* Use the origin page to create a new page
* @param page
* @param totalRecords
* @return
*/
public static Page createPage(Page page, int totalRecords){
return createPage(page.getEveryPage(), page.getCurrentPage(), totalRecords);
}

/**
* the basic page utils not including exception handler
* @param everyPage
* @param currentPage
* @param totalRecords
* @return page
*/
public static Page createPage(int everyPage, int currentPage, int totalRecords){
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int beginIndex = getBeginIndex(everyPage, currentPage);
int totalPage = getTotalPage(everyPage, totalRecords);
boolean hasNextPage = hasNextPage(currentPage, totalPage);
boolean hasPrePage = hasPrePage(currentPage);

return new Page(hasPrePage, hasNextPage,
everyPage, totalPage,
currentPage, beginIndex);
}

private static int getEveryPage(int everyPage){
return everyPage == 0 ? 10 : everyPage;
}

private static int getCurrentPage(int currentPage){
return currentPage == 0 ? 1 : currentPage;
}

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

private static int getTotalPage(int everyPage, int totalRecords){
int totalPage = 0;
if(totalRecords == 0){
return totalPage++;
}
if(totalRecords % everyPage == 0)
totalPage = totalRecords / everyPage;
else
totalPage = totalRecords / everyPage + 1 ;

return totalPage;
}

private static boolean hasPrePage(int currentPage){
return currentPage == 1 ? false : true;
}

private static boolean hasNextPage(int currentPage, int totalPage){
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}

上面是分頁的全部代碼,因爲忙沒時間仔細的整理,需要的人可以仔細看下,不好意思。

希望對看博客的有幫助。
發佈了14 篇原創文章 · 獲贊 0 · 訪問量 1507
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章