Spring如何基于xml实现声明式事务控制

一、pom.xml



  4.0.0

  org.example
  A02spring
  1.0-SNAPSHOT

  
    
    
      org.springframework
      spring-context
      5.2.8.RELEASE
    
    
    
      org.springframework
      spring-jdbc
      5.2.8.RELEASE
    
    
    
      org.springframework
      spring-tx
      5.2.8.RELEASE
    
    
    
      org.aspectj
      aspectjweaver
      1.9.6
    
    
    
      mysql
      mysql-connector-java
      8.0.11
    
    
    
      org.projectlombok
      lombok
      1.18.12
      provided
    
    
    
      org.springframework
      spring-test
      5.2.8.RELEASE
      test
    
    
    
      junit
      junit
      4.13
      test
    
  

  
    
      
        org.apache.maven.plugins
        maven-compiler-plugin
        
          1.8
          1.8
        
      
    
  

二、spring的xml配置文件




  
    
  

  
    
  

  
    
    
    
    
  


  
  
    
  
  
  
    
    
      
      
    
  
  
    
    
    
    
  

三、实体类

package com.wuxi.beans;

import lombok.Data;

import java.io.Serializable;

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

四、dao

1、接口

package com.wuxi.daos;
import com.wuxi.beans.Account;
public interface AccountDao {
  Account findAccountById(Integer accountId);
  Account findAccountByName(String accountName);
  void updateAccount(Account account);
}

2、实现类

package com.wuxi.daos.impl;

import com.wuxi.beans.Account;
import com.wuxi.daos.AccountDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
  @Override
  public Account findAccountById(Integer accountId) {
    List accounts = getJdbcTemplate().query("select * from account where id = ?", new BeanPropertyRowMapper(Account.class), accountId);
    return accounts.isEmpty() ? null : accounts.get(0);
  }

  @Override
  public Account findAccountByName(String accountName) {
    List accounts = getJdbcTemplate().query("select * from account where name = ?", new BeanPropertyRowMapper(Account.class), accountName);
    if (accounts.isEmpty()) {
      return null;
    }
    if (accounts.size() > 1) {
      throw new RuntimeException("结果集不唯一");
    }
    return accounts.get(0);
  }

  @Override
  public void updateAccount(Account account) {
    getJdbcTemplate().update("update account set name=?,money=? where id=?", account.getName(), account.getMoney(), account.getId());
  }
}

五、service

1、接口

package com.wuxi.services;
import com.wuxi.beans.Account;
public interface AccountService {
  Account findAccounById(Integer accountId);
  void transfer(String sourceName, String targetName, Float money);
}

2、实现类

package com.wuxi.services.impl;

import com.wuxi.beans.Account;
import com.wuxi.daos.AccountDao;
import com.wuxi.services.AccountService;

public class AccountServiceImpl implements AccountService {
  private AccountDao accountDao;

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

  @Override
  public Account findAccounById(Integer accountId) {
    return accountDao.findAccountById(accountId);
  }

  @Override
  public void transfer(String sourceName, String targetName, Float money) {
    Account source = accountDao.findAccountByName(sourceName);
    Account target = accountDao.findAccountByName(targetName);

    source.setMoney(source.getMoney() - money);
    target.setMoney(target.getMoney() + money);

    accountDao.updateAccount(source);
    int i = 1 / 0;
    accountDao.updateAccount(target);
  }
}

六、测试

package com.wuxi.tests;

import com.wuxi.services.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application.xml")
public class MySpringTest {

  @Autowired
  private AccountService as;

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Spring如何基于xml实现声明式事务控制)