Spring事务管理(转账例子)

avatar 2017年08月08日19:31:54 1 3398 views
博主分享免费Java教学视频,B站账号:Java刘哥

搭建转账环境

1、创建数据库,添加数据 2、 创建service和dao类,完成注入关系 (1)service层又叫业务逻辑层 (2)dao层,单纯对数据库操作层,在dao层不添加业务 (3)需求:Jerry 转账 300 给 Tom - Jerry 少 300 - Tom 多 300 详细代码,见下面   3、解决问题 (1)如果 Jerry 转出了 300 之后,出现异常(比如银行停电之类的),Tom 不会多 300,钱丢失了   4、解决 (1)添加事务解决,出现异常进行回滚操作  

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

1、 配置文件方式使用aop思想配置 第一步、 配置事务管理器 第二步、 配置事务增强 第三步 、配置切面   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"
  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="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、注解的形式更简单 第一步 配置事务管理器 第二步 配置事务注解 第三步 在要使用事务的方法所在类上面添加注解   以上三步 替代 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"
  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="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>
  参考:传智播客视频 本文链接:https://liuyanzhao.com/5717.html
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0