--------------------siwuxie095

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

Spring 编程式事务管理

 
 

 
 

以转账为例

 
 

 
 

1、在
MySQL 中手动创建数据库和表

 
 

数据库名:tx_db,表名:account,字段:id、name、money

 
 

 
 

 
 

手动添加数据,用作测试

 
 

 
 

 
 

 
 

 
 

2、具体步骤

 
 

(1)配置事务管理器

 
 

<!-- 配置事务管理器 -->

<bean
id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<!--

在 DataSourceTransactionManager 源代码中有

属性 dataSource 和其 set 方法,所以可以注入

-->

<property
name="dataSource"
ref="dataSource"/>

</bean>

 
 

 
 

 
 

(2)配置事务管理模板

 
 

<!-- 配置事务管理模板 -->

<bean
id="transactionTemplate"

class="org.springframework.transaction.support.TransactionTemplate">

<!--

在 TransactionTemplate 源代码中有属性 transactionTemplate

和其 set 方法,所以可以注入

-->

<property
name="transactionManager"
ref="transactionManager"/>

</bean>

 
 

 
 

 
 

(3)在业务层注入事务管理模板

 
 

<!-- 配置对象并注入属性 -->

<bean
id="accountService"
class="com.siwuxie095.service.AccountService">

<property
name="accountDao"
ref="accountDao"></property>

<!-- 在业务层注入注入事务管理模板 -->

<property
name="transactionTemplate"
ref="transactionTemplate"/>

</bean>

 
 

 
 

 
 

(4)在业务层手动编写代码实现事务管理

 
 

 
 

 
 

 
 

3、具体实现

 
 

(1)编写
Dao 类

 
 

AccountDao.java:

 
 

package com.siwuxie095.dao;

 
 

import org.springframework.jdbc.core.JdbcTemplate;

 
 

public class AccountDao {

 
 

private JdbcTemplate jdbcTemplate;

 

public
void setJdbcTemplate(JdbcTemplate jdbcTemplate) {

this.jdbcTemplate = jdbcTemplate;

}

 

 

/**

* 转出

*/

public
void lessMoney(String from, int money) {

String sql="update account set money=money-? where name=?";

jdbcTemplate.update(sql, money, from);

}

 

 

/**

* 转入

*/

public
void moreMoney(String to, int money) {

String sql="update account set money=money+? where name=?";

jdbcTemplate.update(sql, money, to);

}

 

}

 
 

 
 

 
 

(2)编写一个
Service 类

 
 

AccountService.java:

 
 

package com.siwuxie095.service;

 
 

import org.springframework.transaction.TransactionStatus;

import org.springframework.transaction.support.TransactionCallbackWithoutResult;

import org.springframework.transaction.support.TransactionTemplate;

 
 

import com.siwuxie095.dao.AccountDao;

 
 

public class AccountService {

 
 

private AccountDao accountDao;

private TransactionTemplate transactionTemplate;

 

public
void setAccountDao(AccountDao accountDao) {

this.accountDao = accountDao;

}

 

public
void setTransactionTemplate(TransactionTemplate transactionTemplate) {

this.transactionTemplate = transactionTemplate;

}

 

 

/**

* 转账

*/

public
void transfer(String from,String to,int money) {

 

 

// 在业务层手动编写代码实现事务管理

transactionTemplate.execute(new TransactionCallbackWithoutResult() {

 

@Override

protected
void doInTransactionWithoutResult(TransactionStatus status) {

 

accountDao.lessMoney(from, money);

 

// 即便中间出现了什么异常,也会进行回滚

// 如:int num=10/0;

 

accountDao.moreMoney(to, money);

 

}

});

 

 

}

 

}

 
 

 
 

 
 

(3)在配置文件中进行配置

 
 

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:aop="http://www.springframework.org/schema/aop"

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.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.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">

 

 

<!-- 配置内置连接池 -->

<bean
id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property
name="driverClassName"
value="com.mysql.jdbc.Driver"/>

<!--

jdbc:mysql:///tx_db 是 jdbc:mysql://localhost:3306/tx_db 的简写

-->

<property
name="url"
value="jdbc:mysql:///tx_db"/>

<property
name="username"
value="root"/>

<property
name="password"
value="8888"/>

</bean>

 

 

 

<!-- 配置事务管理器 -->

<bean
id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<!--

在 DataSourceTransactionManager 源代码中有

属性 dataSource 和其 set 方法,所以可以注入

-->

<property
name="dataSource"
ref="dataSource"/>

</bean>

 

 

<!-- 配置事务管理模板 -->

<bean
id="transactionTemplate"

class="org.springframework.transaction.support.TransactionTemplate">

<!--

在 TransactionTemplate 源代码中有属性 transactionTemplate

和其 set 方法,所以可以注入

-->

<property
name="transactionManager"
ref="transactionManager"/>

</bean>

 

 

 

<!-- 配置对象并注入属性 -->

<bean
id="accountService"
class="com.siwuxie095.service.AccountService">

<property
name="accountDao"
ref="accountDao"></property>

<!-- 在业务层注入注入事务管理模板 -->

<property
name="transactionTemplate"
ref="transactionTemplate"/>

</bean>

 

<bean
id="accountDao"
class="com.siwuxie095.dao.AccountDao">

<property
name="jdbcTemplate"
ref="jdbcTemplate"></property>

</bean>

 

<bean
id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">

<!--

在 JdbcTemplate 源代码中有属性 dataSource

和其 set 方法,所以可以注入

-->

<property
name="dataSource"
ref="dataSource"></property>

</bean>

 
 

 
 

</beans>

 
 

 
 

 
 

(4)编写一个测试类

 
 

TestDemo.java:

 
 

package com.siwuxie095.test;

 
 

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 
 

import com.siwuxie095.service.AccountService;

 
 

public class TestDmo {

 
 

/**

* 手动加上 @Test 以进行单元测试(将自动导入 JUnit 4 的 jar 包)

*

* 选中方法名,右键->Run As->JUint Test

*/

@Test

public
void testService() {

 

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

 

AccountService accountService=(AccountService) context.getBean("accountService");

 

accountService.transfer("小白", "小黑", 1000);

}

 

}

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

【made by siwuxie095】

Spring编程式事务管理的更多相关文章

  1. spring 编程式事务管理和声明式事务管理

    编程式事务管理 Spring 的编程式事务管理概述 在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择.用过 Hibernate 的人都知道,我们需要在代码中显式调用be ...

  2. Spring编程式事务管理及声明式事务管理

    本文将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. Spring 事务属性分析 事务管理 ...

  3. Spring编程式事务管理和声明式事务管理

    本来想写一篇随笔记一下呢,结果发现一篇文章写的很好了,已经没有再重复写的必要了. https://www.ibm.com/developerworks/cn/education/opensource/ ...

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

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

  5. Spring学习8-Spring事务管理(编程式事务管理)

    一.Spring事务的相关知识   1.事务是指一系列独立的操作,但在概念上具有原子性. 比如转账:A账号-100, B账号+100,完成.这两个操作独立是没问题的. 但在逻辑上,要么全部完成,要么一 ...

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

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

  7. Spring事务管理的另一种方式--TransactionTemplate编程式事务管理简单入门

    1, 一直以来, 在用Spring进行事物管理时, 只知道用声明式的策略, 即根据不同的数据源, 配置一个事物管理器(TransactionManager), 通过配置切面(PointCut)应用到相 ...

  8. 8.spring:事务管理(上):Spring的数据库编程、编程式事务管理

    Spring的数据库编程 Spring框架提供了JDBC模板模式------>JdbcTemplate 简化了开发,在开发中并不经常是使用 实际开发更多使用的是Hibernate和MyBatis ...

  9. Spring事务管理之编程式事务管理

    © 版权声明:本文为博主原创文章,转载请注明出处 案例:利用Spring的编程式事务管理模拟转账过程 数据库准备 -- 创建表 CREATE TABLE `account`( `id` INT NOT ...

随机推荐

  1. 解决Ubuntu下使用命令行subl 打开Sublime text3无法输入中文的问题

    cd /opt/sublime_text/ sudo vim sub-fcitx.c 新建文件sub-fcitx.c,建议放在Sublime Text的所在目录下,将下面的代码复制进去 ,参考: ht ...

  2. 在WINDOWS任务计划程序下执行PHP文件 PHP定时功能的实现

    最近需要做一个定时任务功能,从网站找了很多相关的代码,windows实现方法综合起来大概就两种, 一.使用PHP ignore_user_abort 函数 即使关掉浏览器也能正常运行:(个人感觉PHP ...

  3. uploadify是通过flash上传,服务器获取type为application/octet-stream

    uploadify是通过flash上传,服务器获取type为application/octet-stream,因此允许上传的类型要加上application/octet-stream

  4. vmware12共享windows的文件给虚拟的linux

    1:首先我的vmware的版本是12的 点击vmware的虚拟机---------------------->设置------------------------>选项---------- ...

  5. Mac parallels desktop安装windows,linux

    前言 这款软件你就看作是虚拟机vm,如果你要安装win10系统,请下载ios镜像文件 下载准备工作 Parallels Desktop 13 破解版本 联系站长所要 win10 iso镜像文件    ...

  6. ExtJS模版技术

    学习ExtJS一段时间以后,大家基本都会对于一些显示数据的组件不太符合需求,可能自己需要的组件在ExtJS里面不存在,这是大家基本就会使用Html属性,直接使用Html进行绘制页面数据展现. 但是,使 ...

  7. php do while循环实例

    do-while循环和while循环非常相似,其区别只是在于do-while保证必须执行一次,而while在表达式不成立时则可能不做任何操作. do-while 循环只有一种语法: do { stat ...

  8. EMNLP 2018 | 用强化学习做神经机器翻译:中山大学&MSRA填补多项空白

    人工深度学习和神经网络已经为机器翻译带来了突破性的进展,强化学习也已经在游戏等领域取得了里程碑突破.中山大学数据科学与计算机学院和微软研究院的一项研究探索了强化学习在神经机器翻译领域的应用,相关论文已 ...

  9. ADODB 调用存储过程

    追加参数法调用存储过程 追加参数通过CreateParameter方法,用来指定属性创建新的Parameter对象.具体语法如下: Set parameter = command.CreatePara ...

  10. php 3DES|DES 加密解密(通用)

    <?php //set_include_path(get_include_path().PATH_SEPARATOR.'phpseclib'); include('Crypt/DES.php') ...