使用aop解决事务问题(xml版)

一、引入依赖

使用aop解决事务问题(xml版)_第1张图片
pom.xml代码:



    4.0.0

    com.william
    spring_day04_01_tx_aop_xml
    1.0-SNAPSHOT

    
        1.8
        1.8
    
    
        
            mysql
            mysql-connector-java
            5.1.47
        
        
            org.springframework
            spring-context-support
            5.0.2.RELEASE
        
        
            org.springframework
            spring-test
            5.0.2.RELEASE
        
        
            com.mchange
            c3p0
            0.9.5.2
        
        
            junit
            junit
            4.12
        
        
            commons-dbutils
            commons-dbutils
            1.4
        
        
            org.aspectj
            aspectjweaver
            1.8.9
        
    


二、目录结构

使用aop解决事务问题(xml版)_第2张图片

三、applicationContext.xml配置文件

使用aop解决事务问题(xml版)_第3张图片



    
    
    
    
    
    
    
        
        
        
        
    
    
  
    
    
    
    
    
    
        
        
        
        
        
            
            
            
            

        
    

四、Class文件

1.AccountDaoImpl

代码:

package com.william.Dao.Impl;

import com.william.Dao.AccountDao;
import com.william.domain.Account;
import com.william.utils.ConnectionUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

/**
 * @author :lijunxuan
 * @date :Created in 2019/5/27  16:00
 * @description :
 * @version: 1.0
 */
@Repository
public class AccountDaoImpl implements AccountDao {
    @Autowired
    ConnectionUtils connectionUtils;
    @Autowired
    QueryRunner queryRunner;
    @Override
    public Account findByUserName(String username ) {
        String sql="select * from account where name = ?";
        try {
         return     queryRunner.query(connectionUtils.getThreadConnection(),sql,new BeanHandler<>(Account.class),username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void Update(Account account) {
     String sql =" update account set money = ? where id =?";
     Object [] params= {account.getMoney(),account.getId()};
        try {
            queryRunner.update(connectionUtils.getThreadConnection(),sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

2.AccountServiceImpl

package com.william.service.Impl;

import com.william.Dao.AccountDao;
import com.william.domain.Account;
import com.william.service.AccountService;
import com.william.utils.TransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author :lijunxuan
 * @date :Created in 2019/5/27  16:14
 * @description :
 * @version: 1.0
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    AccountDao accountDao;
    @Override
    public void transfer(String FromMoney, String ToMoney, Float money) {

            //开启事务:transactionManager


            //查询账户名称 两个账户名称
            Account FromuserName = accountDao.findByUserName(FromMoney);
            Account ToUserName = accountDao.findByUserName(ToMoney);
            //修改金额
            FromuserName.setMoney(FromuserName.getMoney()-money);
            ToUserName.setMoney(ToUserName.getMoney()+money);
           //int i=1/0;
            //修改账户表
            accountDao.Update(FromuserName);
            accountDao.Update(ToUserName);

    }
}

3.Utils工具类

(1)ConnectionUtils

package com.william.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author :lijunxuan
 * @date :Created in 2019/5/27  16:50
 * @description :
 * @version: 1.0
 */
@Component
public class ConnectionUtils {
    private ThreadLocal t1=new ThreadLocal();
    @Autowired
    DataSource dataSource;
    /**
     * 用来获取线程中连接对象
     * 1, 在第一次访问该方法时, 线程中没有对象,先获取一个,放入线程
     */
    public Connection getThreadConnection(){
        //获取线程池中的对象
        Connection conn = t1.get();
        if (conn==null){
            try {
                 conn = dataSource.getConnection();
                 t1.set(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return conn;
    }

    /**
     * 移除线程
     */
    public void remove()
    {
        t1.remove();
    }
}

(2)TransactionManager

package com.william.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

/**
 * @author :lijunxuan
 * @date :Created in 2019/5/27  16:50
 * @description :
 * @version: 1.0
 */
@Component
public class TransactionManager {
    @Autowired
    ConnectionUtils connectionUtils;
    //开启事务
    public void beganTransaction(){
        try {
            connectionUtils.getThreadConnection().setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //提交事务
    public void Commit(){
        try {
            connectionUtils.getThreadConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //回滚
    public void rollBack(){
        try {
            connectionUtils.getThreadConnection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //释放资源
    public void release(){
        try {
            connectionUtils.getThreadConnection().setAutoCommit(true);
            connectionUtils.getThreadConnection().close();
            connectionUtils.remove();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

4.TestTrasfer测试类

package com.william;

import com.william.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @author :lijunxuan
 * @date :Created in 2019/5/27  16:22
 * @description :
 * @version: 1.0
 */
@Component
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestTrasfer {
    @Autowired
    AccountService accountService;
    @Test
    public void testTrasferMoney(){

        accountService.transfer("william","william-Li",500f);
    }
}

你可能感兴趣的:(Spring框架)