<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>day04_learn_08account_tx</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.45</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.7</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>
</project>
package com.learn.dao;import com.learn.domain.Account;/*** 账户的持久层接口*/
public interface IAccountDao {/*** 根据Id查询账户* @param accountId* @return*/Account findAccountById(Integer accountId);/*** 根据名称查询账户* @param accountName* @return*/Account findAccountByName(String accountName);/*** 更新账户* @param account*/void updateAccount(Account account);
}
package com.learn.dao.impl;import com.learn.dao.IAccountDao;
import com.learn.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;import java.util.List;/*** 账户的持久层实现类*/
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {public Account findAccountById(Integer accountId) {List<Account> accounts = super.getJdbcTemplate().query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);return accounts.isEmpty()?null:accounts.get(0);}public Account findAccountByName(String accountName) {List<Account> accounts = super.getJdbcTemplate().query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName);if(accounts.isEmpty()){return null;}if(accounts.size()>1){throw new RuntimeException("结果集不唯一");}return accounts.get(0);}public void updateAccount(Account account) {super.getJdbcTemplate().update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());}
}
package com.learn.domain;import java.io.Serializable;/*** 账户的实体类*/
public class Account implements Serializable {private Integer id;private String name;private Float money;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Float getMoney() {return money;}public void setMoney(Float money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + '\'' +", money=" + money +'}';}
}
package com.learn.service;import com.learn.domain.Account;/*** 账户的业务层接口*/
public interface IAccountService {/*** 根据id查询账户信息* @param accountId* @return*/Account findAccountById(Integer accountId);/*** 转账* @param sourceName    转成账户名称* @param targetName    转入账户名称* @param money         转账金额*/void transfer(String sourceName, String targetName, Float money);
}
package com.learn.service.impl;import com.learn.dao.IAccountDao;
import com.learn.domain.Account;
import com.learn.service.IAccountService;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;/*** 账户的业务层实现类** 事务控制应该都是在业务层*/
public class AccountServiceImpl implements IAccountService{private IAccountDao accountDao;private TransactionTemplate transactionTemplate;public void setTransactionTemplate(TransactionTemplate transactionTemplate) {this.transactionTemplate = transactionTemplate;}public void setAccountDao(IAccountDao accountDao) {this.accountDao = accountDao;}public Account findAccountById(final Integer accountId) {return  transactionTemplate.execute(new TransactionCallback<Account>() {public Account doInTransaction(TransactionStatus status) {return accountDao.findAccountById(accountId);}});}public void transfer(final String sourceName, final String targetName, final Float money) {transactionTemplate.execute(new TransactionCallback<Object>() {public Object doInTransaction(TransactionStatus status) {System.out.println("transfer....");//2.1根据名称查询转出账户Account source = accountDao.findAccountByName(sourceName);//2.2根据名称查询转入账户Account target = accountDao.findAccountByName(targetName);//2.3转出账户减钱source.setMoney(source.getMoney()-money);//2.4转入账户加钱target.setMoney(target.getMoney()+money);//2.5更新转出账户accountDao.updateAccount(source);//                int i=1/0;//2.6更新转入账户accountDao.updateAccount(target);return null;}});}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 配置业务层--><bean id="accountService" class="com.learn.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"></property><property name="transactionTemplate" ref="transactionTemplate"></property></bean><!-- 配置账户的持久层--><bean id="accountDao" class="com.learn.dao.impl.AccountDaoImpl"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置数据源--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/test"></property><property name="username" value="root"></property><property name="password" value="123456"></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>
</beans>
package com.learn.test;import com.learn.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** 使用Junit单元测试:测试我们的配置*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {@Autowiredprivate  IAccountService as;@Testpublic  void testTransfer(){as.transfer("aaa","bbb",100f);}}

spring编程式事务控制相关推荐

  1. 【spring】编程式事务控制

    结构: AccountServiceImpl package com.itheima.service.impl;import com.itheima.dao.IAccountDao; import c ...

  2. Spring→事务、隔离级别、事务传播行为、编程式事务控制、XML配置声明式事务(原始方式)、XML配置声明式事务(基于tx/aop)、@注解配置声明式事务、优势总结

    事务 Spring事务管理 不考虑隔离引发问题 隔离级别 事务传播行为 演示环境搭建 编程式事务控制 XML配置声明式事务(原始方式) XML配置声明式事务(基于tx/aop) @注解配置声明式事务 ...

  3. 编程式事务控制相关对象

    以下内容不需要记忆,了解即可 1.PlatformTransactionManager(平台事务管理器) PlatformTransactionManager接口是spring的事务管理器,它提供了我 ...

  4. spring编程式事务

    事务是spring框架中一个核心的模块,事务的ACID特性想必对于学习java的同学来说都不陌生,对于spring,实现事务的底层原理其实很简单,就是通过AOP代理进行实现,而实现spring的AOP ...

  5. Spring 编程式事务实例

    1.通过PlatformTransactionManager控制事务 package com.tx;import org.springframework.jdbc.core.JdbcTemplate; ...

  6. spring编程式和声明式事务控制

    可优先参考:@Transactional Spring 事务的深入学习与使用[两万字] 1. 编程式事务控制 @Autowired private RoleMapper roleMapper;@Aut ...

  7. Spring的编程式事务声明式事务 基于注解的声明式事务控制

    文章目录 Spring中编程式事务 基于XML的声明式事务控制 基于注解的声明式事务控制 Spring集成web环境 Spring中编程式事务 Spring的事务控制可以分为编程式事务控制和声明式事务 ...

  8. 全面分析 Spring 的编程式事务管理及声明式事务管理(转)

    摘要 Spring 的事务管理是 Spring 框架中一个比较重要的知识点,该知识点本身并不复杂,只是由于其比较灵活,导致初学者很难把握.本教程从基础知识开始,详细分析了 Spring 事务管理的使用 ...

  9. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

最新文章

  1. python学习07
  2. 1乘到100 python_python每日一练之如何计算你的应发奖金?
  3. 官方文档翻译-ESP32-High Resolution Timer
  4. 每日简单小妙招:使用python实现控制摄像头拍照并将其发送某某邮箱(仅供学习)
  5. MarshalByRefObject浅析
  6. 控制是否展示_现场展示板管理不在于看,而在于管!
  7. JDK和CGLIB动态代理区别
  8. SpringMVC 使用注解时控制器传参
  9. 3.15 Ext JS 之 Tooltip 的基本使用
  10. freebasic 编译linux,FreeBasic库文件入门 [SJ ZERO原创,沧海冷心翻译]
  11. 行业观察:拐点已至!抛弃传统数据库,乘云而上!
  12. Yii2 使用 faker 生成假数据(转)
  13. 梯度下降优化器小结(RMSProp,Momentum,Adam)
  14. c语言中负数参与除法,C语言中负数除法与右移取整问题
  15. EDEM 2020的安装
  16. Android 画笔演示功能的实现
  17. 中国游戏产业的多事之秋
  18. form 表单提交后,使页面不跳转
  19. 使用chrome控制台下载页面图片
  20. 求解马走棋问题(回溯法)

热门文章

  1. C语言 - 快速排序算法
  2. 使用ImageMagick 在图片上绘制粗斜体的中文也许是一个错误。
  3. Android开发系列(二十八):使用SubMenu创建选项菜单
  4. Codeforces Round #131 (Div. 2)------AB
  5. 新鲜出炉的电信诈骗经历
  6. MySQL5.7.19版本压缩包安装方式的一些坑
  7. Codeforces 140D - New Year Contest
  8. 数据仓库专题(6)-数据仓库、主题域、主题概念与定义
  9. 诺顿本月将发布儿童网络安全软件
  10. 哈佛大学 CS50,全美最受欢迎的计算机课程!