码迷,mamicode.com
首页 > 编程语言 > 详细

Spring 事物 ( 已转账为例 ) 基于 AOP 注解

时间:2017-05-06 17:38:50      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:2.x   回滚   schema   drive   required   localhost   cte   double   nbsp   

<一> 配置为文件

<?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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
>

    <!-- 配置 C3P0 连接池数据源, 作为 DAO 层的数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl">
            <value>jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</value>
        </property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="user" value="root"></property>
        <property name="password" value="920619"></property>
    </bean>
    
    <!-- 配置 DAO 层, 把 C3P0 连接池作为数据源注入到 DAO 层 -->
    <bean id="accountDao" class="spring.things_2.AccountDao">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 配置 SERVICE 层, 把 DAO 层注入到 SERVICE 层 -->
    <bean id="accountService" class="spring.things_2.AccountService">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    
    <!-- 配置事物管理类 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 开启注解事物 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    
</beans>

 

<二> Dao 层和 Service 层的接口

package spring.things_2;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDao extends JdbcDaoSupport implements IAccountDao {
    
    @Override
    public void outMoney(String outName, double money) {
        String sql = "update User set money = money - ? where UserName = ?";
        this.getJdbcTemplate().update(sql, money, outName);
    }

    @Override
    public void inMoney(String inName, double money) {
        String sql = "update User set money = money + ? where UserName = ?";
        this.getJdbcTemplate().update(sql, money, inName);
    }
    
}
package spring.things_2;

import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 注解该类为事物类( 参数类型如下 )
 * propagation:   事物的传播行为
 * isolation:     事物的隔离级别
 * readOnly:      是否只读
 * rollbackFor:   被回滚的异常类型
 * noRollbackFor: 不会滚的异常类型
 */
@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.DEFAULT)
public class AccountService implements IAccountService {
    
    // 注入的 Dao 层
    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    
    // 这个方法在测试中是一个事物单元
    @Override
    public void transfer(String outName, String inName, double money) {
        accountDao.outMoney(outName, money);
        accountDao.inMoney(inName, money);
    }

}

< 三 > 接口的实现类

package spring.things_2;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDao extends JdbcDaoSupport implements IAccountDao {
    
    @Override
    public void outMoney(String outName, double money) {
        String sql = "update User set money = money - ? where UserName = ?";
        this.getJdbcTemplate().update(sql, money, outName);
    }

    @Override
    public void inMoney(String inName, double money) {
        String sql = "update User set money = money + ? where UserName = ?";
        this.getJdbcTemplate().update(sql, money, inName);
    }
    
}
package spring.things_2;

import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 注解该类为事物类( 参数类型如下 )
 * propagation:   事物的传播行为
 * isolation:     事物的隔离级别
 * readOnly:      是否只读
 * rollbackFor:   被回滚的异常类型
 * noRollbackFor: 不会滚的异常类型
 */
@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.DEFAULT)
public class AccountService implements IAccountService {
    
    // 注入的 Dao 层
    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    
    // 这个方法在测试中是一个事物单元
    @Override
    public void transfer(String outName, String inName, double money) {
        accountDao.outMoney(outName, money);
        accountDao.inMoney(inName, money);
    }

}

 

Spring 事物 ( 已转账为例 ) 基于 AOP 注解

标签:2.x   回滚   schema   drive   required   localhost   cte   double   nbsp   

原文地址:http://www.cnblogs.com/lovling/p/6817282.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!