之前分别写了集成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>
复制