Hibernater与spring

转载:http://hance1000000.iteye.com/blog/454580

在整合的时候 保留hibernate的配置文件
在spring的配置文件中这么写:

View Code
<bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>

如果不保留配置文件,统一由spring管理则:

View Code
    <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/pojo/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>

spring与hibernate整合 最重要就是提供声明式服务,管理事物

View Code
    <!-- 事务处理设置 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- 所有方法使用PROPAGATION_REQUIRED类型的事务 -->
<bean id="interceptorTransaction"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 管理所有以Service结尾的Bean -->
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>interceptorTransaction</value>
</list>
</property>
</bean>

 

posted @ 2012-02-07 15:58  xiaoyue001  阅读(212)  评论(0编辑  收藏  举报