转账案例环境搭建

1.引入JAR包

IOC的6个包

AOP的4个包

C3P0的1个包

MySQL的1个驱动包

JDBC的2个目标包

整合JUnit测试1个包

2.引入配置文件

log4j.properties+applicationContext.xml

  1. ### direct log messages to stdout ###
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  3. log4j.appender.stdout.Target=System.err
  4. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  5. log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
  6.  
  7. ### direct messages to file mylog.log ###
  8. log4j.appender.file=org.apache.log4j.FileAppender
  9. log4j.appender.file.File=c\:mylog.log
  10. log4j.appender.file.layout=org.apache.log4j.PatternLayout
  11. log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
  12.  
  13. ### set log levels - for more verbose logging change 'info' to 'debug' ###
  14.  
  15. log4j.rootLogger=info, stdout
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx.xsd">
  16.  
  17. <!-- C3P0连接池 -->
  18. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  19. <property name="driverClass" value="com.mysql.jdbc.Driver"/>
  20. <property name="jdbcUrl" value="jdbc:mysql:///spring_day03"/>
  21. <property name="user" value="root"/>
  22. <property name="password" value="toor"/>
  23. </bean>
  24.  
  25. </beans>

3.创建对应的包结构

  1. public interface AccountDao {
  2.  
  3. /**
  4. * 转出
  5. * @param out
  6. * @param money
  7. */
  8. public void outMoney(String out,double money);
  9.  
  10. /**
  11. * 转入
  12. * @param in
  13. * @param money
  14. */
  15. public void inMoney(String in,double money);
  16.  
  17. }
  1. public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
  2.  
  3. //转出
  4. @Override
  5. public void outMoney(String out, double money) {
  6. // TODO Auto-generated method stub
  7. this.getJdbcTemplate().update("update t_account set money=money-? where name=?", money,out);
  8. }
  9.  
  10. //转入
  11. @Override
  12. public void inMoney(String in, double money) {
  13. // TODO Auto-generated method stub
  14. this.getJdbcTemplate().update("update t_account set money=money+? where name=?", money,in);
  15. }
  16.  
  17. }
  1. public interface AccountService {
  2.  
  3. /**
  4. * 转账
  5. * @param out
  6. * @param in
  7. * @param money
  8. */
  9. public void pay(String out,String in,double money);
  10.  
  11. }
  1. public class AccountServiceImpl implements AccountService {
  2.  
  3. private AccountDao accountDao;
  4. public void setAccountDao(AccountDao accountDao) {
  5. this.accountDao = accountDao;
  6. }
  7.  
  8. @Override
  9. public void pay(String out, String in, double money) {
  10. // TODO Auto-generated method stub
  11.  
  12. //转出
  13. accountDao.outMoney(out, money);
  14.  
  15. //异常
  16. //int i = 1/0;
  17.  
  18. //转入
  19. accountDao.inMoney(in, money);
  20. }
  21.  
  22. }

4.修改配置文件

  1. <!-- 管理业务层 注入持久层对象-->
  2. <bean id="accountService" class="com.spring.demo1.AccountServiceImpl">
  3. <property name="accountDao" ref="accountDao"/>
  4. </bean>
  5.  
  6. <!-- 管理持久层 注入数据源(由于继承JdbcDaoSupport,自动生成JDBC模板类)-->
  7. <bean id="accountDao" class="com.spring.demo1.AccountDaoImpl">
  8. <property name="dataSource" ref="dataSource"/>
  9. </bean>

5.测试

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(value="classpath:applicationContext.xml")
  3. public class Demo1 {
  4.  
  5. @Resource(name="accountService")
  6. private AccountService accountService;
  7.  
  8. @Test
  9. public void m01(){
  10. accountService.pay("测试1", "测试2", 1000);
  11. }
  12.  
  13. }

不出现异常正常转账

出现异常不回滚

1.XML方式

1.配置事务管理器

  1. <!-- 配置事务管理器 -->
  2. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  3. <property name="dataSource" ref="dataSource"/>
  4. </bean>

2.配置事务增强

  1. <!-- 配置事务增强 -->
  2. <tx:advice id="myAdvice" transaction-manager="transactionManager">
  3. <tx:attributes >
  4. <tx:method name="pay"/>
  5. </tx:attributes>
  6. </tx:advice>

  name="pay" 代表切入点

4.配置AOP切面

  1. <!-- 配置AOP切面 -->
  2. <aop:config>
  3. <aop:advisor advice-ref="myAdvice" pointcut="execution(* com.spring.demo2.AccountServiceImpl.pay(..))"/>
  4. </aop:config>

  注意:如果是自己编写的切面,使用<aop:aspect>标签,如果是系统制作的,使用<aop:advisor>标签。

5.测试

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(value="classpath:applicationContext2.xml")
  3. public class Demo1 {
  4.  
  5. @Resource(name="accountService")
  6. private AccountService accountService;
  7.  
  8. @Test
  9. public void m01(){
  10. accountService.pay("测试1", "测试2", 1000);
  11. }
  12.  
  13. }

不出现异常正常转账

出现异常回滚

2.注解方式

1.配置事务管理器

  1. <!-- 配置事务管理器 -->
  2. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  3. <property name="dataSource" ref="dataSource"/>
  4. </bean>

2.开启注解事务

  1. <!-- 开启注解事务 -->
  2. <tx:annotation-driven transaction-manager="transactionManager"/>

3.添加注解

  1. 在业务层上添加一个注解:@Transactiona

4.测试

不出现异常正常转账

出现异常回滚

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(value="classpath:applicationContext3.xml")
  3. public class Demo1 {
  4.  
  5. @Resource(name="accountService")
  6. private AccountService accountService;
  7.  
  8. @Test
  9. public void m01(){
  10. accountService.pay("测试1", "测试2", 1000);
  11. }
  12.  
  13. }

【核心核心】10.Spring事务管理【TX】XML+注解方式的更多相关文章

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

    ----------------------siwuxie095                                 Spring 声明式事务管理(基于注解方式实现)         以转 ...

  2. Spring事务管理的四种方式(以银行转账为例)

    Spring事务管理的四种方式(以银行转账为例) 一.事务的作用 将若干的数据库操作作为一个整体控制,一起成功或一起失败.   原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不 ...

  3. 事务管理(下) 配置spring事务管理的几种方式(声明式事务)

    配置spring事务管理的几种方式(声明式事务) 概要: Spring对编程式事务的支持与EJB有很大的区别.不像EJB和Java事务API(Java Transaction API, JTA)耦合在 ...

  4. Spring事务管理之几种方式实现事务

    1.事务认识 大家所了解的事务Transaction,它是一些列严密操作动作,要么都操作完成,要么都回滚撤销.Spring事务管理基于底层数据库本身的事务处理机制.数据库事务的基础,是掌握Spring ...

  5. Spring事务管理之几种方式实现事务(转)

    一:事务认识 大家所了解的事务Transaction,它是一些列严密操作动作,要么都操作完成,要么都回滚撤销.Spring事务管理基于底层数据库本身的事务处理机制.数据库事务的基础,是掌握Spring ...

  6. Spring事务管理的xml方式

    一个业务的成功: 调用的service是执行成功的,意味着service中调用的所有的dao是执行成功的.  事务应该在Service层统一控制. 如果手动去实现,则需要对dao进行代理,在方法前后进 ...

  7. spring事务管理(xml配置)与spring自带连接数据库JdbcTemplate

    什么是事务,很通俗的话来说就是,我们日常生活中总会出现在银行转账的业务,加入A向B转账100元,此时A的账户中应该减少100元,B的账户中增加100元,但是如果在A转完账B还没有接受的时候,服务器出现 ...

  8. Spring 事务管理tx,aop

    spring tx:advice事务配置 2016年12月21日 17:27:22 阅读数:7629 http://www.cnblogs.com/rushoooooo/archive/2011/08 ...

  9. 9.Spring整合Hibernate_2_声明式的事务管理(Xml的方式)

    使用xml的方式进行声明式的事务管理 推荐使用xml的方式,因为可以同时为多个方法进行声明 <!-- 开启Spring中的事务管理(声明式的事务管理) xml--> <!-- 不管是 ...

随机推荐

  1. NX二次开发-打开part对话框UF_UI_open_part

    这是UFUN帮助的官方例子 /****************************************************************************** Copyri ...

  2. NX二次开发-C++ CopyFile函数的用法

    NX9+VS2012 #include<Windows.h> CopyFile("D:\\test.prt","D:\\1\\test123.prt" ...

  3. 随意从Android端抓取一些数据包看到的协议

    如题,就打开了某银行客户端,登录,查询余额,退出 打开支付宝钱包,登录,来回转账到余额宝 中间收到微信的消息 看到了以下协议: 银行客户端的:DNS (连接银行网站时使用,网站server返回也是用的 ...

  4. 牛客多校第十场 F Popping Balloons 线段树维护稀疏矩阵

    题意: 给定一个稀疏矩阵,里面有若干个气球,让你横着开三枪,竖着开三枪,问最多能打爆多少气球,要求相同方向,相邻两枪必须间隔r. 题解: 横向记录每列有多少个气球,分别在哪行上. 然后把这个数据改造成 ...

  5. KdPrint/DbgPrint and UNICODE_STRING/ANSI_STRING

    typedef struct _UNICODE_STRING { USHORT Length; USHORT MaximumLength; PWSTR Buffer; } UNICODE_STRING ...

  6. SSDT

    2.系统服务调度表SSDT及SSSDT Shadow 系统服务:由操作系统提供的一组函数(内核函数),API可以间接或者直接的调用系统服务.操作系统以动态链接库(DLL)的形式提供API. SSDT: ...

  7. spring boot 项目打成war,丢入tomcat独立运行

    小插曲:通过cmd运行startup.bat,cmd界面显示乱码 解决方法:进入tomcat目录,conf文件夹,用编辑器打开logging.properties 将java.util.logging ...

  8. mssql查询表在哪个数据库中

    mssql查询表在哪个数据库中 EXEC sp_MSforeachdb @command1='IF object_id(''?'' + ''..表名'') IS NOT NULL PRINT ''?' ...

  9. HTML_CSS使用

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  10. override new 的区别

    override : 方法提供从基类继承的成员的新实现. 通过 override 声明重写的方法称为重写基方法. 重写基方法必须具有与 override方法相同的签名 new : 关键字可以显式隐藏从 ...