• 那是从何处传来的钟声呢?偶尔听到那钟声,平添一份喜悦与向往之情。

Spring事务管理(转账例子)

后端 Nanait 12年前 (2012-12-05) 1046次浏览 已收录 0个评论 扫描二维码

搭建转账环境

1、创建数据库,添加数据

Spring 事务管理(转账例子)

2、 创建 service 和 dao 类,完成注入关系

Spring 事务管理(转账例子)

(1)service 层又叫业务逻辑层

Spring 事务管理(转账例子)

(2)dao 层,单纯对数据库操作层,在 dao 层不添加业务

Spring 事务管理(转账例子)

(3)需求:Jerry 转账 300 给 Tom

– Jerry 少 300

– Tom 多 300

详细代码,见下面

 

3、解决问题

(1)如果 Jerry 转出了 300 之后,出现异常(比如银行停电之类的),Tom 不会多 300,钱丢失了

 

4、解决

(1)添加事务解决,出现异常进行回滚操作

 

声明式事务管理(xml 配置)

1、 配置文件方式使用 aop 思想配置

第一步、 配置事务管理器

Spring 事务管理(转账例子)

第二步、 配置事务增强

Spring 事务管理(转账例子)

第三步 、配置切面

Spring 事务管理(转账例子)

 

2、完整代码如下

  • AccountDao.java
  1. package com.liuyanzhao.spring.demo1;
  2. import org.springframework.jdbc.core.JdbcTemplate;
  3. /**
  4.  * Created by 言曌 on 2017/8/8.
  5.  */
  6. public class AccountDao {
  7.     //注入 JdbcTemplate 对象
  8.     private JdbcTemplate jdbcTemplate;
  9.     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  10.         this.jdbcTemplate = jdbcTemplate;
  11.     }
  12.     /**
  13.      * @param name      转账人
  14.      * @param money     转账金额
  15.      */
  16.     public void outMoney(String name,double money) {
  17.         String sql = “update account set money = money – ? where name = ?”;
  18.         jdbcTemplate.update(sql,money,name);
  19.     }
  20.     /**
  21.      * @param name      收账人
  22.      * @param money     收账金额
  23.      */
  24.     public void inMoney(String name,double money) {
  25.         String sql = “update account set money = money + ? where name = ?”;
  26.         jdbcTemplate.update(sql,money,name);
  27.     }
  28. }
  • AccountService.java
  1. package com.liuyanzhao.spring.demo1;
  2. /**
  3.  * Created by 言曌 on 2017/8/8.
  4.  */
  5. public class AccountService {
  6.     //注入 ordersDao 对象
  7.     private AccountDao accountDao;
  8.     public void setAccountDao(AccountDao accountDao) {
  9.         this.accountDao = accountDao;
  10.     }
  11.     /**
  12.      * @param Transfer_person   转账人
  13.      * @param account_holder    收账人
  14.      * @param money             交易金额
  15.      */
  16.     public void accountMoney(String Transfer_person,String account_holder,double money) {
  17.         //转账
  18.         accountDao.outMoney(Transfer_person,money);
  19.         /**
  20.          * 假如,转账过程中,出现异常
  21.          * 如果我们不配置事务管理,会出现 转账成功,收账失败
  22.          * 如果我们配置了事务管理,两者必然是同时成功,同时失败
  23.          */
  24.         //int i = 10/0;
  25.         //收账
  26.         accountDao.inMoney(account_holder,money);
  27.     }
  28. }
  • applicationContext.xml   配置文件
  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=http://www.springframework.org/schema/beans&#8221;
  3.        xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;
  4.        xmlns:context=http://www.springframework.org/schema/context&#8221;
  5.        xmlns:aop=http://www.springframework.org/schema/aop&#8221;
  6.        xmlns:tx=http://www.springframework.org/schema/tx&#8221;
  7.        xsi:schemaLocation=”http://www.springframework.org/schema/beans
  8.     http://www.springframework.org/schema/beans/spring-beans.xsd
  9.     http://www.springframework.org/schema/context
  10.     http://www.springframework.org/schema/context/spring-context.xsd
  11.     http://www.springframework.org/schema/aop
  12.     http://www.springframework.org/schema/aop/spring-aop.xsd
  13.     http://www.springframework.org/schema/tx
  14.     http://www.springframework.org/schema/tx/spring-tx.xsd”>
  15.     <context:component-scan base-package=“com.liuyanzhao.spring.demo1”/>
  16.     <!– 引入外部属性文件 –>
  17.     <context:property-placeholder location=“classpath:jdbc.properties”/>
  18.     <!– 配置 C3P0 连接池 –>
  19.     <bean id=“dataSource” class=“com.mchange.v2.c3p0.ComboPooledDataSource”>
  20.         <property name=“driverClass” value=“${jdbc.driverClass}”/>
  21.         <property name=“jdbcUrl” value=“${jdbc.url}”/>
  22.         <property name=“user” value=“${jdbc.username}”/>
  23.         <property name=“password” value=“”/>
  24.     </bean>
  25.     <!–
  26.         id 值可以随便写,与 ref 对应;
  27.         name 值 是 类中的要注入的属性
  28.     —>
  29.     <bean id=“accountService” class=“com.liuyanzhao.spring.demo1.AccountService”>
  30.         <property name=“accountDao” ref=“accountDao”></property>
  31.     </bean>
  32.     <bean id=“accountDao” class=“com.liuyanzhao.spring.demo1.AccountDao”>
  33.         <property name=“jdbcTemplate” ref=“jdbcTemplate”></property>
  34.     </bean>
  35.     <bean id=“jdbcTemplate” class=“org.springframework.jdbc.core.JdbcTemplate”>
  36.         <property name=“dataSource” ref=“dataSource”></property>
  37.     </bean>
  38.     <!–第一步、配置事务管理器–>
  39.     <bean id=“transactionManager” class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”>
  40.         <!–注入 DataSource–>
  41.         <property name=“dataSource” ref=“dataSource”></property>
  42.     </bean>
  43.     <!–第二步、配置事务增强–>
  44.     <tx:advice id=“txadvice” transaction-manager=“transactionManager”>
  45.         <!–做事务操作–>
  46.         <tx:attributes>
  47.             <!–设置事务操作的方法匹配规则–>
  48.             <tx:method name=“account*” propagation=“REQUIRED” />
  49.         </tx:attributes>
  50.     </tx:advice>
  51.     <!–第三步、配置切面–>
  52.     <aop:config>
  53.         <!–切入点–>
  54.         <aop:pointcut id=“pointcut1” expression=“execution(* com.liuyanzhao.spring.demo1.AccountService.*(..))”/>
  55.         <!–切面–>
  56.         <aop:advisor advice-ref=“txadvice” pointcut-ref=“pointcut1”/>
  57.     </aop:config>
  58. </beans>
  • ServiceTest.java  测试类
  1. package com.liuyanzhao.spring.demo1;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. /**
  6.  * Created by 言曌 on 2017/8/8.
  7.  */
  8. public class ServiceTest {
  9.     @Test
  10.     public void test() {
  11.         ApplicationContext context =
  12.             new ClassPathXmlApplicationContext(“applicationContext.xml”);
  13.         AccountService accountService = (AccountService) context.getBean(“accountService”);
  14.         //Jerry 给 Tom 转账 300
  15.         accountService.accountMoney(“Jerry”,“Tom”,300);
  16.     }
  17. }

 

声明式事务管理(注解)

1、注解的形式更简单

第一步 配置事务管理器

Spring 事务管理(转账例子)

第二步 配置事务注解

Spring 事务管理(转账例子)

第三步 在要使用事务的方法所在类上面添加注解

Spring 事务管理(转账例子)

 

以上三步 替代 xml 方式 的三步,相对更简单。

2、完整代码如下

  • AccountDao.java

同上

  • ServiceTest.java

同上

  • AccountService.java
  1. package com.liuyanzhao.spring.demo1;
  2. import org.springframework.transaction.annotation.Transactional;
  3. /**
  4.  * Created by 言曌 on 2017/8/8.
  5.  */
  6. @Transactional
  7. public class AccountService {
  8.     //注入 ordersDao 对象
  9.     private AccountDao accountDao;
  10.     public void setAccountDao(AccountDao accountDao) {
  11.         this.accountDao = accountDao;
  12.     }
  13.     /**
  14.      * @param Transfer_person   转账人
  15.      * @param account_holder    收账人
  16.      * @param money             交易金额
  17.      */
  18.     public void accountMoney(String Transfer_person,String account_holder,double money) {
  19.         //转账
  20.         accountDao.outMoney(Transfer_person,money);
  21.         /**
  22.          * 假如,转账过程中,出现异常
  23.          * 如果我们不配置事务管理,会出现 转账成功,收账失败
  24.          * 如果我们配置了事务管理,两者必然是同时成功,同时失败
  25.          */
  26.         //int i = 10/0;
  27.         //收账
  28.         accountDao.inMoney(account_holder,money);
  29.     }
  30. }
  • applicationContext.xml
  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=http://www.springframework.org/schema/beans&#8221;
  3.        xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;
  4.        xmlns:context=http://www.springframework.org/schema/context&#8221;
  5.        xmlns:aop=http://www.springframework.org/schema/aop&#8221;
  6.        xmlns:tx=http://www.springframework.org/schema/tx&#8221;
  7.        xsi:schemaLocation=”http://www.springframework.org/schema/beans
  8.     http://www.springframework.org/schema/beans/spring-beans.xsd
  9.     http://www.springframework.org/schema/context
  10.     http://www.springframework.org/schema/context/spring-context.xsd
  11.     http://www.springframework.org/schema/aop
  12.     http://www.springframework.org/schema/aop/spring-aop.xsd
  13.     http://www.springframework.org/schema/tx
  14.     http://www.springframework.org/schema/tx/spring-tx.xsd”>
  15.     <context:component-scan base-package=“com.liuyanzhao.spring.demo1”/>
  16.     <!– 引入外部属性文件 –>
  17.     <context:property-placeholder location=“classpath:jdbc.properties”/>
  18.     <!– 配置 C3P0 连接池 –>
  19.     <bean id=“dataSource” class=“com.mchange.v2.c3p0.ComboPooledDataSource”>
  20.         <property name=“driverClass” value=“${jdbc.driverClass}”/>
  21.         <property name=“jdbcUrl” value=“${jdbc.url}”/>
  22.         <property name=“user” value=“${jdbc.username}”/>
  23.         <property name=“password” value=“”/>
  24.     </bean>
  25.     <!–
  26.         id 值可以随便写,与 ref 对应;
  27.         name 值 是 类中的要注入的属性
  28.     —>
  29.     <bean id=“accountService” class=“com.liuyanzhao.spring.demo1.AccountService”>
  30.         <property name=“accountDao” ref=“accountDao”></property>
  31.     </bean>
  32.     <bean id=“accountDao” class=“com.liuyanzhao.spring.demo1.AccountDao”>
  33.         <property name=“jdbcTemplate” ref=“jdbcTemplate”></property>
  34.     </bean>
  35.     <bean id=“jdbcTemplate” class=“org.springframework.jdbc.core.JdbcTemplate”>
  36.         <property name=“dataSource” ref=“dataSource”></property>
  37.     </bean>
  38.     <!–第一步 配置事务管理器–>
  39.     <bean id=“transactionManager” class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”>
  40.         <property name=“dataSource” ref=“dataSource”></property>
  41.     </bean>
  42.     <!–第二步 开启事务注解–>
  43.     <tx:annotation-driven transaction-manager=“transactionManager”/>
  44. </beans>

 

参考:传智播客视频


何处钟 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Spring 事务管理(转账例子)
喜欢 (0)
[15211539367@163.com]
分享 (0)

您必须 登录 才能发表评论!