**************************************Service层方法****************************
public PageModel getSearch(String key, int articletypeid,int pageNo, int pageSize) //分页--搜索
{
PageModel pagemodel = artircleDao.getSearch
(key , articletypeid , pageNo , pageSize);
List keys = SubString.getInstance().splitKey(key);
if(pagemodel.getList()!=null)
{
ListArticle.getInstance().color(pagemodel.getList() , keys);
ListArticle.getInstance().listDeal(pagemodel.getList() , 1 , 50);
}
return pagemodel;
}
*******************************************Dao层方法**************************************
public PageModel getSearch
(String key ,final int articletypeid,final int pageNo, final int pageSize) //分页--搜索
{
final List keys=SubString.getInstance().splitKey
(key); //方法将在后面给出
PageModel pageModel = new PageModel();
List itemList = new ArrayList();
if(keys.size()>0)
{
itemList=(List)this.getHibernateTemplate().execute(
new HibernateCallback()
{
public Object doInHibernate(org.hibernate.Session session)
{
String hql="from Article a where a.title like ? "; //对标题的搜索
for(int i=0;i<keys.size()-1;i++)//如果只有一个关键字 则不加
{
hql=hql+"or a.title like ?";
}
hql= hql+"or a.content like ?"; //添加内容的搜索
for(int i=0;i<keys.size()-1;i++)//如果只有一个关键字 则不加
{
hql=hql+"or a.content like ?";
}
Query q = session.createQuery(hql+" order by a.id");
for(int i= 0 ;i<keys.size();i++) //添加对标题的参数 从i开始
{
q.setParameter(i, "%"+keys.get(i) + "%");
}
for(int i= 0 ;i<keys.size();i++) //添加对内容的参数 从keys.size()+i开始
{
q.setParameter(keys.size()+i, "%"+keys.get(i) + "%");
}
q.setFirstResult((pageNo - 1) * pageSize)
.setMaxResults(pageSize);
List result = q.list();
return result;
}
}
);
List itemList_2=(List)this.getHibernateTemplate().execute( //用于总记录数
new HibernateCallback()
{
public Object doInHibernate(org.hibernate.Session session)
{
String hql="from Article a where a.title like ? "; //对标题的搜索
for(int i=0;i<keys.size()-1;i++)//如果只有一个关键字 则不加
{
hql=hql+"or a.title like ?";
}
hql= hql+"or a.content like ?"; //添加内容的搜索
for(int i=0;i<keys.size()-1;i++)//如果只有一个关键字 则不加
{
hql=hql+"or a.content like ?";
}
Query q = session.createQuery(hql+" order by a.id");
for(int i= 0 ;i<keys.size();i++) //添加对标题的参数 从i开始
{
q.setParameter(i, "%"+keys.get(i) + "%");
}
for(int i= 0 ;i<keys.size();i++) //添加对内容的参数 从keys.size()+i开始
{
q.setParameter(keys.size()+i, "%"+keys.get(i) + "%");
}
List result = q.list();
return result;
}
}
);
pageModel.setList(itemList);
pageModel.setPageNo(pageNo);
pageModel.setPageSize(pageSize);
pageModel.setTotalRecords(itemList_2.size());//-----------
}
return pageModel;
}
*******************************
splitKey()
*************************************************
public List splitKey
(String key)//格式化关键字 去除多余的空格
{
key = key.trim();
String[] keys = key.split(" ");
List keyList= new ArrayList();
for(String k : keys)
{
if(k.equals("")||k.equals(" ")||k==null)
continue;
else
{
//System.out.println("kkkkkkkkkkkkkkk==:"+k);
keyList.add(k);
}
}
return keyList;
}
*********************************PageModel类
*****************************************
package com.bcm.util;
import java.util.List;
import com.bcm.model.ArticleType;
public class PageModel
{
// 总记录数
private int totalRecords;
// 结果集
private List list;
// 当前页
private int pageNo;
// 每页显示多少条
private int pageSize;
private int getTotalPages; //总页数
private ArticleType articletype;
public int getTotalRecords()
{
return totalRecords;
}
public void setTotalRecords(int totalRecords)
{
this.totalRecords = totalRecords;
}
public List getList()
{
return list;
}
public void setList(List list)
{
this.list = list;
}
public int getPageNo()
{
return pageNo;
}
public void setPageNo(int pageNo)
{
if(pageNo==0)
this.pageNo = 1;
else
this.pageNo = pageNo;
}
public int getPageSize()
{
return pageSize;
}
public void setPageSize(int pageSize)
{
this.pageSize = pageSize;
}
/**
* 取得总页数
* @return
*/
public int getTotalPages()
{
return (totalRecords + pageSize - 1) / pageSize;
}
/**
* 取得第一页
* @return
*/
public int getTopPageNo()
{
return 1;
}
/**
* 取得上一页
* @return
*/
public int getPreviousPageNo()
{
if (pageNo <= 1)
{
return 1;
}
return pageNo - 1;
}
/**
* 取得下一页
* @return
*/
public int getNextPageNo()
{
if (pageNo >= getTotalPages())
{
return getTotalPages() == 0 ? 1 : getTotalPages();
}
return pageNo + 1;
}
/**
* 取得最后一页
* @return
*/
public int getBottomPageNo()
{
return getTotalPages() == 0 ? 1 : getTotalPages();
}
public ArticleType getArticletype()
{
return articletype;
}
public void setArticletype(ArticleType articletype)
{
this.articletype = articletype;
}
}
********************************前台调用分页************************************************
<table width="926" height="21" border="0" class="title1">
<tr>
<form action="article_search.action" ><!-- 只用于手动跳转到XX页 -->
<td height="25" align="center" valign="bottom" class="titlep">
<s:url id="url_top" value="article_search.action">
<s:param name="pageNo" value="1"></s:param>
<s:param name="key" value="#session.key"></s:param>
</s:url>
<s:url id="url_pre" value="article_search.action">
<s:param name="pageNo" value="pagemodel.getPreviousPageNo()"></s:param>
<s:param name="key" value="#session.key"></s:param>
</s:url>
<s:url id="url_next" value="article_search.action">
<s:param name="pageNo" value="pagemodel.getNextPageNo()"></s:param>
<s:param name="key" value="#session.key"></s:param>
</s:url>
<s:url id="url_buttom" value="article_search.action">
<s:param name="pageNo" value="pagemodel.getBottomPageNo()"></s:param>
<s:param name="key" value="#session.key"></s:param>
</s:url>
共<font color="#CC3333"><s:property value="pagemodel.totalRecords"/></font>条
每页<font color="#CC3333"><s:property value="pagemodel.pageSize"/></font>条
第<font color="#CC3333"><s:property value="pagemodel.pageNo"/>/<s:property value="pagemodel.getTotalPages()"/></font>页
<s:if test="pagemodel.pageNo==1&&pagemodel.getTotalPages()==1">
<font color="#999999"> 首页</font>
<font color="#999999"> 上一页</font>
<font color="#999999">下一页</font>
<font color="#999999">尾页</font>
</s:if>
<s:elseif test="pagemodel.pageNo==1&&pagemodel.getTotalPages()!=1">
<font color="#999999"> 首页</font>
<font color="#999999"> 上一页</font>
<s:a href="%{url_next}">下一页</s:a>
<s:a href="%{url_buttom}">尾页</s:a>
</s:elseif>
<s:elseif test="pagemodel.pageNo==pagemodel.getBottomPageNo()">
<s:a href="%{url_top}">首页</s:a>
<s:a href="%{url_pre}">上一页</s:a>
<font color="#999999">下一页</font>
<font color="#999999">尾页</font>
</s:elseif>
<s:else>
<s:a href="%{url_top}">首页</s:a>
<s:a href="%{url_pre}">上一页</s:a>
<s:a href="%{url_next}">下一页</s:a>
<s:a href="%{url_buttom}">尾页</s:a>
</s:else>
转到<input type="hidden" name="key" value="<s:property value="#session.key"/>"/>
<input type="text" name="pageNo" size="2"/>页
<input type="submit" value="go"/>
</td>
</form>
</tr>
</table>
from:http://hi.baidu.com/woaini5730/blog/item/46b3da34a408493c5ab5f551.html