关于spring 与Hibernate 结合使用的事务管理

applicationContext.xml  中是这样的    


<?xml version="1.0" encoding="UTF-8"?>

<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3305/structs_db</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>12345</value>
</property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingResources">
      <list>
        <value>com/lucas/bean/User.hbm.xml</value>
<value>com/lucas/bean/Scary.hbm.xml</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.HSQLDialect
      </value>
    </property>
  </bean>


<bean id="ht" class="org.springframework.orm.hibernate3.HibernateTemplate" >
  <property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="hibernateTransaction" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
  <property name="sessionFactory" ref="sessionFactory" />
  <property name="dataSource" ref="dataSource" />
</bean>
<bean id="scaryDaoImpl" class="com.lucas.dao.impl.ScaryDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="hibernateTransaction">
<tx:attributes>
<tx:method  name="tranfer"  rollback-for="Throwable"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="pc" expression="execution(* com.lucas.dao.impl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>

 
<bean id="loginAction" class="com.lucas.action.LoginAction" scope="prototype">
<property name="userDaoImpl" ref="userDaoImpl"/>
</bean>

</beans>

实现类中是这样的:

package com.lucas.dao.impl;




import org.springframework.orm.hibernate3.HibernateTemplate;


import com.lucas.dao.ScaryDao;


public class ScaryDaoImpl extends HibernateTemplate implements ScaryDao {


@Override
public void tranfer(){
// TODO Auto-generated method stub
String sql1="update Scary as s set s.banlace=s.banlace+1000 where s.id=1";
String sql2="update Scary as s set s.banlace=s.banlace-1000 wdhere s.id=2";
super.getSession().createQuery(sql1).executeUpdate();
super.getSession().createQuery(sql2).executeUpdate();
}
}


以及

package com.lucas.test;


import static org.junit.Assert.*;


import java.util.List;


import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.lucas.bean.Scary;
import com.lucas.dao.ScaryDao;


public class ScaryDaoImplTest {


@Before
public void setUp() throws Exception {
}


@Test
public void testTranfer() {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
ScaryDao ud=(ScaryDao)ac.getBean("scaryDaoImpl");
ud.tranfer();
}
}

可实现事务


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值