1.在web.xml中配置struts2的过滤器和spring的监听
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2.创建struts2.xml和applicationcontext.xml以及hibernate.cfg.xml
1)struts2.xml:控制业务跳转
2)applicationContext.xml:
<!--1. 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="user" value="root"></property>
<property name="password" value=""></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost/zry?characterEncoding=UTF-8"></property>
</bean>
<!--2. 创建session工厂类-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" autowire="byName">
<property name="configLocation">
<value>
classpath:hibernate.cfg.xml
</value>
</property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--3 创建事物管理对象 -->
<bean id="tx" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 创建事物通知 -->
<tx:advice id="advice" transaction-manager="tx">
<tx:attributes>
<tx:method name="add*" read-only="false" />
<tx:method name="save*" read-only="false" />
<tx:method name="del*" read-only="false" />
<tx:method name="update*" read-only="false" />
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- 操作数据库模板类 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate" autowire="byType">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 创建事务切入点-->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.hp.service.impl.*.*(..))" />
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config>
3)hibernate.cfg.xml:
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>