实现可复用的分页组件(struts+spring+hibernate)

[复制链接]
查看11 | 回复6 | 2007-9-9 01:05:40 | 显示全部楼层 |阅读模式
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&quot

);
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&quot

);

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]&quot

;
}
%>


回复

使用道具 举报

千问 | 2007-9-9 01:05:40 | 显示全部楼层
这个代码 怎么这么多html脚本?楼主打个包吧
回复

使用道具 举报

千问 | 2007-9-9 01:05:40 | 显示全部楼层
你不觉得麻烦吗?那么多的java代码,为什么不统一写在一处,分类呢,
回复

使用道具 举报

千问 | 2007-9-9 01:05:40 | 显示全部楼层
看的累~~
回复

使用道具 举报

千问 | 2007-9-9 01:05:40 | 显示全部楼层
分页问题有这么难吗?怎么写这么多代码?一个javaBean就搞好了。然后传给jsp就可以了吧。
回复

使用道具 举报

千问 | 2007-9-9 01:05:40 | 显示全部楼层
就是
回复

使用道具 举报

千问 | 2007-9-9 01:05:40 | 显示全部楼层
楼主辛苦了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行