Dao层:
首先定义一个公共的Dao借口:
Java代码
public interface CommonDao {
public List getUserByCutPage(final int startPage ,String tableName);
public Integer getRecordAmount(final String tableName);
}
public interface CommonDao {
public List getUserByCutPage(final int startPage ,String tableName);
public Integer getRecordAmount(final String tableName);
}
实现CommonDao
Java代码
public class CommonDaoImp extends HibernateDaoSupport implements CommonDao{
public List getUserByCutPage(final int startResult,final String tableName)
{
return this.getSessionFactory().getCurrentSession().createQuery(tableName)
.setFirstResult(startResult)
.setMaxResults(CutPage.preferSize)
.list();
}
public Integer getRecordAmount(final String tableName) {
// TODO Auto-generated method stub
return (Integer)this.getHibernateTemplate().execute(new HibernateCallback()
{
public Object doInHibernate(Session session)
{
return (Integer)session.createQuery("select count(*)"+
tableName
).uniqueResult();
}
});
}
}
public class CommonDaoImp extends HibernateDaoSupport implements CommonDao{
public List getUserByCutPage(final int startResult,final String tableName)
{
return this.getSessionFactory().getCurrentSession().createQuery(tableName)
.setFirstResult(startResult)
.setMaxResults(CutPage.preferSize)
.list();
}
public Integer getRecordAmount(final String tableName) {
// TODO Auto-generated method stub
return (Integer)this.getHibernateTemplate().execute(new HibernateCallback()
{
public Object doInHibernate(Session session)
{
return (Integer)session.createQuery("select count(*)"+
tableName
).uniqueResult();
}
});
}
}
然后由所有的dao引用或者继承CommonDao,并且在其中注如入tableName参数
Service层:
Java代码
public int getCutPage() {
// TODO Auto-generated method stub
return this.customerDao.getRecordCount();
}
public int getCutPage() {
// TODO Auto-generated method stub
return this.customerDao.getRecordCount();
}
Action层:
Java代码
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stubz
CutPage cutPage=new CutPage();
cutPage.setPageIndex(request.getParameter("pageIndex"
![](http://www.itpub.net/static/image/smiley/qqbiaoqin/baiy.gif)
);
cutPage.setRecordCount(this.customerService.getCutPage());
request.setAttribute("cutPage", cutPage);
return mapping.findForward(result);
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stubz
CutPage cutPage=new CutPage();
cutPage.setPageIndex(request.getParameter("pageIndex"
![](http://www.itpub.net/static/image/smiley/qqbiaoqin/baiy.gif)
);
cutPage.setRecordCount(this.customerService.getCutPage());
request.setAttribute("cutPage", cutPage);
return mapping.findForward(result);
}
CutPage类:
Java代码
public class CutPage {
public final static int preferSize=5;
private Integer pageIndex=new Integer(1);
private int startRecord;
private int recordCount;
private int pageCount;
public int getPageIndex() {
return pageIndex.intValue();
}
public void setPageIndex(String pageIndex) {
if(pageIndex!=null)
{
this.pageIndex = new Integer(pageIndex);
}
}
public int getRecordCount()
{
return recordCount;
}
public void setRecordCount(int recordCount)
{
this.recordCount=recordCount;
}
public int getPageCount()
{
pageCount=this.getRecordCount()/this.preferSize;
int temCount=this.getRecordCount()%this.preferSize;
if(temCount!=0)
{
pageCount++;
}
return pageCount;
}
public int getStartRecord() {
return (this.getPageIndex()-1)*this.preferSize;
}
public void setStartRecord(int startRecord) {
this.startRecord = startRecord;
}
}
public class CutPage {
public final static int preferSize=5;
private Integer pageIndex=new Integer(1);
private int startRecord;
private int recordCount;
private int pageCount;
public int getPageIndex() {
return pageIndex.intValue();
}
public void setPageIndex(String pageIndex) {
if(pageIndex!=null)
{
this.pageIndex = new Integer(pageIndex);
}
}
public int getRecordCount()
{
return recordCount;
}
public void setRecordCount(int recordCount)
{
this.recordCount=recordCount;
}
public int getPageCount()
{
pageCount=this.getRecordCount()/this.preferSize;
int temCount=this.getRecordCount()%this.preferSize;
if(temCount!=0)
{
pageCount++;
}
return pageCount;
}
public int getStartRecord() {
return (this.getPageIndex()-1)*this.preferSize;
}
public void setStartRecord(int startRecord) {
this.startRecord = startRecord;
}
}
最后,只要做一个专门的jsp页面面,只要需要时用
Java代码
hrefName
" value="selectItem
"/>
cutpage.jsp:
Java代码
hrefName
+".do?pageIndex="+(k+i)+">"+(k+i)+"[/url]"
![](http://www.itpub.net/static/image/smiley/qqbiaoqin/baiy.gif)
;
}
%>
|