struts+hibernate综合开发案例

    本例模仿的是部门与员工的关系,即一对多,本例只是一个简单案例,旨在让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相关文章介绍


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
struts2 spring hibernate mybatis是一种常见的网站开发技术栈组合。它们分别负责不同的功能和层面,共同协作完成网站开发。 在一个网站开发案例中,可以使用struts2作为前端框架,负责接收用户的请求并将其转发到后台处理。struts2提供了很多方便的特性,比如表单验证、拦截器等,可以帮助开发者更高效地开发和管理代码。 而spring作为后端框架,负责组织应用的结构和控制流程。它提供了依赖注入、AOP等功能,使开发者能够更加灵活地管理和操作对象。spring还可以方便地集成其他框架,比如hibernate和mybatis。 hibernate是一个ORM(Object-Relational Mapping)框架,用于将对象和数据库关系映射起来。开发者可以通过编写简洁的实体类和配置文件,自动实现对象和数据库之间的映射,并且可以很方便地进行数据库操作。 mybatis是另一个ORM框架,它采用了更加灵活的SQL映射方式。开发者可以在XML配置文件中编写SQL语句,并通过mybatis的接口来执行这些SQL语句。mybatis还提供了很多高级功能,比如动态SQL、缓存等,可以提升开发效率和系统性能。 综合使用这些技术,可以设计出一个完整的网站开发流程。比如,前端请求会先经过struts2框架进行处理,然后通过spring框架注入相应的业务逻辑对象,最后通过hibernate或mybatis与数据库进行交互。这种技术栈的优势是各个框架之间的解耦,可以提高代码的可维护性和扩展性。 在课堂中开发网站时,可以结合这些技术进行源码编写。学习者可以尝试使用struts2框架搭建前端页面,使用spring框架管理后端对象,使用hibernate或mybatis进行数据库操作。通过这样的实践,学习者能够更加深入了解这些框架的使用和原理,并且锻炼自己的编码能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值