[color=blue]//数据库表[/color]
create table Student
(
stuid varchar(32) primary key,
stuname varchar(32),
stuage int,
stusex char(2),
stuaddr varchar(50)
)
[color=red]//////////////////////////com.svse.entity///////////////////////////[/color]
[color=blue] //Student实体[/color]
package com.svse.entity;
public class Student {
private String stuid;
private String stuname;
private int stuage;
private String stusex;
private String stuaddr;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String stuaddr, int stuage, String stuid, String stuname,
String stusex) {
super();
this.stuaddr = stuaddr;
this.stuage = stuage;
this.stuid = stuid;
this.stuname = stuname;
this.stusex = stusex;
}
public String getStuid() {
return stuid;
}
public void setStuid(String stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public int getStuage() {
return stuage;
}
public void setStuage(int stuage) {
this.stuage = stuage;
}
public String getStusex() {
return stusex;
}
public void setStusex(String stusex) {
this.stusex = stusex;
}
public String getStuaddr() {
return stuaddr;
}
public void setStuaddr(String stuaddr) {
this.stuaddr = stuaddr;
}
}
[color=blue]//Student.hbm.xml文件[/color]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.svse.entity.Student" table="Student">
<id name="stuid" column="stuid" type="string" length="32">
<generator class="assigned"></generator>
</id>
<property name="stuname" column="stuname" type="string" length="32"></property>
<property name="stuage" column="stuage" type="integer"></property>
<property name="stusex" column="stusex" type="string" length="32"></property>
<property name="stuaddr" column="stuaddr" type="string" length="32"></property>
</class>
</hibernate-mapping>
[color=red]//////////////////////////com.svse.util///////////////////////////[/color]
[color=blue]//分页辅助类[/color]
package com.svse.util;
public class PageHelp {
int totalcount;//总行数
int linesize;//每页显示的条数
int totalpage;// 总页数
int currentpage;//当前页
public PageHelp() {
// TODO Auto-generated constructor stub
}
public PageHelp(int currentpage, int linesize, int totalcount, int totalpage) {
super();
this.currentpage = currentpage;
this.linesize = linesize;
this.totalcount = totalcount;
this.totalpage = totalpage;
}
public int getTotalcount() {
return totalcount;
}
public void setTotalcount(int totalcount) {
this.totalcount = totalcount;
}
public int getLinesize() {
return linesize;
}
public void setLinesize(int linesize) {
this.linesize = linesize;
}
public int getTotalpage() {
return totalpage;
}
//设置总页数
public void setTotalpage() {
if(this.getTotalcount()%this.getLinesize()==0)
{
this.totalpage = this.getTotalcount()/this.getLinesize();
}
else
{
this.totalpage = this.getTotalcount()/this.getLinesize()+1;
}
}
public int getCurrentpage() {
return currentpage;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
}
[color=blue]//字符过滤器[/color]
package com.svse.util;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class Filter implements javax.servlet.Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("text/html;charset=utf-8");
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
[color=blue]//SessionFactory类[/color]
package com.svse.util;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class DefaultSessionFactory {
public static Session getSession()
{
return new Configuration().
configure().buildSessionFactory().openSession();
}
}
[color=red]/////////////////////////////com.svse.dao/////////////////////////////[/color]
[color=blue]//StudentDao类[/color]
package com.svse.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.svse.entity.Student;
import com.svse.util.DefaultSessionFactory;
public class StudentDao {
private Session session=null;
public StudentDao() {
this.session=DefaultSessionFactory.getSession();
}
//查询所有记录的条数
public long getTotalCount()
{
String hql="select count(stuname) from Student";
Query query=this.session.createQuery(hql);
return (Long)query.uniqueResult();
}
//获取当前分页后的集合
public List<Student> getCurrentList(int currentPage,int lineSize)
{
String hql="from Student";
Query query=this.session.createQuery(hql);
query.setFirstResult((currentPage-1)*lineSize);
query.setMaxResults(lineSize);
return query.list();
}
public List findAll()
{
String hql="from Student";
Query query=this.session.createQuery(hql);
return query.list();
}
}
[color=red]////////////////////////////com.svse.action///////////////////////////[/color]
[color=blue]//StudentAction类[/color]
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.svse.struts.action;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.svse.dao.StudentDao;
import com.svse.entity.Student;
import com.svse.util.PageHelp;
/**
* MyEclipse Struts
* Creation date: 06-21-2011
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class StudentAction extends DispatchAction {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
private StudentDao studao=new StudentDao();
public ActionForward findAllStu(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//int totalCount=(int)studao.getTotalCount();
int totalCount=studao.findAll().size();
System.err.println("xxxxxxxxx : "+totalCount);
PageHelp ph=new PageHelp();
ph.setTotalcount(totalCount);
ph.setLinesize(3);
ph.setTotalpage();
int currentPage=0;
if(request.getParameter("currentPage")==null)
{
currentPage=1;
}
else
{
currentPage=Integer.parseInt(request.getParameter("currentPage"));
//防止下标越界的情况
if(currentPage<1)
{
currentPage=1;
}
if(currentPage>ph.getTotalpage())
{
currentPage=ph.getTotalpage();
}
}
ph.setCurrentpage(currentPage);
List<Student> stulist=studao.getCurrentList(ph.getCurrentpage(), ph.getLinesize());
request.setAttribute("stulist", stulist);
request.setAttribute("pagehelp", ph);
System.err.println(stulist.size());
for (Student student : stulist) {
System.err.println(student.getStuname()+student.getStuid());
}
return mapping.findForward("tostulist");
}
}
[color=blue]//显示信息的JSP页面[/color]
<%@ page language="java" pageEncoding="utf-8" isELIgnored="false"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>Hibernate分页</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<table border="0" align="center" width="40%">
<tr align="center" bgcolor="pink">
<td colspan="5">学生详细信息</td>
</tr>
<tr align="center" bgcolor="pink">
<td colspan="5"><hr></td>
</tr>
<tr bgcolor="pink">
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>地址</td>
</tr>
<logic:iterate id="s" name="stulist">
<tr bgcolor="pink">
<td>${s.stuid }</td>
<td>${s.stuname }</td>
<td>${s.stuage}</td>
<td>${s.stusex }</td>
<td>${s.stuaddr }</td>
</tr>
</logic:iterate>
<tr align="center" bgcolor="pink">
<td colspan="5"><hr></td>
</tr>
<tr bgcolor="pink">
<td colspan="5" width="700" align="center">
<a href="student.do?method=findAllStu¤tPage=1"><img src="image/head.gif" style="border: none;"/></a>
<a href="student.do?method=findAllStu¤tPage=${pagehelp.currentpage-1 }"><img src="image/top.gif" style="border: none;"/></a>
<a href="student.do?method=findAllStu¤tPage=${pagehelp.currentpage+1 }"><img src="image/next.gif" style="border: none;"/></a>
<a href="student.do?method=findAllStu¤tPage=${pagehelp.totalpage }"><img src="image/end.gif" style="border: none;"/></a>
</td>
</tr>
<tr align="center" bgcolor="pink">
<td colspan="5">
<font color="red">当前是:【${pagehelp.currentpage}】页, 一共【${pagehelp.totalpage}】页</font>
</td>
</tr>
</table>
</center>
</body>
</html:html>
[color=blue]//Struts-config-xml配置文件[/color]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings >
<action path="/student"
type="com.svse.struts.action.StudentAction"
parameter="method"
>
<forward name="tostulist" path="/showstu.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="com.svse.struts.ApplicationResources" />
</struts-config>
[color=blue]//Hibernate-cfg-xml配置文件[/color]
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">scott</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="myeclipse.connection.profile">oracle</property>
<property name="connection.password">tiger</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/svse/entity/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
create table Student
(
stuid varchar(32) primary key,
stuname varchar(32),
stuage int,
stusex char(2),
stuaddr varchar(50)
)
[color=red]//////////////////////////com.svse.entity///////////////////////////[/color]
[color=blue] //Student实体[/color]
package com.svse.entity;
public class Student {
private String stuid;
private String stuname;
private int stuage;
private String stusex;
private String stuaddr;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String stuaddr, int stuage, String stuid, String stuname,
String stusex) {
super();
this.stuaddr = stuaddr;
this.stuage = stuage;
this.stuid = stuid;
this.stuname = stuname;
this.stusex = stusex;
}
public String getStuid() {
return stuid;
}
public void setStuid(String stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public int getStuage() {
return stuage;
}
public void setStuage(int stuage) {
this.stuage = stuage;
}
public String getStusex() {
return stusex;
}
public void setStusex(String stusex) {
this.stusex = stusex;
}
public String getStuaddr() {
return stuaddr;
}
public void setStuaddr(String stuaddr) {
this.stuaddr = stuaddr;
}
}
[color=blue]//Student.hbm.xml文件[/color]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.svse.entity.Student" table="Student">
<id name="stuid" column="stuid" type="string" length="32">
<generator class="assigned"></generator>
</id>
<property name="stuname" column="stuname" type="string" length="32"></property>
<property name="stuage" column="stuage" type="integer"></property>
<property name="stusex" column="stusex" type="string" length="32"></property>
<property name="stuaddr" column="stuaddr" type="string" length="32"></property>
</class>
</hibernate-mapping>
[color=red]//////////////////////////com.svse.util///////////////////////////[/color]
[color=blue]//分页辅助类[/color]
package com.svse.util;
public class PageHelp {
int totalcount;//总行数
int linesize;//每页显示的条数
int totalpage;// 总页数
int currentpage;//当前页
public PageHelp() {
// TODO Auto-generated constructor stub
}
public PageHelp(int currentpage, int linesize, int totalcount, int totalpage) {
super();
this.currentpage = currentpage;
this.linesize = linesize;
this.totalcount = totalcount;
this.totalpage = totalpage;
}
public int getTotalcount() {
return totalcount;
}
public void setTotalcount(int totalcount) {
this.totalcount = totalcount;
}
public int getLinesize() {
return linesize;
}
public void setLinesize(int linesize) {
this.linesize = linesize;
}
public int getTotalpage() {
return totalpage;
}
//设置总页数
public void setTotalpage() {
if(this.getTotalcount()%this.getLinesize()==0)
{
this.totalpage = this.getTotalcount()/this.getLinesize();
}
else
{
this.totalpage = this.getTotalcount()/this.getLinesize()+1;
}
}
public int getCurrentpage() {
return currentpage;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
}
[color=blue]//字符过滤器[/color]
package com.svse.util;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class Filter implements javax.servlet.Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("text/html;charset=utf-8");
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
[color=blue]//SessionFactory类[/color]
package com.svse.util;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class DefaultSessionFactory {
public static Session getSession()
{
return new Configuration().
configure().buildSessionFactory().openSession();
}
}
[color=red]/////////////////////////////com.svse.dao/////////////////////////////[/color]
[color=blue]//StudentDao类[/color]
package com.svse.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.svse.entity.Student;
import com.svse.util.DefaultSessionFactory;
public class StudentDao {
private Session session=null;
public StudentDao() {
this.session=DefaultSessionFactory.getSession();
}
//查询所有记录的条数
public long getTotalCount()
{
String hql="select count(stuname) from Student";
Query query=this.session.createQuery(hql);
return (Long)query.uniqueResult();
}
//获取当前分页后的集合
public List<Student> getCurrentList(int currentPage,int lineSize)
{
String hql="from Student";
Query query=this.session.createQuery(hql);
query.setFirstResult((currentPage-1)*lineSize);
query.setMaxResults(lineSize);
return query.list();
}
public List findAll()
{
String hql="from Student";
Query query=this.session.createQuery(hql);
return query.list();
}
}
[color=red]////////////////////////////com.svse.action///////////////////////////[/color]
[color=blue]//StudentAction类[/color]
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.svse.struts.action;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.svse.dao.StudentDao;
import com.svse.entity.Student;
import com.svse.util.PageHelp;
/**
* MyEclipse Struts
* Creation date: 06-21-2011
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class StudentAction extends DispatchAction {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
private StudentDao studao=new StudentDao();
public ActionForward findAllStu(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//int totalCount=(int)studao.getTotalCount();
int totalCount=studao.findAll().size();
System.err.println("xxxxxxxxx : "+totalCount);
PageHelp ph=new PageHelp();
ph.setTotalcount(totalCount);
ph.setLinesize(3);
ph.setTotalpage();
int currentPage=0;
if(request.getParameter("currentPage")==null)
{
currentPage=1;
}
else
{
currentPage=Integer.parseInt(request.getParameter("currentPage"));
//防止下标越界的情况
if(currentPage<1)
{
currentPage=1;
}
if(currentPage>ph.getTotalpage())
{
currentPage=ph.getTotalpage();
}
}
ph.setCurrentpage(currentPage);
List<Student> stulist=studao.getCurrentList(ph.getCurrentpage(), ph.getLinesize());
request.setAttribute("stulist", stulist);
request.setAttribute("pagehelp", ph);
System.err.println(stulist.size());
for (Student student : stulist) {
System.err.println(student.getStuname()+student.getStuid());
}
return mapping.findForward("tostulist");
}
}
[color=blue]//显示信息的JSP页面[/color]
<%@ page language="java" pageEncoding="utf-8" isELIgnored="false"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>Hibernate分页</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<table border="0" align="center" width="40%">
<tr align="center" bgcolor="pink">
<td colspan="5">学生详细信息</td>
</tr>
<tr align="center" bgcolor="pink">
<td colspan="5"><hr></td>
</tr>
<tr bgcolor="pink">
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
<td>地址</td>
</tr>
<logic:iterate id="s" name="stulist">
<tr bgcolor="pink">
<td>${s.stuid }</td>
<td>${s.stuname }</td>
<td>${s.stuage}</td>
<td>${s.stusex }</td>
<td>${s.stuaddr }</td>
</tr>
</logic:iterate>
<tr align="center" bgcolor="pink">
<td colspan="5"><hr></td>
</tr>
<tr bgcolor="pink">
<td colspan="5" width="700" align="center">
<a href="student.do?method=findAllStu¤tPage=1"><img src="image/head.gif" style="border: none;"/></a>
<a href="student.do?method=findAllStu¤tPage=${pagehelp.currentpage-1 }"><img src="image/top.gif" style="border: none;"/></a>
<a href="student.do?method=findAllStu¤tPage=${pagehelp.currentpage+1 }"><img src="image/next.gif" style="border: none;"/></a>
<a href="student.do?method=findAllStu¤tPage=${pagehelp.totalpage }"><img src="image/end.gif" style="border: none;"/></a>
</td>
</tr>
<tr align="center" bgcolor="pink">
<td colspan="5">
<font color="red">当前是:【${pagehelp.currentpage}】页, 一共【${pagehelp.totalpage}】页</font>
</td>
</tr>
</table>
</center>
</body>
</html:html>
[color=blue]//Struts-config-xml配置文件[/color]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings >
<action path="/student"
type="com.svse.struts.action.StudentAction"
parameter="method"
>
<forward name="tostulist" path="/showstu.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="com.svse.struts.ApplicationResources" />
</struts-config>
[color=blue]//Hibernate-cfg-xml配置文件[/color]
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">scott</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="myeclipse.connection.profile">oracle</property>
<property name="connection.password">tiger</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/svse/entity/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>