• 基于注解的声明式事务控制


    1、maven依赖

    <?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>

    2、abc服务和实现类

    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;
        }
    }

    3、icbc服务和实现类

    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);
        }
    }

    4、abc dao和实现类

    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);
        }
    }

    5、icbc dao和实现类

    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);
        }
    }

    6、转账服务和实现类

    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;
        }
    }

    7、jdbc资源文件

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

    8、spring配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
        <!--配置jdbc资源文件-->
        <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
        <!--配置注解扫描的包-->
        <context:component-scan base-package="com.ly.spring"></context:component-scan>
        <!--配置数据源-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.driver}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="username" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
        </bean>
        <!--配置事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!--spring开启支持注解的方式控制事务-->
        <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    </beans>

    9、测试类

    package com.ly.spring.test;
    
    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(locations = "classpath:bean.xml")
    public class MainTest {
        @Autowired
        private ITransferAccountService transferAccountService;
        @Test
        public void test() {
            transferAccountService.transferAccountFromIcbcToAbc("1234567890","1001",500);
        }
    }
  • 相关阅读:
    冲刺NO.2
    冲刺NO.1
    用户场景描述
    【洛谷T2695 桶哥的问题——吃桶】
    【洛谷P4445 【AHOI2018初中组】报名签到】
    清北学堂2019.5.4
    清北学堂2019.5.3
    清北学堂2019.5.2
    清北学堂培训2019.5.1
    清北学堂培训2019.4.30
  • 原文地址:https://www.cnblogs.com/liuyang-520/p/12349971.html
Copyright © 2020-2023  润新知