javascript
Spring+Hibernate的典型配置
Spring 框架提供了對 Hibernate、JDO 和 iBATIS SQL Maps 的集成支持。Spring 對 Hibernate 的支持是第一級的,整合了許多 IOC 的方便特性,解決了許多典型的 Hibernate 集成問題??蚣軐?Hibernate 的支持符合 Spring 通用的事務(wù)和數(shù)據(jù)訪問對象(DAO)異常層次結(jié)構(gòu)。
1、配置datasource
現(xiàn)在常用的開源數(shù)據(jù)連接池主要有c3p0、dbcp和proxool三種,其中:hibernate開發(fā)組推薦使用c3p0;spring開發(fā)組推薦使用dbcp(dbcp連接池有weblogic連接池同樣的問題,就是強行關(guān)閉連接或數(shù)據(jù)庫重啟后,無法reconnect ,告訴連接被重置,這個設(shè)置可以解決);hibernate?in action推薦使用c3p0和proxool。
使用dbcp的配置片段例子:
1 <bean id="exampleDataSource" class="org.apache.commons.dbcp.BasicDataSource"> 2 <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 3 <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8" /> 4 <property name="username" value="root" /> 5 <property name="password" value="" /> 6 <property name="initialSize" value="5" /> 7 <property name="maxActive" value="10" /> 8 </bean>使用proxool的配置片段例子:
1 <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> 2 <property name="alias" value="proxoolDataSource"/> 3 <property name="driver" value="com.mysql.jdbc.Driver" /> 4 <property name="driverUrl" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8" /> 5 <property name="user" value="root" /> 6 <property name="password" value="" /> 7 <property name="statistics" value="1m,15m,1h,1d" /> 8 <property name="maximumConnectionCount" value="40"/> 9 <property name="minimumConnectionCount" value="5"/> 10 <property name="simultaneousBuildThrottle" value="30"/> 11 </bean>讀配置文件的方式引用屬性:?
1 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 2 <property name="location" value="/WEB-INF/jdbc.properties"/> 3 </bean> 4 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 5 <property name="driverClassName" value="${jdbc.driverClassName}"/> 6 <property name="url" value="${jdbc.url}"/> 7 <property name="username" value="${jdbc.username}"/> 8 <property name="password" value="${jdbc.password}"/> 9 </bean>配置文件“jdbc.properties”內(nèi)容為:
jdbc.driverClassName= com.mysql.jdbc.Driver jdbc.url= jdbc:mysql://localhost:3309/sampledb jdbc.username=root jdbc.password=12342、配置sessionFactory
使用Hibernate的主要接口是org.hibernate.Session。Session接口提供了基本的CRUD數(shù)據(jù)訪問功能,獲取Hibernate Session是通過Hibernate的SessionFactory接口,負責Session的打開、管理和關(guān)閉。Spring提供了Hibernate Session工廠Bean來獲取Hibernate的SessionFactory。
如果使用XML文件配置持久化域的對象,那么需要使用Spring提供的LocalSessionFactoryBean:
1 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 2 <property name="dataSource" ref="dataSource"/> 3 <property name="mappingDirectoryLocations"> 4 <list> 5 <value>classpath:com/demo/model/mappings</value> 6 </list> 7 </property> 8 <property name="hibernateProperties"> 9 <props> 10 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 11 </props> 12 </property> 13 </bean>如果使用Annotation注解方式來定義持久化,那么需要使用AnnotationSessionFactoryBean:
1 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean"> 2 <property name="dataSource" value="dataSource" /> 3 <property name="packageToScan" value="com.habuma.spitter.domain" /> 4 <property name="hibernateProperties"> 5 <props> 6 <prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop> 7 </props> 8 </property> 9 </bean>聲明了sessionFactory后,在DAO中可以使用@Autowired和@Qualifier("sessionFactory")注入屬性訪問sessionFactory。
Hibernate3之后,引入了上下文session,以保證每個事物使用同一session。Hibernate的SessionFactory提供獲取session的方法是getCurrentSession (獲得與當前線程綁定的session)。內(nèi)部通過代理封裝,此方式得到的session不僅和當前線程綁定,也無需手動開關(guān)。默認在事務(wù)提交之后,session自動關(guān)閉。引入Spring之后,sessionfactory的創(chuàng)建等都交給Spring管理,用戶可以不再考慮session的管理,事務(wù)的開啟關(guān)閉,只需配置事務(wù)即可。
另外在Web應(yīng)用中,session關(guān)閉后,因延遲加載導致前臺無法顯示的問題以往解決方式為強制全部加載,現(xiàn)在也可通過在web.xml中配OpenSessionInViewFilter來解決。如果擔心OpenSessionInViewFilter會使系統(tǒng)出現(xiàn)數(shù)據(jù)庫資源的性能瓶頸,則要設(shè)計好Service層的服務(wù)接口,使延遲加載在Service層內(nèi)完成。
當Session出現(xiàn)異常時,如果需要Spring自動回滾事務(wù),必須使異常以Spring非檢查型異常的形式重新拋出。這需要在Spring應(yīng)用上下文中添加一個Bean:
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />?3、配置Hibernate事務(wù)
Spring并不直接管理事物,而是提供了多種事物管理器,它們將事務(wù)管理的職責委托給JTA或其他持久化機制所提供的平臺相關(guān)的事務(wù)實現(xiàn)。如果程序的持久化通過Hibernate實現(xiàn),那么需要使用HibernateTransactionManager,需要在Spring上下文定義中添加如下的<bean>聲明,sessionFactory屬性需要裝配Hibernate SessionFactory。Spring提供了一個tx配置命名空間,借助它可以簡化Spring中的聲明式事務(wù)。:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/tx 9 http://www.springframework.org/schema/tx/spring-tx.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop.xsd"> 12 13 <!-- 聲明事務(wù)管理器開始 --> 14 <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 15 <property name="sessionFactory" ref="sessionFactory"/> 16 </bean> 17 <!-- 聲明事務(wù) --> 18 <tx:advice id="txAdvice" transaction-manager="txManager"> 19 <tx:attributes> 20 <tx:method name="save*" propagation="REQUIRED" /> 21 <tx:method name="update*" propagation="REQUIRED" /> 22 <tx:method name="merge*" propagation="REQUIRED" /> 23 <tx:method name="delete*" propagation="REQUIRED" /> 24 <tx:method name="enable*" propagation="REQUIRED" /> 25 <tx:method name="disable*" propagation="REQUIRED" /> 26 <tx:method name="get*" propagation="REQUIRED" read-only="true" /> 27 <tx:method name="count*" propagation="REQUIRED" read-only="true" /> 28 <tx:method name="find*" propagation="REQUIRED" read-only="true" /> 29 <tx:method name="list*" propagation="REQUIRED" read-only="true" /> 30 <tx:method name="*" propagation="REQUIRED" read-only="true" /> 31 </tx:attributes> 32 </tx:advice>?其中,<tx:method>有多個屬性,propagation="REQUIRED”表示該方法需要事務(wù),read-only="true”指定該方法事務(wù)為只讀。
聲明完畢事務(wù)之后,就可以通過Spring的AOP技術(shù)將配置好的事務(wù)織入Service方法中去:
1 <aop:config> 2 <!-- 通過aop定義事務(wù)增強切面 --> 3 <aop:pointcut id="txPointcut" expression="execution(* com.infrastructure.project.base.service..*.*(..))" /> 4 <!-- 引用事務(wù)增強 --> 5 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> 6 </aop:config>如果使用注解配置聲明式事務(wù),即通過@Transactional對需要事務(wù)增強的Bean接口實現(xiàn)類或方法進行標注,則需要在Spring容器中配置基于注解的事務(wù)增強驅(qū)動:
1 <tx:annotation-driven transaction-manager="txManager"/>然后即可在Service中使用@Transactional注解聲明事務(wù):
1 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true) 2 public class SpitterServiceImpl implements SpitterService{ 3 ... 4 @Transactional(propagation = Propagation.REQUIRED, readOnly = false) 5 public void addSpitter(Spitter spitter){ 6 ... 7 } 8 ... 9 }?4、將以上整合,典型的配置文件如下:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/tx 9 http://www.springframework.org/schema/tx/spring-tx.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop.xsd"> 12 13 <!-- 配置dataSource --> 14 <bean id="exampleDataSource" class="org.apache.commons.dbcp.BasicDataSource"> 15 <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 16 <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8" /> 17 <property name="username" value="root" /> 18 <property name="password" value="" /> 19 <property name="initialSize" value="5" /> 20 <property name="maxActive" value="10" /> 21 </bean> 22 23 <!-- 聲明sessionFactory --> 24 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 25 <property name="dataSource" ref="dataSource"/> 26 <property name="mappingDirectoryLocations"> 27 <list> 28 <value>classpath:com/demo/model/mappings</value> 29 </list> 30 </property> 31 <property name="hibernateProperties"> 32 <props> 33 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 34 </props> 35 </property> 36 </bean> 37 38 <!-- 聲明事務(wù)管理器開始 --> 39 <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 40 <property name="sessionFactory" ref="sessionFactory"/> 41 </bean> 42 43 <!-- 聲明事務(wù) --> 44 <tx:advice id="txAdvice" transaction-manager="txManager"> 45 <tx:attributes> 46 <tx:method name="save*" propagation="REQUIRED" /> 47 <tx:method name="update*" propagation="REQUIRED" /> 48 <tx:method name="merge*" propagation="REQUIRED" /> 49 <tx:method name="delete*" propagation="REQUIRED" /> 50 <tx:method name="enable*" propagation="REQUIRED" /> 51 <tx:method name="disable*" propagation="REQUIRED" /> 52 <tx:method name="get*" propagation="REQUIRED" read-only="true" /> 53 <tx:method name="count*" propagation="REQUIRED" read-only="true" /> 54 <tx:method name="find*" propagation="REQUIRED" read-only="true" /> 55 <tx:method name="list*" propagation="REQUIRED" read-only="true" /> 56 <tx:method name="*" propagation="REQUIRED" read-only="true" /> 57 </tx:attributes> 58 </tx:advice> 59 60 <!-- 織入事務(wù) --> 61 <aop:config> 62 <aop:pointcut id="txPointcut" expression="execution(* com.infrastructure.project.base.service..*.*(..))" /> 63 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> 64 </aop:config>?
轉(zhuǎn)載于:https://www.cnblogs.com/afeng7882999/p/4297908.html
總結(jié)
以上是生活随笔為你收集整理的Spring+Hibernate的典型配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (011) Linux之高级键盘技巧
- 下一篇: POJ 1637 Sightseeing