右键项目,选择MyEclipse->add Spring Cabalitiles
版本选择Spring3.1 勾选前面四个,点击finsh即可
右键项目,选择MyEclipse->add Hibernation Cabalitiles(同上)
2.版本选择3.3,即下图
一直下一步,到下图是把左上角的勾取消,点击完成
1.右键项目,选择MyEclipse->add Struts Cabalitiles(同上)
版本选择2.1,点击下一步
勾选与spring相关的选项,如图,点击完成
注意:无需创建abstract 类,可取消,以及选择Spring DAO,并将创建的dao放进dao包里面,一直下一步即可
配置sessionFactory属性。
注入所有action类以及dao类。注意:注入的dao类必须在原来的类中添加属性以及setter方法
启动Spring:打开web.xml
第一步:选择Context Parameters
Name:contextConfigLocation Value:classpath:applicationContext.xml
第二步:选择Listeners 查找contextLoaderLister
注意:action中的class是spring配置文件中注入的id
通过Spring配置管理Hibernate事务。
注意:
Spring配置事务时:
切入目标必须有接口实现;
在代码中必须要使用接口类型。
创建接口:鼠标指向dao类 选择Refactor Extract Interface
注意:取名最好在原来的名字前面加个大写I,并且选择需要的方法勾上即可
Spring中配置事务管理
<!--先在applicationContext.xml文件头部修改添加tx和aop空间(不修改已有的空间)-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
?
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
?
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
?
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
在java代码里面输入hibernatetransaction,然后查看导入的包复制即可(class中的长代码)
<!-- 注入事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 注入事务管理通知器 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 配置哪些方法开启事务 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<!-- 配置哪些方法只读处理 -->
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 事务AOP配置 -->
<!--
创建切入点,指示切入哪些方法
表达式:* dao.impl.*.*(..)
意思表示:任何返回类型 dao.impl.任何类.任何方法(不限参数)
-->
<aop:config>
<aop:pointcut expression="execution(* dao.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config>
扩展:消除懒加载
重点:找到OpenSessionViewFilter
第二步 在web.xml文件中配置地址
注意:过滤器必须在所有dao类注入前配置好
原文:https://www.cnblogs.com/caledown/p/14478405.html