Spring 开启事务管理

1.导入 spring-tx-4.3.2.RELEASE.jar

2.开启spring的事务管理有两种声明式方法

- xml声明

<?xml version="1.0" encoding="UTF-8"?>
                    <!-- 导入tx约束-->
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 
        配置事务管理器,对于不同的dao框架具有不同的事务管理器的实现类  
        jdbc/MyBatis:org.springframework.jdbc.datasource.DataSourceTransactionManager
        hibernate:org.springframework.orm.hibernate3.HibernateTransactionManager
    --> 
    <bean id="transactionManager"   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <!-- 注入连接池(dbcp,c3p0....)-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

        <!-- 配置事务管理器具体要增强哪个方法-->
    <tx:advice id="advice" transaction-manager="transactionManager">
        <tx:attributes>                 
            <tx:method name=“transfarAccounts"/>
        </tx:attributes>
    </tx:advice >

        <!-- 配置切入面和切点(spring实现事务是通过aop的方式实现的) -->
    <aop:config>
        <aop:pointcut expression="execution(* cn.yellowimg.service.transfarAccounts(..))" id="pointcut1"/>
        <aop:advisor advice-ref="advice" pointcut-ref="pointcut1"/>
    </aop:config>
</beans>

- 注解声明

<?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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <!-- 
        配置事务管理器,对于不同的dao框架具有不同的事务管理器的实现类  
        jdbc/MyBatis:org.springframework.jdbc.datasource.DataSourceTransactionManager
        hibernate:org.springframework.orm.hibernate3.HibernateTransactionManager
    --> 
    <bean id="transactionManager"   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <!-- 注入连接池(dbcp,c3p0....)-->
        <property name="dataSource" ref="dataSource"></property>
    </bean> 
    <!-- 注入连接池(dbcp,c3p0....)-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

在需要开启事务的类上声明注解

//开启事务,该类上的所有方法都会开启事务
@Transactional
public class AccountService {

    @Resource(name="accountDao")
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void makemoney()
    {
        accountDao.edit("小马", 1000);
        accountDao.edit("小王", -1000);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值