spring 与 hibernate 及事务的配置

本文详细介绍了Spring框架与Hibernate ORM工具的整合配置过程,包括Session Factory的设置、事务管理器的定义以及AOP方式的事务配置。通过XML配置文件展示了如何连接MySQL数据库,并设置Hibernate的属性来优化性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 spring 与hibernate 及事务的配置:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.         xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
  4.         xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
  5.         xmlns:tx="http://www.springframework.org/schema/tx"
  6.         xsi:schemaLocation="
  7.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  8.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  9.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  10.             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
  11.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  12.             
  13.     <!-- Hibernate SessionFactory -->
  14.     <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  15.             <property name="dataSource">
  16.                 <ref bean="mysqldataSource1" />
  17.             </property>
  18.             <!--  
  19.             <property name="mappingResources">
  20.                 <list>
  21.                     <value>com/xh/hibernate/entity/User.hbm.xml</value>
  22.                 </list>
  23.             </property
  24.             -->
  25.             <property name="mappingLocations">
  26.                 <list>
  27.                     <!--value>classpath:/com/company/club/**/*.hbm.xml</value-->
  28.                 </list>
  29.             </property>
  30.             
  31.             <property name="hibernateProperties">
  32.                 <props>
  33.                     <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  34.                     <prop key="hibernate.show_sql">true</prop>
  35.                     <prop key="hibernate.generate_statistics">true</prop>
  36.                     <prop key="hibernate.connection.release_mode">auto</prop>
  37.                     <prop key="hibernate.autoReconnect">true</prop>
  38.                 </props>
  39.             </property>
  40.     </bean>
  41.     <bean id="transactionManager"   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  42.         <property name="sessionFactory">
  43.             <ref local="hibernateSessionFactory" />
  44.         </property>
  45.     </bean>
  46.     <bean id="abstractTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
  47.         <property name="transactionManager" ref="transactionManager" />
  48.         <property name="transactionAttributes">
  49.             <props>
  50.                 <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
  51.                 <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
  52.                 <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
  53.                 <!--  prop key="set*">PROPAGATION_REQUIRED,timeout_80</prop-->
  54.                 <prop key="save*">PROPAGATION_REQUIRED,timeout_80</prop>
  55.                 <prop key="create*">PROPAGATION_REQUIRED,timeout_80</prop>
  56.                 <prop key="update*">PROPAGATION_REQUIRED,timeout_80</prop>
  57.                 <prop key="delete*">PROPAGATION_REQUIRED,timeout_80</prop>
  58.                 <prop key="*">PROPAGATION_REQUIRED</prop>
  59.             </props>
  60.         </property>
  61.     </bean>
  62. <!-- 
  63.     把事务配置在service层
  64.     <bean id="daoTarget" class="a dao class impl " lazy-init="true">
  65.         <property name="dataSource" ref="dataSource"/>
  66.     </bean>
  67.     <bean id="daoService" parent="abstractTransactionProxy">
  68.         <property name="target" ref="daoTarget"/>
  69.     </bean>
  70. -->
  71. </beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值