struts+hibernate分页

本文介绍了一个基于Struts和Hibernate框架的简单分页查询系统实现。系统包括数据库表结构设计、实体类定义、分页逻辑处理及前端展示等内容。通过创建数据库表、定义实体类、实现分页逻辑和前端页面展示,完成了一个完整的分页查询功能。
新建表
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
DROPDATABASEIFEXISTS`wjcms`;
CREATEDATABASE`wjcms`/*!40100DEFAULTCHARACTERSETgb2312*/;
USE`wjcms`;

#
#
Tablestructurefortablet_article
#

CREATETABLE`t_article`(
`a_id`
int(11)NOTNULLauto_increment,
`a_sort`
int(11)NOTNULLdefault'0',
`a_title`
varchar(50)defaultNULL,
`a_body`
text,
`a_author`
varchar(11)default'',
`a_hit`
int(11)NOTNULLdefault'0',
`c_id`
int(11)default'0',
`a_date`
varchar(20)defaultNULL,
PRIMARYKEY(`a_id`)
)

实体

public class articleVO {
private int a_id;
private int a_sort;
private int a_hit;
private int c_id;
private String a_title;
private String a_body;
private String a_author;
private String a_date;
// getter setter


新建page.java

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->packagepage.dal;

publicclasspage{
privateinttotalRows;//总行数
privateintpageSize=10;//每页显示的行数
privateintcurrentPage;//当前页号
privateinttotalPages;//总页数
privateintstartRow;//当前页在数据库中的起始行

publicpage(int_totalRows){
totalRows
=_totalRows;
totalPages
=totalRows/pageSize;
intmod=totalRows%pageSize;
if(mod>0){
totalPages
++;
}
currentPage
=1;
startRow
=0;
}

publicintgetStartRow(){
returnstartRow;
}

publicintgetTotalPages(){
returntotalPages;
}

publicintgetCurrentPage(){
returncurrentPage;
}

publicintgetPageSize(){
returnpageSize;
}

publicvoidsetTotalRows(inttotalRows){
this.totalRows=totalRows;
}

publicvoidsetStartRow(intstartRow){
this.startRow=startRow;
}

publicvoidsetTotalPages(inttotalPages){
this.totalPages=totalPages;
}

publicvoidsetCurrentPage(intcurrentPage){
this.currentPage=currentPage;
}

publicvoidsetPageSize(intpageSize){
this.pageSize=pageSize;
}

publicintgetTotalRows(){
returntotalRows;
}

publicvoidfirst(){
currentPage
=1;
startRow
=0;
}

publicvoidprevious(){
if(currentPage==1){
return;
}
currentPage
--;
startRow
=(currentPage-1)*pageSize;
}

publicvoidnext(){
if(currentPage<totalPages){
currentPage
++;
}
startRow
=(currentPage-1)*pageSize;
}

publicvoidlast(){
currentPage
=totalPages;
startRow
=(currentPage-1)*pageSize;
}

publicvoidrefresh(int_currentPage){
currentPage
=_currentPage;
if(currentPage>totalPages){
last();
}
}

}



新建 pageHelp.java

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->packagepage.dal;
importjavax.servlet.http.*;

publicclassPagerHelp{
publicstaticpagegetPager(HttpServletRequesthttpServletRequest,inttotalRows){

//定义pager对象,用于传到页面
pagepager=newpage(totalRows);

//从Request对象中获取当前页号
StringcurrentPage=httpServletRequest.getParameter("currentPage");

//如果当前页号为空,表示为首次查询该页
//如果不为空,则刷新page对象,输入当前页号等信息
if(currentPage!=null){
pager.refresh(Integer.parseInt(currentPage));
}

//获取当前执行的方法,首页,前一页,后一页,尾页。
StringpagerMethod=httpServletRequest.getParameter("pageMethod");

if(pagerMethod!=null){
if(pagerMethod.equals("first")){
pager.first();
}
elseif(pagerMethod.equals("previous")){
pager.previous();
}
elseif(pagerMethod.equals("next")){
pager.next();
}
elseif(pagerMethod.equals("last")){
pager.last();
}
}
returnpager;
}

}



新建 util.java
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->packagepage.dal;
importnet.sf.hibernate.Query;
importnet.sf.hibernate.cfg.Configuration;
importjava.util.List;
importnet.sf.hibernate.HibernateException;
importnet.sf.hibernate.SessionFactory;
importnet.sf.hibernate.Session;
importjava.util.*;
publicclassutil{
publicutil(){
}
privateSessionss=null;
publicSessiongetSession()
{
//Configurationconfig=null;
SessionFactorysessionFactory;
try{
Configurationcfg
=newConfiguration();
sessionFactory
=cfg.addClass(articleVO.class).
buildSessionFactory();
//SessionFactorysessionFactory=config.buildSessionFactory();
ss=sessionFactory.openSession();
returnss;
}
catch(HibernateExceptionex){
System.out.print(
"getsession出错了。。"+ex.getMessage());
returnnull;
}
}

publicintgetCount()
{
Stringsql
="selectcount(*)fromarticleVO";
this.getSession();

try{
//ss.createQuery("selectcount(a)ascontfromarticleVOa");
introws=((Integer)ss.iterate(sql).next()).intValue();
ss.flush();
returnrows;

}
catch(HibernateExceptionex){
System.out.print(
"ex::"+ex.getMessage());
return0;
}


}

publicCollectiongetList(intpagesize,intcurrow)throwsHibernateException{
CollectionvehicleList
=null;
this.getSession();
Queryq
=ss.createQuery("fromarticleVO");
q.setFirstResult(currow);
q.setMaxResults(pagesize);
vehicleList
=q.list();
ss.flush();
returnvehicleList;
}

}


新建 struts PageAction.java


<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->packagepage.dal;

importorg.apache.struts.action.ActionMapping;
importorg.apache.struts.action.ActionForm;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.Action;
importpage.dal.*;
importjava.util.*;
importnet.sf.hibernate.*;

publicclasspageActionextendsAction{
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,
HttpServletResponseresponse){
CollectionclInfos
=null;//用于输出到页面的记录集合
inttotalRows;//记录总行数
utildal=newutil();
totalRows
=dal.getCount();
System.out.print(
"总行数=="+totalRows);
pagep
=PagerHelp.getPager(request,totalRows);
try{
clInfos
=dal.getList(p.getPageSize(),p.getStartRow());

}
catch(HibernateExceptionex){
System.out.print(
"action里的错误="+ex.getMessage());
}
request.setAttribute(
"page",p);
request.setAttribute(
"list",clInfos);
returnmapping.findForward("page");
//pageFormpageForm=(pageForm)form;
//thrownewjava.lang.UnsupportedOperationException(
//"Method$execute()notyetimplemented.");
}
}


前台页面

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><%@tagliburi="/WEB-INF/struts-tiles.tld"prefix="tiles"%>
<%@tagliburi="/WEB-INF/struts-nested.tld"prefix="nested"%>
<%@tagliburi="/WEB-INF/struts-logic.tld"prefix="logic"%>
<%@tagliburi="/WEB-INF/struts-bean.tld"prefix="bean"%>
<%@tagliburi="/WEB-INF/struts-html.tld"prefix="html"%>
<%@pagecontentType="text/html;charset=GBK"%>
<html:html>
<head>
<title>
page
</title>
</head>
<body>
<tablealign="center"border="2">
<tr>
<th>a_title</th>
<th>a_body</th>
<th>a_a_date</th>
<th>a_author</th>
</tr>

<logic:iterateid="listd"name="list">
<tr>
<td>
<bean:writename="listd"property="a_title"/>
</td>
<td>
<bean:writename="listd"property="a_author"/>
</td>
<td>
<bean:writename="listd"property="a_date"/>
</td>
<td>
<bean:writename="listd"property="a_date"/>
</td>
</tr>
</logic:iterate>

</table>

<bean:writename="page"property="currentPage"/>
<bean:writename="page"property="totalPages"/>
<html:linkaction="/pageAction.do?pageMethod=first"
paramName
="page"paramProperty="currentPage"paramId="currentPage">首页</html:link>
<html:linkaction="/pageAction.do?pageMethod=previous"
paramName
="page"paramProperty="currentPage"paramId="currentPage">上一页</html:link>
<html:linkaction="/pageAction.do?pageMethod=next"
paramName
="page"paramProperty="currentPage"paramId="currentPage">下一页</html:link>

<html:linkaction="/pageAction.do?pageMethod=last"
paramName
="page"paramProperty="currentPage"paramId="currentPage">尾页</html:link>
</body>
</html:html>



启动浏览 pageAction.do 运行OK。



****************************************************************************************


配置文件


<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEhibernate-mappingPUBLIC
"-//Hibernate/HibernateMappingDTD2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
<hibernate-mapping>

<classname="page.dal.articleVO"table="t_article">


<idname="a_id"column="a_id"unsaved-value="0">
<generatorclass="native"/>
</id>
<propertyname="c_id"column="c_id"/>
<propertyname="a_title"column="a_title"/>
<propertyname="a_sort"column="a_sort"/>
<propertyname="a_date"column="a_date"/>
<propertyname="a_body"column="a_body"/>
<propertyname="a_hit"column="a_hit"/>
<propertyname="a_author"column="a_author"/>


</class>

</hibernate-mapping>
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
hibernate.dialectnet.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_classorg.gjt.mm.mysql.Driver
hibernate.connection.urljdbc:mysql://localhost:
3306/wjcms
hibernate.connection.usernameroot
hibernate.connection.password
hibernate.connection.pool_size
1
hibernate.proxool.pool_aliaspool1
hibernate.show_sqltrue
hibernate.max_fetch_depth
1
hibernate.cache.use_query_cachetrue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值