Spring_day04_实例1:基于XML的声明式事务控制

项目组织结构如下:

Spring_day04_实例1:基于XML的声明式事务控制_第1张图片

一、pom依赖:

pom.xml



    4.0.0

    com.zl
    spring_day04_transaction_xml
    1.0-SNAPSHOT
    jar

    
        
            org.springframework
            spring-context
            5.0.2.RELEASE
        

        
            mysql
            mysql-connector-java
            5.1.6
        

        
            org.springframework
            spring-jdbc
            5.0.2.RELEASE
        

        
            org.springframework
            spring-tx
            5.0.2.RELEASE
        

        
            junit
            junit
            4.12
        

        
            org.aspectj
            aspectjweaver
            1.8.13
        
    

二、实体类Account:

package com.zl.domain;

import java.io.Serializable;

public class Account implements Serializable {
    private Integer id;
    private String name;
    private Float money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getMoney() {
        return money;
    }

    public void setMoney(Float money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

三、Service层:

com.zl.service.AccountService接口:

package com.zl.service;

public interface AccountService {

    /**
     * 转账
     * @param sourceName    转出账户名称
     * @param targetName    转入账户名称
     * @param money         转账金额
     */
    void transfer(String sourceName,String targetName,Float money);
}

com.zl.service.AccountServiceImpl实现类:

package com.zl.service.impl;

import com.zl.dao.AccountDao;
import com.zl.domain.Account;
import com.zl.service.AccountService;

public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

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

    public void transfer(String sourceName, String targetName, Float money) {
        //1.根据名称查询两个账户名称
        Account source = accountDao.findAccountByName(sourceName);
        Account target = accountDao.findAccountByName(targetName);
        //修改两个账户的金额
        source.setMoney(source.getMoney()-money);
        target.setMoney(target.getMoney()+money);
        //3.更新两个账户
        accountDao.updateAccount(source);
        int i = 1/0;
        accountDao.updateAccount(target);
    }
}

四、Dao层:

`com.zl.Dao接口:

package com.zl.dao;

import com.zl.domain.Account;

public interface AccountDao {

    /**
     * 根据账户名称查询账户
     * @param name
     * @return
     */
    Account findAccountByName(String name);

    /**
     * 更新账户
     * @param account
     */
    void updateAccount(Account account);
}

com.zl.dao.impl.AccountDaoImpl实现类:

package com.zl.dao.impl;

import com.zl.dao.AccountDao;
import com.zl.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class AccountDaoImpl implements AccountDao {

    private JdbcTemplate jt;

    public void setJt(JdbcTemplate jt) {
        this.jt = jt;
    }

    public Account findAccountByName(String name) {
        List accounts = jt.query("select * from account where name=?", new BeanPropertyRowMapper(Account.class), name);
        if(accounts.isEmpty()){
            return null;
        }else if(accounts.size()>1){
            throw new RuntimeException("根据名称查询得到的结果不唯一...");
        }
        return accounts.get(0);
    }

    public void updateAccount(Account account) {
        jt.update("update account set money=? where id=?",account.getMoney(),account.getId());
    }
}

四、AOP主配置文件:

resources.bean.xml




    
    
        
    

    
    
        
    

    
    
        
    

    
    
        
        
        
        
    

    
    
        
    

    
    
        
            
        
    

    
    
        
        
    


需要注意的是:当我们使用环绕通知时,其他四个通知无法使用,否则反之。

五、测试类:

import com.zl.service.AccountService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestTx {
    private ApplicationContext app;
    private AccountService accountService;
    @Before
    public void init(){
        app = new ClassPathXmlApplicationContext("bean.xml");
        accountService = app.getBean("accountService", AccountService.class);
    }


    @Test
    public void testTransfer(){
        accountService.transfer("aaa","bbb",100f);
    }
}

你可能感兴趣的:(Spring_day04_实例1:基于XML的声明式事务控制)