Spring 事务 搭建转账环境以及事务管理手动编写事务管理

  1. 创建业务层Dao接口和实现
    package tx;
    
    public interface AccountDao {
    
        public void outMoney(String from,Double money);
        public void inMoney(String to,Double money);
    
    }
    
    package tx;
    
    public interface AccountService {
        public void  transfer(
                String from,String to,Double money
        );
    }
    
    package tx;
    
    import org.springframework.transaction.TransactionStatus;
    import org.springframework.transaction.support.TransactionCallbackWithoutResult;
    import org.springframework.transaction.support.TransactionTemplate;
    
    public class AccountServiceImplement implements AccountService{
        private AccountDao accountDao;
    
        public void setAccountDao(AccountDao accountDao) {
            this.accountDao = accountDao;
        }
    
        private TransactionTemplate transactionTemplate;
    
        public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
            this.transactionTemplate = transactionTemplate;
        }
    
        @Override
        public void transfer( final String from,final String to,final Double money) {
    
            transactionTemplate.execute(new TransactionCallbackWithoutResult() {
                @Override
                protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
    
                    accountDao.outMoney(from,money);
                    int d =1/0;
                    accountDao.inMoney(to,money);
                }
            });
    
    
    
        }
    }
    

    配置业务层:

    <bean id="accountService" class="tx.AccountServiceImplement">
            <property name="accountDao" ref="accountDao"/>
            <!--注入事务管理模板-->
    
            <property name="transactionTemplate" ref="transactionTemplate"/>
        </bean>
        <!--Dao-->
        <bean id="accountDao" class="tx.AccountDaoImplement">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
        <!--配置c3p0连接池-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${driverClass}"/>
            <property name="jdbcUrl" value="${jdbcUrl}"/>
            <property name="user" value="${user}"/>
            <property name="password" value="${password}"/>
        </bean>
    
        <!--<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <constructor-arg>
                <ref bean="dataSource" />
            </constructor-arg>
        </bean>-->
        <!--配置平台事务管理=======-->
        <bean id="transactionMsnger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    <!--配置事务管理的模板-->
        <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
            <constructor-arg>
                <ref bean="transactionMsnger"/>
            </constructor-arg>
        </bean>
    

    测试类:

    package tx;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import javax.annotation.Resource;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:tx.xml")
    public class SpringTest1 {
        @Resource(name = "accountService")
        private AccountService accountService;
        @Test
        public void test1(){
            accountService.transfer("王4一","王3一",1000d);
        }
    }
    

猜你喜欢

转载自blog.csdn.net/dagedeshu/article/details/87891375