Spring注解实现声明式事务

准备:

jar包:

aopalliance-1.0.jar
commons-dbcp-1.4.jar
commons-logging-1.2.jar
commons-pool-1.6.jar
mysql-connector-java-5.1.0-bin.jar
spring-aop-4.3.9.RELEASE.jar
spring-beans-4.3.9.RELEASE.jar
spring-context-4.3.9.RELEASE.jar
spring-core-4.3.9.RELEASE.jar
spring-expression-4.3.9.RELEASE.jar
spring-jdbc-4.2.0.RELEASE.jar
spring-tx-4.3.9.RELEASE.jar

1.Dao层

创建AccountDao.java接口

package com.zy.dao;

public interface AccountDao {
    /**
     * 汇款
     * @param outer 汇款人
     * @param money 汇款金额
     */
    public void out(String outer,int money);
     
    /**
     * 收款
     * @param inner 收款人
     * @param money 收款金额
     */
    public void in(String inner,int money);
 
}

2.Dao层接口实现

创建AccountDaoImpl.java

package com.zy.dao.impl;

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

import com.zy.dao.AccountDao;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
	 
    /**
     * 根据用户名减少账户金额
     */
    @Override
    public void out(String outer, int money) {
        this.getJdbcTemplate().update("update account set money = money - ? where username = ?",money,outer);
         System.out.println(outer+"的钱-"+money);
    }

    /**
     * 根据用户名增加账户金额
     */
    @Override
    public void in(String inner, int money) {
        this.getJdbcTemplate().update("update account set money = money + ? where username = ?",money,inner);
        System.out.println(inner+"的钱+"+money);
    }
 
}

 

3.Service层实现接口

创建AccountService.java接口

package com.zy.service;

public interface AccountService {
    
    /**
     * 转账
     * @param outer 汇款人
     * @param inner 收款人
     * @param money 交易金额
     */
    public void transfer(String outer,String inner,int money);
 
}

创建AccountServiceImpl.java实现

package com.zy.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zy.dao.AccountDao;
import com.zy.service.AccountService;

@Transactional(propagation=Propagation.REQUIRED, isolation = Isolation.DEFAULT)
@Service("accountService")
public class AccountServiceImpl implements AccountService{
	@Resource(name="accountDao")
    private AccountDao accountDao;
     
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    @Override
    public void transfer(String outer, String inner, int money) {
        accountDao.out(outer, money);
//        当事务中出现异常,事务将会回滚
//        int i = 1/0;
        accountDao.in(inner, money);
    }
 
}

4.配置applicationContext.xml

  1. 配置数据库信息
  2. 给Dao层注入数据库信息
  3. 给Service注入Dao层
  4. 注册事务管理器
  5. 将事务管理器交给Spring
<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.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
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">
     <!-- 配置数据库信息 -->
   	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  >
		 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		 <property name="url"  value="jdbc:mysql://localhost:3306/smbms"></property>
         <property name="username"  value="root"></property>
         <property name="password" value="root"></property>
	</bean>
    
    <!-- 将dao层注入数据库信息 -->
    <bean id="accountDao" class="com.zy.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
   
   	<!-- 给service注入 -->
    <bean id="accountService" class="com.zy.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
     
     
     <!-- 声明式事务处理实现转账(基于AOP的 注解 配置) -->
     <!-- 1 注册事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 2 将管理器交予spring
        * transaction-manager 配置事务管理器
        * proxy-target-class
            true : 底层强制使用cglib 代理
   	-->
    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
      
</beans>

5.测试Test

创建test.java测试类

package com.zy.Test;

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

import com.zy.service.AccountService;

public class test{
    
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = (AccountService) context.getBean("accountService");
        //Tom 向 Marry 转账1000
        accountService.transfer("Tom", "Marry", 1000);
	}
	
	/**
	 * 采用注解方式测试类  不用写main()函数
    @Test
    public void testNoTransaction(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService account = (AccountService) context.getBean("accountService");
        //Tom 向 Marry 转账1000
        account.transfer("Tom", "Marry", 1000);
    }
	 */
}

6.运行结果

7.项目结构

当事务过程出现异常时,会自动回滚。不会提交,不会对数据库产生影响。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值