1.在applicationContext中配置数据源和sessionFactory
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<!-- 数据库驱动 -->
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<!--数据库URL -->
<property name="url">
<value>jdbc:oracle:thin:@192.168.1.154:1526:test</value>
</property>
<!-- 数据库用户名 -->
<property name="username">
<value>test</value>
</property>
<!-- 数据库密码 -->
<property name="password">
<value>test</value>
</property>
</bean>
<!--定义Hibernate 的SessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource">
<ref local="dataSource"/>
</property>
<!-- hbm映射文件 -->
<property name="mappingResources">
<list>
<value>com/test/ticket/vo/Testvo.hbm.xml</value>
</list>
</property>
<!-- sessionFactory属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
2.配置事务
<!-- 配置事务 -->
<!-- 事务管理器 使用适用于Hibernate的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 事务拦截器 -->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 配置自动创建事务 -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Dao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
3.dao继承HibernateDaoSupport
public class TestDao extends HibernateDaoSupport implements TestInf{
public void savenew(TestVO testvo) throws Exception{
this.getSession().save(testvo);
}
}