1.加入spring jar包,添加spring配置文件spring-config.xml
2.加入hibernate jar包
3.加入strut2 jar包 添加struts.xml文件
4.将添加的spring-config.xml与strut.xml文件加载到web.xml文件中
4.1载入spring配置文件语法
<!-- 载入spring配置文件,相当于ApplicationContext ctx = new ClassPathXmlApplicatcion(""); -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value>classpath:spring-config.xml</param-value> -->
<param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>
4.2载入Struts2配置文件语法
<!-- 载入struts2配置文件 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExeuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
5.配置datasource,sessionFactory(别忘记加上JAR包,下面这个是DBCP的JAR包)
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="myspace"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="show_sql">true</prop>
<prop key="format_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/ruicaiedu/ssh2/domain/Userinfo.hbm.xml</value>
</list>
</property>
</bean>
6.配置事务tx
<!-- 加载事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="get*" propagation="NOT_SUPPORTED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut
expression="execution(* com.ruicaiedu.ssh2.service.impl.UserinfoServiceImpl.*(..))"
id="pc_user" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc_user" />
</aop:config>
7.配置spring-bean文件,实现化持久,业务,控制层类并di
2.加入hibernate jar包
3.加入strut2 jar包 添加struts.xml文件
4.将添加的spring-config.xml与strut.xml文件加载到web.xml文件中
4.1载入spring配置文件语法
<!-- 载入spring配置文件,相当于ApplicationContext ctx = new ClassPathXmlApplicatcion(""); -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value>classpath:spring-config.xml</param-value> -->
<param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>
4.2载入Struts2配置文件语法
<!-- 载入struts2配置文件 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExeuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
5.配置datasource,sessionFactory(别忘记加上JAR包,下面这个是DBCP的JAR包)
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="myspace"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="show_sql">true</prop>
<prop key="format_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/ruicaiedu/ssh2/domain/Userinfo.hbm.xml</value>
</list>
</property>
</bean>
6.配置事务tx
<!-- 加载事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="get*" propagation="NOT_SUPPORTED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut
expression="execution(* com.ruicaiedu.ssh2.service.impl.UserinfoServiceImpl.*(..))"
id="pc_user" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc_user" />
</aop:config>
7.配置spring-bean文件,实现化持久,业务,控制层类并di