Spring 事务管理

AccountDaoImpl.java

public class AccountDaoImpl implements AccountDao{
    public JdbcTemplate temp;
    public void setTemp(JdbcTemplate temp) {
        this.temp = temp;
    }

    @Override//转入账号
    public void increaseMoney(Integer id, Integer money) {
        String sql = "update test set money = money + ? where id = ?";
        temp.update(sql,money,id);
    }
    @Override//转出账号
    public void decreaseMoney(Integer id, Integer money) {
        String sql = "update test set money = money - ? where id = ?";
        temp.update(sql,money,id);
    }
}

AccountServiceImpl.java

public class AccountServiceImpl implements AccountService {
    public AccountDao dao;
    //事务管理类
    public TransactionTemplate trans;

    public void setDao(AccountDao dao) {
        this.dao = dao;
    }
    public void setTrans(TransactionTemplate trans) {
        this.trans = trans;
    }

    @Override
    public void transfer(Integer from, Integer to, Integer money) {
        //调用事务管理对象的execute
        trans.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                dao.decreaseMoney(from,money);
                dao.increaseMoney(to,money);
            }
        });
    }
}

事务管理.xml

<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--引入properties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--注册数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.Driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--注册JDBCTemplate-->
    <bean id="temp" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--注册事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager"/>
    </bean>

    <!--注册Dao-->
    <bean id="accountDao" class="banana.事务管理.dao.AccountDaoImpl">
        <property name="temp" ref="temp"/>
    </bean>

    <!--注册service-->
    <bean id="service" class="banana.事务管理.service.AccountServiceImpl">
        <property name="dao" ref="accountDao"/>
        <property name="trans" ref="transactionTemplate"/>
    </bean>
</beans>

test.java 测试类

public class test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("事务管理.xml");
        AccountService service = applicationContext.getBean("service", AccountServiceImpl.class);
        service.transfer(1,3,1000);
        System.out.println("转账完成");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值