spring事务管理之转账案例
2018-01-08 20:47
741 查看
AccountDao类: public interface AccountDao { //加钱 void increaseMoney(Integer id,Double money); //减钱 void decreaseMoney(Integer id,Double money); }
AccountDaoImpl实现类: public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{ @Override public void increaseMoney(Integer id, Double money) { getJdbcTemplate().update("update t_account set money=money+? where id=?",money,id); } @Override public void decreaseMoney(Integer id, Double money) { getJdbcTemplate().update("update t_account set money=money-? where id=?",money,id); } }
AccountService类: public interface AccountService { //转账方法 void transfer(Integer from,Integer to,Double money); }
AccountServiceImpl实现类: public class AccountServiceImpl implements AccountService{ private AccountDao ad; public AccountDao getAd() { return ad; } public void setAd(AccountDao ad) { this.ad = ad; } @Override public void transfer(Integer from, Integer to, Double money) { //减钱 ad.decreaseMoney(from, money); //加钱 ad.increaseMoney(to, money); } }
applicatContext2.xml配置文件: <?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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "> <!-- 指定spring读取db.properties配置 --> <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <!-- 事务核心管理器,封装了所有事务操作,依赖于连接池 --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 一方法为单位指定方法应用什么事务属性 isolation:隔离级别 propagation:传播行为 read-only:是否只读--> <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"></tx:method> <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"></tx:method> <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"></tx:method> <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"></tx:method> <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"></tx:method> <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"></tx:method> <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"></tx:method> <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"></tx:method> <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"></tx:method> </tx:attributes> </tx:advice> <!-- 配置植入,将通知植入目标对象 --> <aop:config> <aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="txPc"></aop:pointcut> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/> </aop:config> <!-- 1.将连接池放入spring容器 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="user" value="root"/> <property name="password" value=""/> </bean> <!-- 2.将JDBCTemplate放入spring容器 --> <!-- <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> --> <!-- 3.将UserDao放入spring容器 --> <bean name="userDao" class="cn.itcast.a_jdbctemplate.UserDaoImpl"> <!-- <property name="jt" ref="jdbcTemplate"></property> --> <property name="dataSource" ref="dataSource"></property> </bean> <bean name="accountDao" class="cn.itcase.dao.AccountDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean> <!-- service --> <bean name="accountService" class="cn.itcast.service.AccountServiceImpl"> <property name="ad" ref="accountDao"/> </bean> </beans>
测试类: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext2.xml") public class Demo { @Resource(name="accountService") private AccountService as; @Test public void fun1(){ as.transfer(1, 2, 100d); } }
注解配置aop事务:
applicationContext.xml配置文件中开启注解管理事务:
<!-- 开启使用注解管理事务aop -->
<tx:annotation-driven/>
使用注解:(service方法中注解打开事务)
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public void transfer(Integer from, Integer to, Double money) {
//减钱
ad.decreaseMoney(from, money);
int i=1/0;
//加钱
ad.increaseMoney(to, money);
}
相关文章推荐
- 27Spring_的事务管理_银行转账业务加上事务控制_基于tx.aop进行声明式事务管理
- spring事务转账案例
- Spring通过事务实现模拟银行转账案例
- Spring框架的事务管理完成转账的案例
- 【JavaWeb-25】事务管理相关知识、手动/半自动/自动管理事务案例、整合Junit、整合Web、spring和hibernate整合、struts2和spring整合
- Spring编程式事务管理案例
- Spring+Mybatis+MySql+Maven 简单的事务管理案例
- spring事务学习(转账案例)(一)
- Spring编程式事务管理和声明式事务管理 案例
- spring事务学习(转账案例)(二)
- Spring中事务管理的四种方法(银行转账为例)
- Spring+Mybatis+MySql+Maven 简单的事务管理案例
- Spring+Mybatis+MySql+Maven 简单的事务管理案例
- Spring 事务管理案例
- 12-spring的事务管理(转账环境搭建一)
- 28Spring_的事务管理_银行转账业务加上事务控制_基于注解进行声明式事务管理
- spring事务学习(转账案例)(二)
- 通过案例掌握Spring 管理事务的步骤及配置
- Spring+Mybatis+MySql+Maven 简单的事务管理案例
- Spring 事务操作(银行转账案例),使用spring 对jdbc的支持完成对数据库的操作