#纯注解小项目:JdbcTemplate、SpEL@Transactional@PropertySource@EnableTransactionManagement#spring-tx@FDDLC

测试环境:IDEA 2020.2、MySQL 8.0.16

 

项目结构

#纯注解小项目:JdbcTemplate、SpEL@Transactional@PropertySource@EnableTransactionManagement#spring-tx@FDDLC_第1张图片

 

pom.xml的内容:



    4.0.0

    org.example
    P040_TransferProblem_spring-tx_full_annotation
    1.0-SNAPSHOT

    
        
            org.springframework
            spring-context
            5.1.8.RELEASE
        

        
            mysql
            mysql-connector-java
            8.0.16
        

        
        
            com.mchange
            c3p0
            0.9.5.2
        

        
        
            junit
            junit
            4.12
            test
        

        
            org.springframework
            spring-test
            5.1.8.RELEASE
        

        
            org.springframework
            spring-tx
            
            5.1.8.RELEASE
        

        
            org.aspectj
            aspectjweaver
            1.8.10
        

        
            org.springframework
            spring-jdbc
            5.1.8.RELEASE
        
    

 

bean.xml的内容:无,纯注解!

 

MySQL数据库:

database:test        port:3306        table:account

#纯注解小项目:JdbcTemplate、SpEL@Transactional@PropertySource@EnableTransactionManagement#spring-tx@FDDLC_第2张图片

 

Account.java的内容:

package cn.liuxingchang.Domain;

import java.io.Serializable;

public class Account implements Serializable {
    private Integer id;
    private String name;
    private Double 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 Double getMoney() {
        return money;
    }

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

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

 

AccountDao.java的内容:

package cn.liuxingchang.Dao;

import cn.liuxingchang.Domain.Account;

public interface AccountDao {
    void updateAccount(Account account);
    Account findAccountByName(String name);
}

 

AccountDaoImpl.java的内容:

package cn.liuxingchang.Dao.impl;

import cn.liuxingchang.Dao.AccountDao;
import cn.liuxingchang.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 jdbcTemplate;

    public void updateAccount(Account account) {
        try {
            jdbcTemplate.update("update account set name = ?, money = ? where id = ?",
                    account.getName(), account.getMoney(), account.getId());
        } catch(Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Account findAccountByName(String name) {
        try {
            List accounts = jdbcTemplate.query("select * from account where name = ?",
                    new BeanPropertyRowMapper(Account.class), name);

            if(accounts == null || accounts.size() == 0) {
                throw new RuntimeException("找不到目标账户!");
            } else if(accounts.size() == 1) {
                return accounts.get(0);
            } else {
                throw new RuntimeException("账户不唯一!");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

 

AccountService.java的内容:

package cn.liuxingchang.service;

public interface AccountService {

    void transfer(String sourceName, String targetName, Double money);
}

 

AccountServiceImpl.java的内容:

package cn.liuxingchang.service.impl;

import cn.liuxingchang.Dao.AccountDao;
import cn.liuxingchang.Domain.Account;
import cn.liuxingchang.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(transactionManager = "transactionManager", propagation = Propagation.REQUIRED)
//@Transactional可作用于类和方法上,方法上的优先级高于类上的优先级
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;

    public void transfer(String sourceName, String targetName, Double money) {
        /**
         * 1、根据sourceName查找souce账户
         * 2、根据targetName查找target账户
         * 3、souce账户减钱
         * 4、target账户加钱
         * 5、更新souce账户
         * 6、更新target账户
         */

        //进行操作
        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);
    }
}

 

DataSourceConfig.java的内容:

package config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

@PropertySource("jdbc.properties")
public class DataSourceConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.user}")
    private String user;
    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource")
    public DataSource createDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(user);
        dataSource.setPassword(password);

        return dataSource;
    }
}

 

JdbctemplateConfig.java的内容:

package config;

import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

public class JdbcTemplateConfig {
    @Bean("jdbcTemplate")
    public JdbcTemplate createJdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

 

TransactionManagerConfig.java的内容:

package config;

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

import javax.sql.DataSource;

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

 

MainConfig.java的内容:

package config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@ComponentScan("cn.liuxingchang")
@EnableTransactionManagement //相当于标签
@Import({JdbcTemplateConfig.class, DataSourceConfig.class, TransactionManagerConfig.class})
public class MainConfig {
}

 

Test.java的内容:

import cn.liuxingchang.service.AccountService;
import config.MainConfig;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@SpringJUnitConfig(classes = MainConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class Test {
    //@Autowired
    @Resource(name = "accountService")
    private AccountService accountService;
    @org.junit.Test
    public void transferTest() {
        accountService.transfer("Zhao", "Qian", 1.0);
    }
}

 

你可能感兴趣的:(Spring教程)