zoukankan      html  css  js  c++  java
  • Spring框架的事务管理之编程式的事务管理(了解)

    1. 说明:Spring为了简化事务管理的代码:提供了模板类 TransactionTemplate,所以手动编程的方式来管理事务,只需要使用该模板类即可!!
    2.手动编程方式的具体步骤如下:
      1.步骤一:创建WEB工程,引入需要的jar包
          * IOC的6个包
          * AOP的4个包
          * C3P0的1个包
          * MySQL的驱动包
          * JDBC目标2个包
          * 整合JUnit测试包
      2.步骤二:创建数据库的表结构
          create database spring_day03;
          use spring_day03;
          create table t_account(
              id int primary key auto_increment,
              name varchar(20),
              money double
         );
      3.步骤三:引入配置文件
          * 引入配置文件
              * 引入log4j.properties
      4. 步骤四:引入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:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"> 
         
    </beans> 
        * 使用C3P0连接池
            * 先引入C3P0的jar包
                * com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
    
            * 编写配置文件
                <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
                    <property name="jdbcUrl" value="jdbc:mysql:///spring-day03"/>
                    <property name="user" value="root"/>
                    <property name="password" value="root"/>
                </bean>
      5.步骤三:创建对应的包结构和类(具体内容,见“https://www.cnblogs.com/wyhluckdog/p/10137283.html”这篇博文)
          * com.huida.demo1
              * AccountService
              * AccountServlceImpl
              * AccountDao
              * AccountDaoImpl 
      6.步骤五:引入Spring的配置文件,将类配置到Spring中
    <bean id="accountDao" class="com.huida.demo1.AccountDaoImpl">
            <property name="dataSource" ref="dataSource"/>
        </bean>
         <bean id="accountService" class="com.huida.demo1.AccountServiceImpl">
                <property name="accountDao" ref="accountDao"/>
         </bean>
      7.步骤六:配置一个事务管理器,Spring使用PlatformTransactionManager接口来管理事务,所以咱们需要使用到他的实现类!!
            <!-- 配置事务管理器 -->
            <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                <property name="dataSource" ref="dataSource"/>
            </bean>
      8.步骤七:配置事务管理的模板
            <!-- 配置事务管理的模板 -->
            <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
                <property name="transactionManager" ref="transactionManager"/>
            </bean>
      9.步骤八:在需要进行事务管理的类中,注入事务管理的模板.
            <bean id="accountService" class="com.huida.demo1.AccountServiceImpl">
                <property name="accountDao" ref="accountDao"/>
                <property name="transactionTemplate" ref="transactionTemplate"/>
            </bean>
      10.完整的配置文件配置信息为:
    <?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:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"> 
         
         <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
             <property name="driverClass" value="com.mysql.jdbc.Driver"/>
             <property name="jdbcUrl" value="jdbc:mysql:///spring-day03"/>
             <property name="user" value="root"/>
             <property name="password" value="root"/>
         </bean>
        
            <!-- 配置事务管理器 -->
            <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                <property name="dataSource" ref="dataSource"/>
            </bean>
            <!-- 配置事务管理的模板 -->
            <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
                <property name="transactionManager" ref="transactionManager"/>
            </bean>
        <bean id="accountDao" class="com.huida.demo1.AccountDaoImpl">
            <property name="dataSource" ref="dataSource"/>
        </bean>
         <bean id="accountService" class="com.huida.demo1.AccountServiceImpl">
                <property name="accountDao" ref="accountDao"/>
                <property name="transactionTemplate" ref="transactionTemplate"/>
         </bean>
    
        
         
    </beans>
      11.步骤九:在业务层使用模板管理事务:
    package com.huida.demo1;
    
    import javax.annotation.Resource;
    
    import org.springframework.transaction.TransactionStatus;
    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.transaction.support.TransactionCallbackWithoutResult;
    import org.springframework.transaction.support.TransactionTemplate;
    
    public class AccountServiceImpl implements AccountService{
    
        @Resource(name="accountDao")
        private AccountDaoImpl accountDao;
        
    
        public void setAccountDao(AccountDaoImpl accountDao) {
            this.accountDao = accountDao;
        }
    // 注入事务模板对象
        @Resource(name="transactionTemplate")
        private TransactionTemplate transactionTemplate;
        public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
            this.transactionTemplate = transactionTemplate;
        }
        
       
    
        public void pay(final String out, final String in, final double money) {
            transactionTemplate.execute(new TransactionCallbackWithoutResult() {
    
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    // 扣钱
                    accountDao.outMoney(out, money);
                  //  int a = 10/0;
                    // 加钱
                    accountDao.inMoney(in, money);
                }
            });
        }
    
        
    }
      12.测试代码为:
    package com.huida.demo1;
    
    import javax.annotation.Resource;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext3.xml")
    public class Demo1 {
    
        @Resource(name="accountService")
        private AccountService accountService;
        
        @Test
        public void run1(){
            accountService.pay("小明","小红",1000);
        }
    }
      13.单元测试run1方法,刷新spring-day03数据库中的user表,发现小明的钱减少了1000,小红的钱增加了1000.

      



  • 相关阅读:
    web前端学习(四)JavaScript学习笔记部分(8)-- JavaScript瀑布流
    localStorage对象简单应用
    html文档加载顺序简单理解
    回调函数理解(转载)
    web前端学习(四)JavaScript学习笔记部分(8)-- JavaScript 浏览器对象
    web前端学习(四)JavaScript学习笔记部分(7)-- JavaScript DOM对象控制HTML元素详解
    JS random函数深入理解(转载)
    web前端学习(四)JavaScript学习笔记部分(6)-- js内置对象
    下雪了-js下雪效果
    LESS笔记
  • 原文地址:https://www.cnblogs.com/wyhluckdog/p/10137312.html
Copyright © 2011-2022 走看看