Spring+hibernate 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.0.xsd">


<!-- define basic datasource by apache dbcp
虽然hibernate的高级版本已经明确的指出对dbcp不再提供支持,但是就开发阶段来说,
使用dbcp配置数据源未曾不可.....
-->
<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/schooldb?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="123456"/>
</bean>




<!-- define transaction aware data source proxy
定义proxy,是spring感知数据源....
-->
<bean id="transactionAwareDataSourceProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg>
<ref bean="basicDataSource" />
</constructor-arg>
</bean>




<!-- define session factory by transaction aware data source proxy
使用spring support的sessionfactory 配合spring已经感知的数据源来定义sessionfactory...
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="transactionAwareDataSourceProxy" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>mapping.hbm.xml</value>
</list>
</property>
</bean>






<!-- define dao by session factory
定义 data access object ,
次类需要 扩展 org.springframework.orm.hibernate3.support.HibernateDaoSupport...
-->
<bean id="hibernateBasedDAO" class="com.gmail.chrnc0000.hibernate.HibernateBasedDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>



<!-- define spring transaction manager by session factory -->
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>


<!--
定义 spring interceptor ,并且对需要回滚的异常进行配置...
-->
<bean id="transactionProxyFactoryBean" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="hibernateTransactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>





<!-- define service
定义interceptor 配合的 service ...
-->
<bean id="transactionProxyBasedService" parent="transactionProxyFactoryBean">
<property name="target">
<bean class="com.gmail.chrnc0000.spring.TransactionProxyBasedService">
<property name="hibernateBasedDAO">
<ref local="hibernateBasedDAO" />
</property>
</bean>
</property>
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值