S2S3H4框架深度集成搭建(3) hibernate的深度集成

以前分別寫了集成struts2,以及spring3的關鍵問題,就剩hibernate4了,可是其中並不須要什麼特殊的地方。只是將hibernate的配置所有轉換到spring的配置中去而已。網上搜一搜有大量的技術文章,我這裏就不詳細贅述了,只是將本人的配置文件內容貼出來供你們參考:web

  
  
  
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.        xmlns:aop="http://www.springframework.org/schema/aop" 
  4.        xmlns:tx="http://www.springframework.org/schema/tx" 
  5.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.            http://www.springframework.org/schema/tx   
  9.            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  10.            http://www.springframework.org/schema/aop   
  11.            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"  
  12.            default-lazy-init="true" default-autowire="byName"> 
  13.            
  14.     <import resource="datasource-config.xml"/>   
  15.     <import resource="hibernate-properties.xml"/> 
  16.     <import resource="transaction-config.xml"/>   
  17.     <import resource="application-project.xml"/>     
  18.     <import resource="../../resource/bean/base.xml"/>   
  19. </beans> 

 上面配置部分,引用了多個其餘配置,這個文件也就是我得主配置文件,第一個導入,是數據源,第二個是hibernate的屬性以及映射配置,第三個是將事務叫給spring管理的配置。其餘的事項目級別的配置,以及業務部分開發的基礎配置,我們只關注與hibernate相關的配置,以下:spring

 

  
  
  
  
  1. 數據源配置,本人使用的proxool鏈接池     
  2. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">     
  3.         <property name="driverClassName">     
  4.             <value>org.logicalcobwebs.proxool.ProxoolDriver</value>     
  5.         </property>     
  6.         <property name="url">     
  7.             <value>proxool.core</value>     
  8.         </property>     
  9.     </bean>        
  10. hibernate的屬性以及映射配置  
  11.  
  12. <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
  13.         <property name="dataSource" ref="dataSource"/> 
  14.         <property name="mappingResources"> 
  15.             <list> 
  16.                 <value>com/xk/model/XkBaseCity.hbm.xml</value> 
  17.                 <value>com/xk/model/XkBaseProvince.hbm.xml</value> 
  18.                 <value>com/xk/model/XkSystemDate.hbm.xml</value> 
  19.                 <value>com/xk/model/XkTrain.hbm.xml</value> 
  20.             </list> 
  21.         </property> 
  22.         <property name="hibernateProperties"> 
  23.             <props> 
  24.                 <prop key="hibernate.show_sql">true</prop> 
  25.                 <prop key="hibernate.format_sql">false</prop> 
  26.                 <prop key="hibernate.jdbc.use_scrollable_resultset">false</prop> 
  27.                 <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> 
  28.                 <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>   
  29.             </props> 
  30.         </property> 
  31.     </bean>     
  32.  
  33. 下面是事務管理配置:  
  34.  <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
  35.         <property name="sessionFactory" ref="sessionFactory"/> 
  36.     </bean> 
  37.  
  38.     <aop:config> 
  39.         <aop:pointcut id="pointcutMethods" expression="execution(* com.xk..boimpl.*Impl.*(..))"/> 
  40.         <aop:advisor advice-ref="transactionAdvice" pointcut-ref="pointcutMethods"/> 
  41.     </aop:config> 
  42.       
  43.     <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> 
  44.         <tx:attributes> 
  45.             <tx:method name="get*" read-only="true" propagation="REQUIRED" /> 
  46.             <tx:method name="find*" read-only="true" propagation="REQUIRED" /> 
  47.             <tx:method name="query*" read-only="true" propagation="REQUIRED" /> 
  48.             <tx:method name="add*" propagation="REQUIRED" /> 
  49.             <tx:method name="set*" propagation="REQUIRED" /> 
  50.             <tx:method name="put*" propagation="REQUIRED" /> 
  51.             <tx:method name="save*" propagation="REQUIRED" /> 
  52.             <tx:method name="update*" propagation="REQUIRED" /> 
  53.             <tx:method name="delete*" propagation="REQUIRED" /> 
  54.             <tx:method name="*" read-only="true" propagation="REQUIRED"/> 
  55.         </tx:attributes> 
  56.     </tx:advice>
相關文章
相關標籤/搜索