您的位置:首页 > 编程语言 > Java开发

Spring事务管理(转账例子)

2017-11-08 18:52 169 查看

搭建转账环境

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

package com.liuyanzhao.spring.demo1;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * Created by 言曌 on 2017/8/8.
 */
public class AccountDao {
    //注入JdbcTemplate对象
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * @param name      转账人
     * @param money     转账金额
     */
    public void outMoney(String name,double money) {
        String sql = "update account set money = money - ? where name = ?";
        jdbcTemplate.update(sql,money,name);
    }

    /**
     * @param name      收账人
     * @param money     收账金额
     */
    public void inMoney(String name,double money) {
        String sql = "update account set money = money + ? where name = ?";
        jdbcTemplate.update(sql,money,name);
    }
}

AccountService.java

package com.liuyanzhao.spring.demo1;

/**
 * Created by 言曌 on 2017/8/8.
 */
public class AccountService {
    //注入ordersDao对象
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    /**
     * @param Transfer_person   转账人
     * @param account_holder    收账人
     * @param money             交易金额
     */
    public void accountMoney(String Transfer_person,String account_holder,double money) {
        //转账
        accountDao.outMoney(Transfer_person,money);

        /**
         * 假如,转账过程中,出现异常
         * 如果我们不配置事务管理,会出现 转账成功,收账失败
         * 如果我们配置了事务管理,两者必然是同时成功,同时失败
         */
        //int i = 10/0;

        //收账
        accountDao.inMoney(account_holder,money);

    }
}

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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.liuyanzhao.spring.demo1"/>

    <!-- 引入外部属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 配置C3P0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value=""/>
    </bean>

    <!--
        id值可以随便写,与ref对应;
        name值 是 类中的要注入的属性
    -->
    <bean id="accountService" class="com.liuyanzhao.spring.demo1.AccountService">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    <bean id="accountDao" class="com.liuyanzhao.spring.demo1.AccountDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--第一步、配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入DataSource-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--第二步、配置事务增强-->
    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <!--做事务操作-->
        <tx:attributes>
            <!--设置事务操作的方法匹配规则-->
            <tx:method name="account*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>

    <!--第三步、配置切面-->
    <aop:config>
        <!--切入点-->
        <aop:pointcut id="pointcut1" expression="execution(* com.liuyanzhao.spring.demo1.AccountService.*(..))"/>
        <!--切面-->
        <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
    </aop:config>

</beans>

ServiceTest.java  测试类

package com.liuyanzhao.spring.demo1;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by 言曌 on 2017/8/8.
 */
public class ServiceTest {
    @Test
    public void test() {
        ApplicationContext context =
            new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = (AccountService) context.getBean("accountService");
        //Jerry 给 Tom 转账 300
        accountService.accountMoney("Jerry","Tom",300);
    }
}

 

声明式事务管理(注解)

1、注解的形式更简单

第一步 配置事务管理器



第二步 配置事务注解



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



 

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

2、完整代码如下

AccountDao.java
同上

ServiceTest.java
同上

AccountService.java

package com.liuyanzhao.spring.demo1;

import org.springframework.transaction.annotation.Transactional;

/**
 * Created by 言曌 on 2017/8/8.
 */

@Transactional
public class AccountService {

    //注入ordersDao对象
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    /**
     * @param Transfer_person   转账人
     * @param account_holder    收账人
     * @param money             交易金额
     */
    public void accountMoney(String Transfer_person,String account_holder,double money) {
        //转账
        accountDao.outMoney(Transfer_person,money);

        /**
         * 假如,转账过程中,出现异常
         * 如果我们不配置事务管理,会出现 转账成功,收账失败
         * 如果我们配置了事务管理,两者必然是同时成功,同时失败
         */
        //int i = 10/0;

        //收账
        accountDao.inMoney(account_holder,money);

    }
}

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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.liuyanzhao.spring.demo1"/>

    <!-- 引入外部属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 配置C3P0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value=""/>
    </bean>

    <!--
        id值可以随便写,与ref对应;
        name值 是 类中的要注入的属性
    -->
    <bean id="accountService" class="com.liuyanzhao.spring.demo1.AccountService">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    <bean id="accountDao" class="com.liuyanzhao.spring.demo1.AccountDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

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

    <!--第二步 开启事务注解-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

 

参考:传智播客视频

本文链接:https://liuyanzhao.com/5717.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: