Spring使用注解事务管理

  1. 本次采用的是使用注解这种方式,我感觉使用这种方式是最简单的。本次代码参考自慕课网
  2. 本事例是模仿银行转钱,中间发生异常,使用事务进行管理
  3. 定义接口

public interface AccountService {
   public void transfer(String out,String in ,int money);
}

4.定义接口实现类


import org.springframework.transaction.annotation.Transactional;
//使用注解
@ransactional  
public class AccontServiceImpl  implements AccountService {
   private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
    this.accountDao = accountDao;
}
    public void transfer(String out, String in, int money) {
        // TODO Auto-generated method stub
                  accountDao.outMoney(out, money);
                   int i=1/0;  //发生异常
                   accountDao.inMoney(in, money);
    }
}

5.定义Dao接口


public interface AccountDao {
   public void outMoney(String out,int money);
   public void inMoney(String in,int money);
}

6.定义Dao接口实现类


import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

    @Override
    public void outMoney(String out, int money) {
        // TODO Auto-generated method stub
    String sql="update account set money=money-? where name=?";
    this.getJdbcTemplate().update(sql,money,out);
    }

    @Override
    public void inMoney(String in, int money) {
        // TODO Auto-generated method stub
         String sql="update account set money=money+? where name=?";
            this.getJdbcTemplate().update(sql,money,in);
    }

}

7.测试类


import javax.annotation.Resource;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class Test {

    @Resource(name="accountService")

    private AccountService accountservice;
    @org.junit.Test
    public void test() {
        accountservice.transfer("aaa", "bbb", 100);
    }

}

8.配制文件

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

    <context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
          <property name="driverClass" value="${jdbc.driverClassName}"></property>
          <property name="jdbcUrl" value="${jdbc.url}"></property>
          <property name="user" value="${jdbc.username}"></property>
          <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 配置业务 -->
    <bean id="accountService" class="com.transaction2.AccontServiceImpl">
        <property name="accountDao" ref="accountDao"></property>

    </bean>
    <!-- 配置dao -->
    <bean id="accountDao" class="com.transaction2.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"> </property>
    </bean>

   <!-- 配置事务管理类 -->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配制注解 -->

    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>


  </beans>

9.项目结构
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wending-Y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值