效果图
数据库表
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="fenye" class="org.struts.actions.fenyeAction">
<result name="SUCCESS">/studentfenye.jsp</result>
</action>
</package>
</struts>
hibernate.cfg.xml
<?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">
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/study</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.pool_size">1</property>
<property name="show_sql">false</property>
<mapping resource="org/hibernate/entity/student.hbm.xml"/>
<mapping resource="org/hibernate/entity/sc.hbm.xml"/>
<mapping resource="org/hibernate/entity/dept.hbm.xml"/>
<mapping resource="org/hibernate/entity/course.hbm.xml"/>
</session-factory>
</hibernate-configuration>
HibernateUtil.java
package org.hibernate.entity;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static Configuration configuration = new Configuration();
// 创建线程局部变量threadLocal,用来保存Hibernate的Session
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
// 使用静态代码块初始化Hibernate
static {
try {
configuration= new Configuration().configure(); // 读取配置文件hibernate.cfg.xml
sessionFactory = configuration.buildSessionFactory(); // 创建SessionFactory
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
// 获得SessionFactory实例
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
// 获得ThreadLocal 对象管理的Session实例.
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
// 通过SessionFactory对象创建Session对象
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
// 将新打开的Session实例保存到线程局部变量threadLocal中
threadLocal.set(session);
}
return session;
}
// 关闭Session实例
public static void closeSession() throws HibernateException {
// 从线程局部变量threadLocal中获取之前存入的Session实例
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
// 重建SessionFactory
public static void rebuildSessionFactory() {
try {
configuration.configure("/hibernate.cfg.xml"); // 读取配置文件hibernate.cfg.xml
sessionFactory = configuration.buildSessionFactory(); // 创建SessionFactory
} catch (Exception e) {
System.err.println("Error Creating SessionFactory ");
e.printStackTrace();
}
}
// 关闭缓存和连接池
public static void shutdown() {
getSessionFactory().close();
}
}
private static SessionFactory sessionFactory;
private static Configuration configuration = new Configuration();
// 创建线程局部变量threadLocal,用来保存Hibernate的Session
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
// 使用静态代码块初始化Hibernate
static {
try {
configuration= new Configuration().configure(); // 读取配置文件hibernate.cfg.xml
sessionFactory = configuration.buildSessionFactory(); // 创建SessionFactory
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
// 获得SessionFactory实例
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
// 获得ThreadLocal 对象管理的Session实例.
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
// 通过SessionFactory对象创建Session对象
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
// 将新打开的Session实例保存到线程局部变量threadLocal中
threadLocal.set(session);
}
return session;
}
// 关闭Session实例
public static void closeSession() throws HibernateException {
// 从线程局部变量threadLocal中获取之前存入的Session实例
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
// 重建SessionFactory
public static void rebuildSessionFactory() {
try {
configuration.configure("/hibernate.cfg.xml"); // 读取配置文件hibernate.cfg.xml
sessionFactory = configuration.buildSessionFactory(); // 创建SessionFactory
} catch (Exception e) {
System.err.println("Error Creating SessionFactory ");
e.printStackTrace();
}
}
// 关闭缓存和连接池
public static void shutdown() {
getSessionFactory().close();
}
}
student.java
package org.hibernate.entity;
import java.util.HashSet;
import java.util.Set;
public class student implements java.io.Serializable{
/**
*
*/
private static final long serialVersionUID = -8869903490483721193L;
private String sno;
private String sname;
private String sgender;
private int sage;
private String sdept;
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSgender() {
return sgender;
}
public void setSgender(String sgender) {
this.sgender = sgender;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public String getSdept() {
return sdept;
}
public void setSdept(String sdept) {
this.sdept = sdept;
}
}
student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-7-6 9:06:22 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="org.hibernate.entity.student" table="STUDENT">
<id name="sno" type="java.lang.String">
<column name="sno" />
<generator class="native" />
</id>
<property name="sname" type="java.lang.String">
<column name="sname" />
</property>
<property name="sgender" type="java.lang.String">
<column name="sgender" />
</property>
<property name="sage" type="int">
<column name="sage" />
</property>
<property name="sdept" type="java.lang.String">
<column name="sdept" />
</property>
</class>
</hibernate-mapping>
fenyeAction.java
package org.struts.actions;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.entity.HibernateUtil;
import org.hibernate.entity.student;
public class fenyeAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 3002274532278464323L;
private List<student> stu;
private int currentpage=1;//当前页
private int allpage;//总页数
private int pagesize=8;//页面显示数量
public List<student> getStu() {
return stu;
}
public void setStu(List<student> stu) {
this.stu = stu;
}
public int getCurrentpage() {
return currentpage;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
public int getAllpage() {
return allpage;
}
public void setAllpage(int allpage) {
this.allpage = allpage;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public String execute()throws Exception {
Session session = HibernateUtil.getSession();
Query count = session.createQuery("select count(*) from student");
int allrows = ((Long) count.iterate().next()).intValue();
allpage = (allrows-1)/pagesize+1;
Query query = session.createQuery("from student");
query.setFirstResult((currentpage-1)*pagesize);
query.setMaxResults(pagesize);
stu = query.list();
// for (student stu : stu) {
// System.out.println(stu.getSname()+" "+stu.getSgender());
// }
session.close();
return "SUCCESS";
}
}
studentfenye.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>分页显示</title>
<style>
table {
width:400px;
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table td {
text-align:center;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
</head>
<body>
<center>
<table border=1>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>系别</th>
<s:iterator value="stu">
<tr>
<td><s:property value="sno"/></td>
<td><s:property value="sname"/></td>
<td><s:property value="sgender"/></td>
<td><s:property value="sage"/></td>
<td><s:property value="sdept"/></td>
</tr>
</s:iterator>
</table>
</center>
</br>
<center>
第<s:property value="currentpage"/>页,共<s:property value="allpage"/>页
<a href="javascript:if(<s:property value="currentpage"/>!= 1)location='fenye.action?currentpage=1'"> <font size="2" color="blue">首页</font></a>
<a href="javascript:if(<s:property value="currentpage"/> > 1)location='fenye.action?currentpage=<s:property value="currentpage-1"/>'"><font size="2" color="red">上一页</font></a>
<a href="javascript:if(<s:property value="currentpage"/> < <s:property value="allpage"/>)location='fenye.action?currentpage=<s:property value="currentpage+1"/>'"><font size="2" color="red">下一页</font></a>
<a href="javascript:if(<s:property value="currentpage"/>!= <s:property value="allpage"/>)location='fenye.action?currentpage=<s:property value="allpage"/>'"><font size="2" color="blue">末页</font></a>
</center>
</body>
</html>