struts2+spring4.x+hibernate5.x

0.导入struts2+spring4.x+hibernate5.x的jar,这里额外用到c3p0.jar和aspectjweaver.jar,没有的自己网上找找

1.web.xml配置spring的监听器,还有struts2的过滤器和spring的session处理

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>sturts2SpringHibernate</display-name>
  
 
<!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

  <filter>
        <filter-name>sturts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    
    
    <filter>
	   <filter-name>SpringOpenSessionInViewFilter</filter-name>
	   <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
	  <init-param>  
            <param-name>singleSession</param-name>  
            <param-value>true</param-value>  
        </init-param>  
	 </filter>
    
  <filter-mapping>
    <filter-name>SpringOpenSessionInViewFilter</filter-name>
    <url-pattern>*.do,*.action</url-pattern>
  </filter-mapping>
  
    <!-- END SNIPPET: filter -->

    <filter-mapping>
        <filter-name>sturts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

2.db.properties的配置

jdbc.user=root
jdbc.password=soft1403
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///springHibernate
jdbc.initialPoolSize=5
jdbc.maxPoolSize=10

3.hibernate的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
    <!-- 配置hibernate的基本属性 -->
		<!-- 1.数据源需配到IOC容器中,所以在此不再配置数据源 
		     2.关联.hbm.xml也在IOC容器配置SessionFactory实列时在进行配置 
			3.配置hibernate的基本属性:SQL显示及格式化,生成数据表的策略以及二级缓存等。 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
	</session-factory>
</hibernate-configuration>
4.spring的配置文件

<?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:context="http://www.springframework.org/schema/context"
	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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	<!-- 配置一个自动扫描的包 -->
	

	<context:component-scan base-package="com.test"></context:component-scan>
	
	
	
	<!-- 导入资源文件,并配置数据源 -->
	<context:property-placeholder location="classpath:db.properties" />
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
	</bean>

	<!-- 配置Hibernate的SessionFactory 实列:通过Spring提供的LocalSessionFactoryBean进行配置 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- 配置数据源属性 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 配置hibernate配置文件的路径 -->
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
		<!-- 配置hibernate映射文件的位置及名称 ,可以使用通配符 -->
		<property name="mappingLocations" value="classpath:com/test/model/*.hbm.xml"></property>
	</bean>


	<!-- 配置spring的声明式事物 -->
	<!-- 1.配置事物管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 2.配置事物属性,需要事物管理器 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
		<tx:method name="add*" propagation="REQUIRED"/>  
			<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED" />
			<tx:method name="*" rollback-for="Exception"/>
		</tx:attributes>
	</tx:advice>
	<!-- 3.配置事物切点,并把切点与事物属性关联起来 -->
   <aop:config >
     <aop:pointcut expression="execution(* com.test.service.*.*(..))" id="txPointcut"/>
     <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
   </aop:config>


 <!-- 
<bean id="PersonDaoImpl" class="com.test.daoimpl.PersonDaoImpl">
   <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="PersonServicesImpl" class="com.test.serviceimpl.PersonServicesImpl">
    <property name="pdl" ref="PersonDaoImpl"></property>
</bean>
-->
<!-- 下面是整合sturts2的相关配置 -->
	<bean id="person" class="com.test.model.person">
		<property name="id" value="1"></property>
		<property name="name" value="夏根"></property>
		<property name="age" value="18"></property>
		<property name="sex" value="男"></property>

	</bean>
	<!-- 注意sturts2的action必须是非单列的因此要加上 scope="prototype" -->
	<bean id="personAction" class="com.test.action.personAction"
		scope="prototype">
		<property name="pe" ref="person"></property>
	</bean>

</beans>

5.结合上面的配置文件看下面的工程文件图


6.总结下千万注意aspectjweaver.jar包是spring事物管理service最主要的连接包,千万别漏了。。。。。。。开心就好  哈哈



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值