zoukankan      html  css  js  c++  java
  • Spring编程式事务管理

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

       

       

       

       

       

       

       

       

    Spring 编程式事务管理

       

       

    以转账为例

       

       

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

       

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

       

       

       

    手动添加数据,用作测试

       

       

       

       

       

    2、具体步骤

       

    1)配置事务管理器

       

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

    <bean id="transactionManager"

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

    <!--

    DataSourceTransactionManager 源代码中有

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

    -->

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

    </bean>

       

       

       

    2)配置事务管理模板

       

    <!-- 配置事务管理模板 -->

    <bean id="transactionTemplate"

    class="org.springframework.transaction.support.TransactionTemplate">

    <!--

    TransactionTemplate 源代码中有属性 transactionTemplate

    和其 set 方法,所以可以注入

    -->

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

    </bean>

       

       

       

    3)在业务层注入事务管理模板

       

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

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

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

    <!-- 在业务层注入注入事务管理模板 -->

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

    </bean>

       

       

       

    4)在业务层手动编写代码实现事务管理

       

       

       

       

    3、具体实现

       

    1)编写 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.TransactionStatus;

    import org.springframework.transaction.support.TransactionCallbackWithoutResult;

    import org.springframework.transaction.support.TransactionTemplate;

       

    import com.siwuxie095.dao.AccountDao;

       

    public class AccountService {

       

    private AccountDao accountDao;

    private TransactionTemplate transactionTemplate;

     

    public void setAccountDao(AccountDao accountDao) {

    this.accountDao = accountDao;

    }

     

    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {

    this.transactionTemplate = transactionTemplate;

    }

     

     

    /**

    * 转账

    */

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

     

     

    // 在业务层手动编写代码实现事务管理

    transactionTemplate.execute(new TransactionCallbackWithoutResult() {

     

    @Override

    protected void doInTransactionWithoutResult(TransactionStatus status) {

     

    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>

     

     

    <!-- 配置事务管理模板 -->

    <bean id="transactionTemplate"

    class="org.springframework.transaction.support.TransactionTemplate">

    <!--

    TransactionTemplate 源代码中有属性 transactionTemplate

    和其 set 方法,所以可以注入

    -->

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

    </bean>

     

     

     

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

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

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

    <!-- 在业务层注入注入事务管理模板 -->

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

    </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】

  • 相关阅读:
    stty
    ping
    read
    echo
    grep
    date
    vi与vim编辑器使用
    rename
    netstat
    input输入框的背景图片也可以这样玩
  • 原文地址:https://www.cnblogs.com/siwuxie095/p/7420052.html
Copyright © 2011-2022 走看看