SSH整合

本文介绍如何将Spring、Hibernate与Struts三大框架进行整合。首先通过Spring管理Bean的生命周期并配置事务,接着利用Hibernate实现持久层操作,最后通过Struts完成MVC架构的搭建。文章详细展示了配置文件的编写过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.spring
导入spring的jar包
测试spring是否可以正常运行

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
        
      <!-- 在spring中注册action  由spring来接管action  -->
    <bean id="PetAction" class="com.my.action.PetAction">
      <property name="petService" ref="PetService"></property>
    </bean>
      
 </beans>    

2.整合Hibernate
导入hibernate的jar包
测试hibernate是否正常工作
在整合spring中,因为要整合spring 也就是spring接管hibernate
所以把hibernate.cfg.xml的配置配置到spring配置文件中

在Spring中首先要配置一个数据源(dbcp/c30p/jdbc/druid)
再配置一个sessionFactory(需要给SessionFactory注入一个数据源)
因为spring要接管hibernate的事务 所以要配置一个事务管理器
再通过aop的方式来加入事务管理

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
         http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
           http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
	

	<!--配置一个数据源 -->
	<bean id="myDataSource" class="org.apache.commons.dbcp2.BasicDataSource"
		destroy-method="close">
		<!-- 数据库驱动 -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<!-- 数据库路径 -->
		<property name="url" value="jdbc:mysql:///epet" />
		<!-- 数据库用户名 -->
		<property name="username" value="root" />
		<!-- 数据库密码 -->
		<property name="password" value="0000" />
	</bean>


	<!-- 配置一个SessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<!-- 应用数据库 因为要连接数据库 -->
		<property name="dataSource" ref="myDataSource" />
		<property name="mappingResources">
			<list>
				<value>com/my/bean/PetInfo.hbm.xml</value>
				<value>com/my/bean/PetDairy.hbm.xml</value>
				<value>com/my/bean/PetType.hbm.xml</value>
			</list>
		</property>

		<!--配置hibernate的属性配置 -->
		<property name="hibernateProperties">
			<value>
				hibernate.dialect=org.hibernate.dialect.MySQLDialect
				hibernate.show_sql =true
				hibernate.hbm2ddl.auto=update
			</value>
			<!-- <props> <prop key=" hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
				<prop key=" hibernate.show_sql">true</prop> </props> -->
		</property>
	</bean>

	<!--hibernate的事务也要交给spring 接管,所以需要配置一个事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!--使用spring aop 来管理的事务,所以要配置aop -->
	<aop:config>
		<aop:pointcut id="pointcut" expression="execution(* com.my.Impl.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
	</aop:config>

	<!--配置事务通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- propagation="REQUIRED -->
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="get*" read-only="true" />
			<tx:method name="show*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<bean id="PetDao" class="com.my.Impl.PetDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<bean id="PetService" class="com.my.seviceImpl.PetServiceImpl">
		<property name="petDaoImpl" ref="PetDao"></property>
	</bean>
	
	
	 <!-- <import resource="spring-ActionBean.xml" /> -->

</beans>    

3.struts 整合
导入struts的jar包
测试是否正常工作
导入插件包整合struts-plugin-2.5.2.jar

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
	<!--把struts生产action的权利交给spring -->
	 <constant name="struts.objectFactory" value="spring" />
	
	<package name="one" extends="json-default" namespace="/">
		<action name="PetAction_*" class="PetAction" method="{1}">
			<result name="show">/showPet.jsp</result>
			<result name="error">/error.jsp</result>
			<result type="json" />
			<allowed-methods>showPet,getALLpet,deleteById,updatePet,addPetDiary,login</allowed-methods>
		</action>
	</package>

</struts>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值