本例模仿的是部门与员工的关系,即一对多,本例只是一个简单案例,旨在让struts和hibernate实现基本的整合,了解两大框架的基本应用,下面是关键代码配置详细信息
1.hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库连接信息 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hib-demo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- session创建方式 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 加载映射 -->
<mapping resource="cn/itcast/entity/Dept.hbm.xml"/>
<mapping resource="cn/itcast/entity/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration><!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库连接信息 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hib-demo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- session创建方式 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 加载映射 -->
<mapping resource="cn/itcast/entity/Dept.hbm.xml"/>
<mapping resource="cn/itcast/entity/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<h2>部门:<s:property value="#request.dept.deptName"/></h2>
<!-- 部门下的员工(懒加载数据) -->
<table border="1" align="center">
<tr>
<td>员工编号</td>
<td>员工姓名</td>
<td>员工薪水</td>
</tr>
<s:if test="#request.dept.emps!=null">
<s:iterator var="emp" value="#request.dept.emps">
<tr>
<td><s:property value="#emp.empId"/></td>
<td><s:property value="#emp.empName"/></td>
<td><s:property value="#emp.salary"/></td>
</tr>
</s:iterator>
</s:if>
<s:else>
<tr><td colspan="3">没有员工信息!</td></tr>
</s:else>
</table>
</body>
</html>
3.配置该项目下面WebRoot/WEB-INF下面的web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 自己的过滤器 -->
<!-- struts过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4.struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<struts>
<package name="dept" extends="struts-default">
<!-- 拦截器配置 -->
<interceptors>
<interceptor name="sessionInterceptor"
class="cn.itcast.interceptor.SessionInterceptor"></interceptor>
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="sessionInterceptor"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<!-- action配置 -->
<action name="show" class="cn.itcast.action.DeptAction">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
5.SessionInterceptor (配置拦截器)
package cn.itcast.interceptor;
import org.hibernate.Session;
import cn.itcast.utils.HibernateUtils;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class SessionInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
try{
// 1. 先创建Session
Session session=HibernateUtils.getSession();
// 2. 开启事务
session.beginTransaction();
// 3. 执行Action
String result=invocation.invoke();
// 4. 提交事务
session.getTransaction().commit(); // 不需要关闭session
return result;
}catch(Exception e){
e.printStackTrace();
return "error";
}
}
}
此外,还有struts和hibernate的夹包,还需要导入,Dept和Employee类及映射文件都还没有写,在这里忽略,如果想知道其他文件,请参考我的hibernate相关文章介绍