spring 事务管理

1.导入相应的jar包

2. 编写配置文件:jdbc.properties

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/test
user=root
password=123456

3.编写业务类接口:AccountService  

public interface AccountService {
    public void transfer(String out,String in,Double money);
}

4.编写实现类:AccountServiceImpl

public class AccountServiceImpl implements AccountService{
    
    public AccountDao accountDao;
    
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transfer(String out, String in, Double money) {
        accountDao.outMoney(out, money);
        int i = 10/0;
        accountDao.inMoney(in, money);
    }
}

5.编写dao层接口:AccountDao

public interface AccountDao {
    public void outMoney(String out,Double money);
    public void inMoney(String in,Double money);
}

6.编写dao层实现类:AccountDaoImpl

public class AccountDaoImpl implements AccountDao{
    private JdbcTemplate jdbcTemplate;
    
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public void outMoney(String out, Double money) {
        String sql = "update db01 set money = money - ? where name=?";
        jdbcTemplate.update(sql, money,out);
    }

    @Override
    public void inMoney(String in, Double money) {
        String sql = "update db01 set money = money + ? where name=?";
        jdbcTemplate.update(sql, money,in);
    }

}

7.配置spring配置文件:bean.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:p="http://www.springframework.org/schema/p"
    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/context 
        http://www.springframework.org/schema/context/spring-context-4.3.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-4.3.xsd">
        
         <!-- 引入外部属性文件 -->
        <context:property-placeholder location="classpath:jdbc.properties"/>
    
        <!-- 配置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>
        
        <!-- 配置DAO层,注入连接池就可以得到jdbc模板-->
        <bean id="accountDao" class="com.xuzhiwen.service.AccountDaoImpl">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        
        <!-- 配置业务层 -->
        <bean id="accountService" class="com.xuzhiwen.service.AccountServiceImpl">
            <property name="accountDao" ref="accountDao"/>
        </bean>
        
        
        <!-- 配置SpringJdbc的事务管理器 -->
            <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
        </bean>
        
        <!-- 配置事务的增强 -->
            <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="transfer" propagation="REQUIRED"/>
        </tx:attributes>
        </tx:advice>
        
        <!-- 配置切面 -->
        <aop:config>
        <!-- 配置切点 -->
        <aop:pointcut expression="execution(* com.xuzhiwen.service.AccountServiceImpl.*(..))" id="pointcut1"/>
        <!-- 配置切面 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
        </aop:config>
        
</beans>

8.建库建表:

 9.编写测试类:Test

public class Test {

    @org.junit.Test
    public void test() {
        ApplicationContext app = new ClassPathXmlApplicationContext("bean.xml");
        AccountService test = (AccountService) app.getBean("accountService");
        test.transfer("xuzhiwen", "weibin", 200d);
    }
}

10.运行结果为:

要么都成功,要么都失败!

原文地址:https://www.cnblogs.com/beibidewomen/p/7219062.html