新建表
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
DROP
DATABASE
IF
EXISTS
`wjcms`;
CREATE
DATABASE
`wjcms`
/*
!40100DEFAULTCHARACTERSETgb2312
*/
;
USE
`wjcms`;
#
#
Table
structure
for
table
t_article
#
CREATE
TABLE
`t_article`(
`a_id`
int
(
11
)
NOT
NULL
auto_increment,
`a_sort`
int
(
11
)
NOT
NULL
default
'
0
'
,
`a_title`
varchar
(
50
)
default
NULL
,
`a_body`
text
,
`a_author`
varchar
(
11
)
default
''
,
`a_hit`
int
(
11
)
NOT
NULL
default
'
0
'
,
`c_id`
int
(
11
)
default
'
0
'
,
`a_date`
varchar
(
20
)
default
NULL
,
PRIMARY
KEY
(`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>-->
package
page.dal;
public
class
page{
private
int
totalRows;
//
总行数
private
int
pageSize
=
10
;
//
每页显示的行数
private
int
currentPage;
//
当前页号
private
int
totalPages;
//
总页数
private
int
startRow;
//
当前页在数据库中的起始行
public
page(
int
_totalRows){
totalRows
=
_totalRows;
totalPages
=
totalRows
/
pageSize;
int
mod
=
totalRows
%
pageSize;
if
(mod
>
0
){
totalPages
++
;
}
currentPage
=
1
;
startRow
=
0
;
}
public
int
getStartRow(){
return
startRow;
}
public
int
getTotalPages(){
return
totalPages;
}
public
int
getCurrentPage(){
return
currentPage;
}
public
int
getPageSize(){
return
pageSize;
}
public
void
setTotalRows(
int
totalRows){
this
.totalRows
=
totalRows;
}
public
void
setStartRow(
int
startRow){
this
.startRow
=
startRow;
}
public
void
setTotalPages(
int
totalPages){
this
.totalPages
=
totalPages;
}
public
void
setCurrentPage(
int
currentPage){
this
.currentPage
=
currentPage;
}
public
void
setPageSize(
int
pageSize){
this
.pageSize
=
pageSize;
}
public
int
getTotalRows(){
return
totalRows;
}
public
void
first(){
currentPage
=
1
;
startRow
=
0
;
}
public
void
previous(){
if
(currentPage
==
1
){
return
;
}
currentPage
--
;
startRow
=
(currentPage
-
1
)
*
pageSize;
}
public
void
next(){
if
(currentPage
<
totalPages){
currentPage
++
;
}
startRow
=
(currentPage
-
1
)
*
pageSize;
}
public
void
last(){
currentPage
=
totalPages;
startRow
=
(currentPage
-
1
)
*
pageSize;
}
public
void
refresh(
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>-->
package
page.dal;
import
javax.servlet.http.
*
;
public
class
PagerHelp{
public
static
pagegetPager(HttpServletRequesthttpServletRequest,
int
totalRows){
//
定义pager对象,用于传到页面
pagepager
=
new
page(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();
}
else
if
(pagerMethod.equals(
"
previous
"
)){
pager.previous();
}
else
if
(pagerMethod.equals(
"
next
"
)){
pager.next();
}
else
if
(pagerMethod.equals(
"
last
"
)){
pager.last();
}
}
return
pager;
}
}
新建 util.java
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
package
page.dal;
import
net.sf.hibernate.Query;
import
net.sf.hibernate.cfg.Configuration;
import
java.util.List;
import
net.sf.hibernate.HibernateException;
import
net.sf.hibernate.SessionFactory;
import
net.sf.hibernate.Session;
import
java.util.
*
;
public
class
util{
public
util(){
}
private
Sessionss
=
null
;
public
SessiongetSession()
{
//
Configurationconfig=null;
SessionFactorysessionFactory;
try
{
Configurationcfg
=
new
Configuration();
sessionFactory
=
cfg.addClass(articleVO.
class
).
buildSessionFactory();
//
SessionFactorysessionFactory=config.buildSessionFactory();
ss
=
sessionFactory.openSession();
return
ss;
}
catch
(HibernateExceptionex){
System.out.print(
"
getsession出错了。。
"
+
ex.getMessage());
return
null
;
}
}
public
int
getCount()
{
Stringsql
=
"
selectcount(*)fromarticleVO
"
;
this
.getSession();
try
{
//
ss.createQuery("selectcount(a)ascontfromarticleVOa");
int
rows
=
((Integer)ss.iterate(sql).next()).intValue();
ss.flush();
return
rows;
}
catch
(HibernateExceptionex){
System.out.print(
"
ex::
"
+
ex.getMessage());
return
0
;
}
}
public
CollectiongetList(
int
pagesize,
int
currow)
throws
HibernateException{
CollectionvehicleList
=
null
;
this
.getSession();
Queryq
=
ss.createQuery(
"
fromarticleVO
"
);
q.setFirstResult(currow);
q.setMaxResults(pagesize);
vehicleList
=
q.list();
ss.flush();
return
vehicleList;
}
}
新建 struts PageAction.java
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
package
page.dal;
import
org.apache.struts.action.ActionMapping;
import
org.apache.struts.action.ActionForm;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.Action;
import
page.dal.
*
;
import
java.util.
*
;
import
net.sf.hibernate.
*
;
public
class
pageAction
extends
Action{
public
ActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,
HttpServletResponseresponse){
CollectionclInfos
=
null
;
//
用于输出到页面的记录集合
int
totalRows;
//
记录总行数
utildal
=
new
util();
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);
return
mapping.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
>
<
table
align
="center"
border
="2"
>
<
tr
>
<
th
>
a_title
</
th
>
<
th
>
a_body
</
th
>
<
th
>
a_a_date
</
th
>
<
th
>
a_author
</
th
>
</
tr
>
<
logic:iterate
id
="listd"
name
="list"
>
<
tr
>
<
td
>
<
bean:write
name
="listd"
property
="a_title"
/>
</
td
>
<
td
>
<
bean:write
name
="listd"
property
="a_author"
/>
</
td
>
<
td
>
<
bean:write
name
="listd"
property
="a_date"
/>
</
td
>
<
td
>
<
bean:write
name
="listd"
property
="a_date"
/>
</
td
>
</
tr
>
</
logic:iterate
>
</
table
>
第
<
bean:write
name
="page"
property
="currentPage"
/>
页
共
<
bean:write
name
="page"
property
="totalPages"
/>
页
<
html:link
action
="/pageAction.do?pageMethod=first"
paramName
="page"
paramProperty
="currentPage"
paramId
="currentPage"
>
首页
</
html:link
>
<
html:link
action
="/pageAction.do?pageMethod=previous"
paramName
="page"
paramProperty
="currentPage"
paramId
="currentPage"
>
上一页
</
html:link
>
<
html:link
action
="/pageAction.do?pageMethod=next"
paramName
="page"
paramProperty
="currentPage"
paramId
="currentPage"
>
下一页
</
html:link
>
<
html:link
action
="/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
>
<
class
name
="page.dal.articleVO"
table
="t_article"
>
<
id
name
="a_id"
column
="a_id"
unsaved-value
="0"
>
<
generator
class
="native"
/>
</
id
>
<
property
name
="c_id"
column
="c_id"
/>
<
property
name
="a_title"
column
="a_title"
/>
<
property
name
="a_sort"
column
="a_sort"
/>
<
property
name
="a_date"
column
="a_date"
/>
<
property
name
="a_body"
column
="a_body"
/>
<
property
name
="a_hit"
column
="a_hit"
/>
<
property
name
="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