第一种方式:使用TransactionTemplate模板

第一种方式:使用TransactionTemplate模板

步骤

  1. 配置事务核心管理器
<!-- 配置事务核心管理器,封装了所有的事务操作,依赖于连接池 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>
  1. 配置使用TransactionTemplate模板
<!-- 配置事务模板对象 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManager"></property>
</bean>
  1. 然后在service方法中配置transactionTemplate模板且使用execute方法
<!-- 配置service对象 -->
<bean id="accountServiceImpl" class="com.service.impl.AccountServiceImpl">
    <property name="ac" ref="accountDaoImpl"></property>
    <property name="tt" ref="transactionTemplate"></property>
</bean>

service方法

  • 使用execute方法,传入TransactionCallbackWithoutResult对象
  • execute的底层执行流程
    1. 先打开事务
    2. 调用doInTransactionWithoutResult方法
    3. 提交事务
    4. 有try/catch执行,有异常回滚
public class AccountServiceImpl implements IAccountService{

    private AccountDaoImpl ac;
    private TransactionTemplate tt;

    @Override
    public void transfer(final Integer from,final Integer to,final Double money) {

        /**
         * execute的底层执行流程
         * 1. 先打开事务
         * 2. 调用doInTransactionWithoutResult方法
         * 3. 提交事务
         * 4. 有try/catch执行,有异常回滚
         */
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus arg0) {
                //加钱
                ac.addMoney(to, money);
//              int i=1/0;
                //减钱
                ac.decreseMoney(from, money);
            }
        });

    }
    public void setAc(AccountDaoImpl ac) {
        this.ac = ac;
    }

    public void setTt(TransactionTemplate tt) {
        this.tt = tt;
    }
}

总配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd ">

    <!-- 加载jdbc.properties文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id="accountServiceImpl" class="com.service.impl.AccountServiceImpl">
        <property name="ac" ref="accountDaoImpl"></property>
        <property name="tt" ref="transactionTemplate"></property>
    </bean>

    <bean id="accountDaoImpl" class="com.dao.impl.AccountDaoImpl">
        <property name="jt" ref="jdbcTemplate"></property>
    </bean>

    <!-- 配置模板对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置事务核心管理器,封装了所有的事务操作,依赖于连接池 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置事务模板对象 -->
    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager"></property>
    </bean>

    <!-- 配置连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
</beans>

dao类

public class AccountDaoImpl implements IAccountDao{

    private JdbcTemplate jt;

    @Override
    public void addMoney(Integer id, Double money) {
        String sql= "update t_user set t_money = t_money + ? where t_id = ?";
        jt.update(sql,money,id);
    }

    @Override
    public void decreseMoney(Integer id, Double money) {
        String sql= "update t_user set t_money = t_money - ? where t_id = ?";
        jt.update(sql,money,id);
    }

    public void setJt(JdbcTemplate jt) {
        this.jt = jt;
    }
}

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDemo {

    @Resource(name="accountServiceImpl")
    private IAccountService as;

    @Test
    public void test1(){
        as.transfer(1, 2, 100d);
    }
}

猜你喜欢

转载自blog.csdn.net/Kato_op/article/details/80247821