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

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

Spring 声明式事务管理(基于注解方式实现)

 
 

 
 

以转账为例

 
 

 
 

1、导入相关
jar 包(共 10 个包)

 
 

(1)导入核心 jar 包和日志相关的 jar 包

 
 

 
 

 
 

 
 

(2)导入
JdbcTemplate 的 jar 包

 
 

 
 

 
 

 
 

(3)导入
MySQL 的 JDBC 驱动包

 
 

 
 

 
 

mysql-connector-java
下载链接:

 
 

https://dev.mysql.com/downloads/connector/j/

 
 

 
 

 
 

(4)导入 AOP 的 jar 包

 
 

 
 

 
 

 
 

 
 

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

 
 

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

 
 

 
 

 
 

手动添加数据,用作测试

 
 

 
 

 
 

 
 

 
 

3、具体步骤

 
 

(1)配置事务管理器

 
 

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

<bean
id="transactionManager"

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

<!--

在 DataSourceTransactionManager 源代码中有

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

-->

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

</bean>

 
 

 
 

 
 

(2)配置事务注解

 
 

<!-- 配置事务注解,即
开启事务注解 -->

<tx:annotation-driven
transaction-manager="transactionManager"/>

 
 

 
 

 
 

(3)在使用事务的类上添加注解:@Transactional

 
 

「使用事务的类一般在业务层」

 
 

 
 

 
 

 
 

4、具体实现

 
 

 
 

编写一个
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.annotation.Transactional;

 
 

import com.siwuxie095.dao.AccountDao;

 
 

// 在使用事务的类上添加注解:@Transactional

@Transactional

public class AccountService {

 
 

private AccountDao accountDao;

 

public
void setAccountDao(AccountDao accountDao) {

this.accountDao = accountDao;

}

 

 

/**

* 转账

*/

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

 

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>

 

 

<!-- 配置事务注解,即
开启事务注解 -->

<tx:annotation-driven
transaction-manager="transactionManager"/>

 

 

 

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

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

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

</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声明式事务管理基于@Transactional注解

    概述:我们已知道Spring声明式事务管理有两种常用的方式,一种是基于tx/aop命名空间的xml配置文件,另一种则是基于@Transactional 注解.         第一种方式我已在上文为大 ...

  2. Spring声明式事务管理基于tx/aop命名空间

    目的:通过Spring AOP 实现Spring声明式事务管理; Spring支持编程式事务管理和声明式事务管理两种方式. 而声明式事务管理也有两种常用的方式,一种是基于tx/aop命名空间的xml配 ...

  3. Spring事务管理之声明式事务管理-基于注解的方式

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

  4. Spring声明式事务管理(基于XML方式实现)

    --------------------siwuxie095                             Spring 声明式事务管理(基于 XML 方式实现)         以转账为例 ...

  5. Spring 声明式事务管理方式

    声明式事务管理,基于AOP对目标代理,添加环绕通知,比编码方案优势,不具有侵入式,不需要修改原来的代码. 1.基于XML配置的声明式事务管理方案(案例)      接口Service public i ...

  6. XML方式实现Spring声明式事务管理

    1.首先编写一个实体类 public class Dept { private int deptId; private String deptName; public int getDeptId() ...

  7. Spring声明式事务管理与配置介绍

    转至:http://java.9sssd.com/javafw/art/1215 [摘要]本文介绍Spring声明式事务管理与配置,包括Spring声明式事务配置的五种方式.事务的传播属性(Propa ...

  8. Spring声明式事务如何选择代理方式?

    Spring声明式事务如何选择代理方式   解决方法: 1.基于注解方法: <tx:annotation-driven transaction-manager="txManager&q ...

  9. spring 声明式事务管理

    简单理解事务: 比如你去ATM机取5000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉5000元钱:然后ATM出5000元钱.这两个步骤必须是要么都执行要么都不执行.如果银行卡扣除了5000块但 ...

随机推荐

  1. 给easyui datebox扩展一个清空按钮

    /** * 给时间框控件扩展一个清除的按钮 */ $.fn.datebox.defaults.cleanText = '清空'; (function ($) { var buttons = $.ext ...

  2. zclip复制到剪切板插件有个bug

    今天发现zclip复制到剪切板插件有个bug,就是在遨游和360浏览器的兼容模式下,点击复制没响应,后来我看了页面代码,发现在这两个浏览器的兼容模式下,生成的是<object>,其他浏览器 ...

  3. wp模版强制用CSS空两格的问题

    之前我写过一篇文章<关于模板该不该用css强制编辑器文本开头空两格>,里面有说到一个观点,模版作者设计的时候,不要控制文章段落空两格,但是我用久了wp,我才慢慢发现,做wp模版的时候,确实 ...

  4. [UE4]目标是Pawn、Get Player Character

    “目标是Pawn”表示这一个定义继承与Pawn类的方法. 这样可以很清楚的看到这个是方法是在什么地方定义的 “Get Player Character”可以获得当前控制的角色实例,可以转换成真正具体的 ...

  5. 使用Golang进行性能分析(Profiling)

    转自:http://www.cppblog.com/sunicdavy/archive/2015/04/11/210308.html 本文介绍游戏服务器的性能分析, web服务器性能分析不在本文分析范 ...

  6. 文件读写’r'和’rb’区别

    2012年08月22日 ⁄ 综合 ⁄ 共 849字 ⁄ 字号 小 中 大 ⁄ 评论关闭不管何种语言在进行文件读写时,大家都知道有以下模式: r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别 ...

  7. MySQL数据库索引(中)

    上一篇回顾: 1.一个索引对应一颗B+树,所有的真实记录都是存在叶子节点里面的,所有的项目录都存在内节点或者说根节点上. 2.innodb会为我们的表格主键添加一个聚簇索引,如果没有主键的话数据库是会 ...

  8. maven快速入门之安装

    maven是基于项目对象模型(pom) 可以通过y一小段描述信息来管理项目的构建,报告,和文档的软件项目管理工具. 覆盖编译,测试运行清理构建周期,提供仓库的概念,统一帮助管理项目. 下载:http: ...

  9. selenium+python自动化98--文件下载弹窗处理(PyKeyboard)

    前言 在web自动化下载操作时,有时候会弹出下载框,这种下载框不属于web的页面,是没办法去定位的(有些同学一说到点击,脑袋里面就是定位!定位!定位!) 有时候我们并不是非要去定位到这个按钮再去点击, ...

  10. 应用层级时空记忆模型(HTM)实现对实时异常流时序数据检测

    应用层级时空记忆模型(HTM)实现对实时异常流时序数据检测 Real-Time Anomaly Detection for Streaming Analytics Subutai Ahmad SAHM ...