Struts+Spring+Hibernate实例--Spring的bean.xml(Hibernate_Context.xml)

5 篇文章 0 订阅
2 篇文章 0 订阅

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<!--数据源配置-->
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName">
   <value>org.postgresql.Driver</value>
  </property>
  <property name="url">
   <value>jdbc:postgresql://localhost/hibernate</value>
  </property>
  <property name="username">
   <value>hibernate</value>
  </property>
  <property name="password">
   <value>hibernate</value>
  </property>
 </bean>

<!--Hibernate的SessionFactory配置-->

 <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref local="dataSource"/>
  </property>
  <property name="mappingResources">
   <list>
    <value>TSysuser.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.PostgreSQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
 </bean>

<!--Hibernate的事务配置-->

 <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
 </bean>
 <bean id="jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
  <property name="dataSource">
   <ref bean="dataSource"/>
  </property>
 </bean>

<!--不清楚-->

 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate.HibernateTemplate">
  <property name="sessionFactory">
   <ref bean="sessionFactory"/>
  </property>
  <property name="jdbcExceptionTranslator">
   <ref bean="jdbcExceptionTranslator"/>
  </property>
 </bean>

<!--事务配置-->

 <bean id="baseTransactionProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager"/>
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="find*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>            
   </props>
  </property>
 </bean>

<!--对应的DAO的具体实现类-->

 <bean id="userDao" class="spring.dao.hibernate.UserHibernateDao">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
 </bean>

<!--封装上面的DAO-->

 <bean id="userManagerTarget" class="spring.UserManagerImpl">
  <property name="userDao">
   <ref local="userDao"/>
  </property>
 </bean>

<!--给上面的封装类添加事务处理配置-->

 <bean id="userManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager"/>
  </property>
  <property name="target">
   <ref local="userManagerTarget"/>
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="remove*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> 
   </props>
  </property>
 </bean>
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值