struts2+spring+hibernate整合

来源:互联网 发布:什么是心知天气 编辑:程序博客网 时间:2024/05/02 01:07

3个框架整合分两步

首先把hibernate整合到spring中

jdbc.properties文件

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/kaiyuanjdbc.username=rootjdbc.password=roothibernate.dialect=org.hibernate.dialect.MySQLDialecthibernate.show_sql=falsehibernate.hbm2ddl.auto=update


 

在spring的配置文件中 如下配置:

<bean  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"  destroy-method="close">     <property name="locations">   <list>    <!-- 标准配置 -->    <value>classpath:jdbc.properties</value>   </list>     </property> </bean><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><!-- Connection Info --><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><!-- Connection Pooling Info --><property name="initialSize" value="5" /><property name="maxActive" value="100" /><property name="maxIdle" value="30" /><property name="maxWait" value="500" /><property name="defaultAutoCommit" value="true" /></bean><!-- Hibernate配置 --><bean id="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="hibernateProperties"><props><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop></props></property><property name="packagesToScan" value="com/datang/bean/" /></bean><!-- 事务管理 -->   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory"></property>   </bean>     <!-- 事务通知 -->   <tx:advice id="txAdvice" transaction-manager="transactionManager">       <tx:attributes>           <tx:method name="get*" read-only="true"/>           <tx:method name="*" propagation="REQUIRES_NEW" rollback-for="Exception"/>       </tx:attributes>   </tx:advice>     <!-- aop代理设置 -->   <aop:config proxy-target-class="true">       <aop:pointcut id="txPointcut" expression="execution(* com.hbs..*Service.*(..))"/>       <aop:pointcut id="logPointcut" expression="execution(* com.hbs.customer..*Service.*(..))"/>       <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" order="1"/>       <aop:aspect id="logAspect" ref="logInterceptor" order="2" >           <aop:after-throwing               pointcut-ref="logPointcut"                method="serviceIntercept" />       </aop:aspect>   </aop:config>     <!-- log拦截器类 -->   <bean id="logInterceptor" class="com.hbs.eventlog.EventLogInterceptor">       <property name="service" ref="logService"></property>   </bean>


在整合struts2和spring

在web.xml文件中如下配置


 

<listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><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>


这边有一个Struts2+spring+Hibernate的一个简单的登陆案例    导入eclipse ,可以直接运行

http://download.csdn.net/detail/zhangye2011z/4682823

原创粉丝点击