基於純註解的聲明式事務控制

一、maven依賴java

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ly.spring</groupId>
    <artifactId>spring07</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <!--JdbcTemplate-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <!--整合junit-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <!--解析切入點表達式-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--解決IDEA maven變動後自動重置LanguageLevel和JavaCompiler版本的問題-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>13</source>
                    <target>13</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

二、abc服務和實現類mysql

package com.ly.spring.service;

public interface IAbcService {
    public void updateAbcMoney(String abcBankNo,double addMoney);
}
package com.ly.spring.service.impl;

import com.ly.spring.dao.IAbcDao;
import com.ly.spring.service.IAbcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("abcService")
public class AbcServiceImpl implements IAbcService {
    @Autowired
    private IAbcDao abcDao;

    @Override
    public void updateAbcMoney(String abcBankNo, double addMoney) {
        abcDao.updateAbcMoney(abcBankNo,addMoney);
        //int i = 1/0;
    }
}

三、icbc服務和實現類spring

package com.ly.spring.service;

public interface IIcbcService {
    public void updateIcbcMoney(String icbcBankNo, double addMoney);
}
package com.ly.spring.service.impl;

import com.ly.spring.dao.IIcbcDao;
import com.ly.spring.service.IIcbcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("icbcService")
public class IcbcServiceImpl implements IIcbcService {
    @Autowired
    private IIcbcDao icbcDao;

    @Override
    public void updateIcbcMoney(String icbcBankNo, double addMoney) {
        icbcDao.updateIcbcMoney(icbcBankNo,addMoney);
    }
}

四、abc dao和實現類sql

package com.ly.spring.dao;

public interface IAbcDao {
    public void updateAbcMoney(String abcBankNo,double addMoney);
}
package com.ly.spring.dao.impl;

import com.ly.spring.dao.IAbcDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;

@Repository("abcDao")
public class AbcDaoImpl extends JdbcDaoSupport implements IAbcDao {
    //給JdbcDaoSupport注入數據源
    @Autowired
    public AbcDaoImpl(DataSource dataSource) {
        super.setDataSource(dataSource);
    }
    @Override
    public void updateAbcMoney(String abcBankNo, double addMoney) {
        super.getJdbcTemplate().update("update abc set money = money+? where bankno = ?",addMoney,abcBankNo);
    }
}

五、icbc dao和實現類apache

package com.ly.spring.dao;

public interface IIcbcDao {
    public void updateIcbcMoney(String icbcBankNo, double addMoney);
}
package com.ly.spring.dao.impl;

import com.ly.spring.dao.IIcbcDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;

@Repository("icbcDao")
public class IcbcDaoImpl extends JdbcDaoSupport implements IIcbcDao {
    //給JdbcDaoSupport注入數據源
    @Autowired
    public IcbcDaoImpl(DataSource dataSource) {
        super.setDataSource(dataSource);
    }
    @Override
    public void updateIcbcMoney(String icbcBankNo, double addMoney) {
        super.getJdbcTemplate().update("update icbc set money = money+? where bankno = ?",addMoney,icbcBankNo);
    }
}

六、轉帳服務和實現類maven

package com.ly.spring.service;

/**
 * 轉帳服務
 */
public interface ITransferAccountService {
    public void transferAccountFromIcbcToAbc(String icbcBankNo,String abcBankNo,double money);
}
package com.ly.spring.service.impl;

import com.ly.spring.service.IAbcService;
import com.ly.spring.service.IIcbcService;
import com.ly.spring.service.ITransferAccountService;
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("transferAccountService")
//聲明事務
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class TransferAccountServiceImpl implements ITransferAccountService {
    @Autowired
    private IIcbcService icbcService;
    @Autowired
    private IAbcService abcService;

    //覆蓋在類上聲明的事務
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @Override
    public void transferAccountFromIcbcToAbc(String icbcBankNo, String abcBankNo, double money) {
        //先扣減icbc
        icbcService.updateIcbcMoney(icbcBankNo,0-money);
        //再增長abc
        abcService.updateAbcMoney(abcBankNo,money);
        int i = 1/0;
    }
}

七、jdbc資源文件ide

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/db01
jdbc.user = root
jdbc.password = root

八、spring主配置類測試

package com.ly.spring.config;

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

//指定此類爲配置類
@Configuration
//配置註解掃描包
@ComponentScan("com.ly.spring")
//jdbc資源文件
@PropertySource("classpath:jdbc.properties")
//引入其它配置類
@Import({JdbcConfig.class,TransactionConfig.class})
//開啓註解事務
@EnableTransactionManagement
public class SpringConfiguration {
}

九、jdbc配置類ui

package com.ly.spring.config;

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

import javax.sql.DataSource;

@Configuration
public class JdbcConfig {
    //讀取jdbc資源文件
    @Value(value = "${jdbc.driver}")
    private String jdbcDriver;
    @Value(value = "${jdbc.url}")
    private String jdbcUrl;
    @Value(value = "${jdbc.user}")
    private String username;
    @Value(value = "${jdbc.password}")
    private String password;

    //建立數據源
    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(jdbcDriver);
        dataSource.setUrl(jdbcUrl);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}

十、事務配置類url

package com.ly.spring.config;

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

import javax.sql.DataSource;

@Configuration
public class TransactionConfig {
    //建立事務管理器
    @Bean(name = "transactionManager")
    public PlatformTransactionManager getTransactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

十一、測試類

package com.ly.spring.test;

import com.ly.spring.config.SpringConfiguration;
import com.ly.spring.service.ITransferAccountService;
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;
//替換junit的main方法
@RunWith(SpringJUnit4ClassRunner.class)
//指定spring配置類
@ContextConfiguration(classes = SpringConfiguration.class)
public class MainTest {
    @Autowired
    private ITransferAccountService transferAccountService;
    @Test
    public void test() {
        transferAccountService.transferAccountFromIcbcToAbc("1234567890","1001",500);
    }
}
相關文章
相關標籤/搜索