之前分别寫了內建struts2,以及spring3的關鍵問題,就剩hibernate4了,但是其中并不需要什麼特殊的地方。隻是将hibernate的配置全部轉換到spring的配置中去而已。網上搜一搜有大量的技術文章,我這裡就不詳細贅述了,隻是将本人的配置檔案内容貼出來供大家參考:
<?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相關的配置,如下:
資料源配置,本人使用的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>
複制