Hibernate.cfg.xml由Hibernate管理,Hibernate的session与Transaction由Spring管理

本文详细介绍了如何通过MyEclipse5.5环境,利用JDK1.5构建SSH框架,包括Hibernate、Spring、Struts的集成与应用。文章从构建包结构开始,逐步引入Spring支持、Struts支持、Hibernate支持,通过实例演示如何由Hibernate自动生成Entity类和DAO接口,以及在Spring的ApplicationContext中配置这些组件。同时,文章还讲解了如何在Spring中配置Struts的Action与ActionForm,并提供了一个完整的web.xml配置示例。最后,文章强调了在组合过程中可能出现的错误及解决方案,如依赖库缺失或冲突。

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


2009年04月09日 星期四 21:29

SSH框架的组合方式一:Hibernate.cfg.xml由Hibernate自己管理

说明:我开发的环境是:MyEclipse5.5与JDK1.5

1,应先写好包
com.mengya.entity:实本包
com.mengya.dao:DAO接口包
com.mengya.dao.imple实体包
com.megnya.util:uitl包

2,加上Spring支持

3,加上Struts支持

4,加上Hibernate支持

5,由Hibernate创建entity类和DAO
可以在dao中重载一下delete方法,由ID删除对象
由产生的DAO抽出接口来分别把它们分到com.megnya.dao与com.mengya.dao.imple包中

6,在Spring的applicationContext.xml中分为applicationContext-beans.xml(普通的beans)
和applicationContext-comon.xml(公共的beans)(个人习惯问题,可以不用分开)

7,修改applicationContext-comon.xml有DTD
修改为:
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
">
这样写的话后面写起来比较方便

applicationContext-comon.xml最后为:

<?xml version="1.0" encoding="UTF-8"?>
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
">

<!-- 配置公共的部分 -->

<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 配置事务传播属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="merge" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- 配置AOP -->
<aop:config>
<!-- com.mengya.dao.imple下在所有类,所有类的所有方法(..表示任何形式的参数)都是poincut切入点 -->
<aop:pointcut id="allMethod" expression="execution(* com.mengya.dao.imple.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod"/>
</aop:config>

</beans>

8,加上Struts的action与actionForm
在actionForm中封装Hibernate生成的Javabean
修改struts-config.xml中的action的type将com.mengya.struts.action.StuAction修改为:org.springframework.web.struts.DelegatingActionProxy

9,在Spring中配置Struts的action

Struts中的action的type值改为:org.springframework.web.struts.DelegatingActionProxy(代理)

<bean id="StudentDAO" class="com.mengya.dao.imple.StudentDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
注:DAO应继承HibernateDaoSupport
<!-- 配置Struts的action,bean的name值为action的path -->
<bean name="/stu" class="com.mengya.struts.action.StuAction">
<property name="studao" ref="StudentDAO" />
</bean>

10,在web.xml中配置
<!-- 使用Spring提供的encoding的filter -->
<filter>
<filter-name>springfitler</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springfiter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 若不在web.xml中配置如下内容则:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
-->


11,好了.OK,组合成功!

个人说明:集合后可能会出错

1,可能是少了commons-pool-1.3.jar包(MyEclipse5.5)

2,可能是要重盖hibernate-annotations.jar包(MyEclipse5.1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值