1、先新建个Web项目
2 、添加struts2依赖包和配置文件
右击工程,选择“myeclipse”在二级菜单中找到“Add Struts Capabiliies” 点击进入
3、添加spring框架依赖包和配置文件
右击工程,选择“myeclipse”在二级菜单中找到“Add Spring Capabiliies” 点击进入
4、 新建“mysql-test”数据库连接
首先,打开“MyEclipse Database Explorer”视图,步骤:工具栏“Widow” ——》“Open Perspective”——》“Other”——》“MyEclipse Database Explorer” ——“OK”
接着,在Myeclipse右上角找到切换视图的图标,切换到“MyEclipse Database Explorer”视图,在该视图上我们可以新建数据库连接,如下图
5、添加Hibernate框架依赖包和配置文件
右击工程,选择“myeclipse”在二级菜单中找到“Add Hibernate Capabiliies” 点击进入
数据源选择步骤4新建的数据库连接“mysql-test”
6、通过“mysql-test”数据库连接,我们可以生成hibernate实体类和映射文件,以users表为例,如下图
效果如下图:
7、添加Spring事务管理
首先,修改applicationContext.xml文件,修改<beans>标签,将标签的属性修改成:
<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/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
接着,再添加以下内容:
<!--声明一个事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- 声明一个事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 声明一个AOP切入点 -->
<aop:config>
<aop:pointcut id="daoMethods" expression="execution(* edu.dao.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods"/>
</aop:config>