编程式事务管理

  通过使用将Spring框架提供的TransactionTemplate模板注入到业务层来进行事务管理,这样对业务层原来的代码修改过多。不利于项目的后期维护。

以下是声明式事务管理的具体代码实现:

环境搭建:http://www.cnblogs.com/kuoAT/p/7803193.html

Dao层

package com.sdf.spring;

/**
* @author AT
* 转账dao层
*/
public interface AccountDao {
/**
* 转出钱
* @param outer
* @param money
*/
public void remove(String outer,Double money);
/**
* 转入钱
* @param input
* @param money
*/
public void add(String input,Double money);
}

dao层实现类

package com.sdf.spring;

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

/**
* 转账dao层实现
*/
public class AccountDaoimpl extends JdbcDaoSupport implements AccountDao{
/**
* 转出钱
* @param outer
* @param money
*/
@Override
public void remove(String outer, Double money) {
String sql = "update account set money = money - ? where name = ?";
this.getJdbcTemplate().update(sql, money,outer);
}
/**
* 转入钱
* @param input
* @param money
*/
@Override
public void add(String input, Double money) {
String sql = "update account set money = money + ? where name = ?";
this.getJdbcTemplate().update(sql, money,input);
} }

service业务层

package com.sdf.spring;

/**
* @author AT
* 转账业务接口
*/
public interface AccountSevice {
public void transfer(String input,String out,Double money);//消费
}

service业务层实现类

/**
* @author AT
* 编程式事务管理
*/
public class AccountServiceImpl implements AccountSevice { @Resource(name="accountDao")
private AccountDao accountDao;
//在业务类中注入事务模板
@Resource(name="transactionTemplate")
private TransactionTemplate transactionTemplate;
@Override
public void transfer(final String input, final String out,final Double money) {
// accountDao.remove(out, money);
// int a = 1/0;
// accountDao.add(input, money);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
accountDao.add(input, money);
int a = 1/0;//模拟转账过程中发生故障
accountDao.remove(out, money);
}
});
} public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
} }

applicationContext.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"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.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.xsd"> <!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0连接池 -->
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- dao层和业务的类 -->
<bean id="accountDao" class="com.sdf.spring.dao.impl.AccountDaoimpl">
<property name="dataSource" ref="datasource"></property>
</bean>
<bean id="accountService" class="com.sdf.spring.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="transactionTemplate" ref="transactionTemplate"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean> <!-- 定义事务管理模板:Spring为了简化事务管理的代码提供的类 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"/>
</bean>
</beans>

测试类

/**
* @author AT
* 测试转账信息 编程式事务管理
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AccountTest {
@Resource(name="accountService")
private AccountSevice accountService;
@Test
public void test01(){
accountService.transfer("A", "B", 100d);
}
public void setAccountService(AccountSevice accountService) {
this.accountService = accountService;
}
}

最新文章

  1. Maven pom.xml 元素配置说明(一)
  2. [WinForm] VS2010发布、打包安装程序
  3. CSS的样式表基本概念
  4. 数据契约(DataContract)及序列化指定输出字段
  5. 优化Laravel网站打开速度
  6. Oracle Goldengate工作原理
  7. Delphi-Delete 过程
  8. Phoenix中Sequence的用法
  9. Linux less命令
  10. Linux Expect自动化交互脚本简介
  11. Linux下防火墙配置
  12. bilinear pooling
  13. docker 5 docker-阿里云加速配置
  14. Spring错误——Spring xml注释——org.xml.sax.SAXParseException; lineNumber: 24; columnNumber: 10; cvc-complex-type.2.3: 元素 &#39;beans&#39; 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”。
  15. MyBatis 源码分析 - 配置文件解析过程
  16. HSQL可视化工具
  17. html 音乐 QQ播放器 外链 代码 播放器 外链 代码
  18. window上安装 MongoDB 及其 PHP扩展
  19. Mysql 5.7 忘记root密码或重置密码的详细方法
  20. PTA (Advanced Level) 1024 Palindromic Number

热门文章

  1. 网络分类及OSI七层模型
  2. 使用phpstudy搭建的外网网站 运行很慢 解决办法
  3. 0005SpringBoot中用Junit测试实体类中绑定yml中的值
  4. 0001SpringBoot整合Mybatis
  5. mysql中对表操作----为所有列插入数据
  6. Postman----登录接口返回的reponse中token值传递给其他接口的一个简单接口测试示例
  7. 打包完的rcp产品svn不储存密码问题
  8. mysql json数据类型
  9. GET 和 POST is so different
  10. [Functional Programming] Using ComposeK for both get State and modify State