以前分別寫了集成struts2,以及spring3的關鍵問題,就剩hibernate4了,可是其中並不須要什麼特殊的地方。只是將hibernate的配置所有轉換到spring的配置中去而已。網上搜一搜有大量的技術文章,我這裏就不詳細贅述了,只是將本人的配置文件內容貼出來供你們參考:web
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
- default-lazy-init="true" default-autowire="byName">
- <import resource="datasource-config.xml"/>
- <import resource="hibernate-properties.xml"/>
- <import resource="transaction-config.xml"/>
- <import resource="application-project.xml"/>
- <import resource="../../resource/bean/base.xml"/>
- </beans>
上面配置部分,引用了多個其餘配置,這個文件也就是我得主配置文件,第一個導入,是數據源,第二個是hibernate的屬性以及映射配置,第三個是將事務叫給spring管理的配置。其餘的事項目級別的配置,以及業務部分開發的基礎配置,我們只關注與hibernate相關的配置,以下:spring
- 數據源配置,本人使用的proxool鏈接池
- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName">
- <value>org.logicalcobwebs.proxool.ProxoolDriver</value>
- </property>
- <property name="url">
- <value>proxool.core</value>
- </property>
- </bean>
- hibernate的屬性以及映射配置
- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="mappingResources">
- <list>
- <value>com/xk/model/XkBaseCity.hbm.xml</value>
- <value>com/xk/model/XkBaseProvince.hbm.xml</value>
- <value>com/xk/model/XkSystemDate.hbm.xml</value>
- <value>com/xk/model/XkTrain.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">false</prop>
- <prop key="hibernate.jdbc.use_scrollable_resultset">false</prop>
- <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
- <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
- </props>
- </property>
- </bean>
- 下面是事務管理配置:
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <aop:config>
- <aop:pointcut id="pointcutMethods" expression="execution(* com.xk..boimpl.*Impl.*(..))"/>
- <aop:advisor advice-ref="transactionAdvice" pointcut-ref="pointcutMethods"/>
- </aop:config>
- <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="get*" read-only="true" propagation="REQUIRED" />
- <tx:method name="find*" read-only="true" propagation="REQUIRED" />
- <tx:method name="query*" read-only="true" propagation="REQUIRED" />
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="set*" propagation="REQUIRED" />
- <tx:method name="put*" propagation="REQUIRED" />
- <tx:method name="save*" propagation="REQUIRED" />
- <tx:method name="update*" propagation="REQUIRED" />
- <tx:method name="delete*" propagation="REQUIRED" />
- <tx:method name="*" read-only="true" propagation="REQUIRED"/>
- </tx:attributes>
- </tx:advice>