Spring_day04_实例3:基于纯注解的声明式事务控制

本文是基于纯注解的声明式事务控制,也就是说,没有xml配置文件
项目组织结构如下:

Spring_day04_实例3:基于纯注解的声明式事务控制_第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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("accountService")
//只读型
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    //读写型
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    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.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {

    @Autowired
    private JdbcTemplate 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());
    }
}

四、配置类:

conf.MySpringConfiguration主配置类:

package conf;

import org.springframework.context.annotation.*;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@ComponentScan("com.zl")
@Import({JdbcConfig.class,TransactionConfig.class})
@EnableTransactionManagement
@PropertySource("jdbcConfig.properties")
public class MySpringConfiguration {
}

conf.JdbcConfig数据库配置类:

package conf;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

public class JdbcConfig {

    @Value("${jdbc.driver}")
    private String driver;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    @Bean("jt")
    public JdbcTemplate createJdbcTemplate(DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

    @Bean("dataSource")
    public DataSource createDatasource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}

conf.TransactionConfig事务配置类:

package conf;

import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

public class TransactionConfig {

    @Bean("transactionManager")
    public PlatformTransactionManager createTransactionManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }
}

五、数据库配置文件:

resources.jdbcConfig.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root

六、测试类:

写法一:

import com.zl.service.AccountService;
import conf.MySpringConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

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


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

写法二:

import com.zl.service.AccountService;
import conf.MySpringConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MySpringConfiguration.class)
public class TestTx2 {

    @Autowired
    private AccountService accountService;

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

需要注意的是,写法二的junit测试类,需要添加下面一个依赖:


      org.aspectj
      aspectjweaver
      1.8.13

你可能感兴趣的:(Spring_day04_实例3:基于纯注解的声明式事务控制)