Spring入门之事务管理【二】注解方式

49 篇文章 5 订阅
37 篇文章 1 订阅

前言:

  • 本文讲解Spring的事务管理(注解方式)实现
  • 本文是通过一步步创建项目的方式讲解
  • 本文主要是讲具体的操作,对于事务管理的原理并不会深入讲解

环境:

  • Intellij IDEA 2017 CI

具体步骤

1、创建Spring项目

这里写图片描述

如果不清楚怎么创建项目,请移步Spring入门之事务管理【一】XML配置文件方式

OrdersDao类:

public class OrdersDao {

//    注入jdbcTemplate
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    /*
    做对数据库的操作,不写业务操作
     */

    //减少金额
    public void lessMoney(){
        String sql = "update account set salary=salary-? where name=?";
        jdbcTemplate.update(sql,100,"Tom");
    }

    //增加金额
    public void moreMoney(){
        String sql = "update account set salary=salary+? where name=?";
        jdbcTemplate.update(sql,100,"John");
    }
}

OrdersService类:

public class OrdersService {
    private OrdersDao ordersDao;

    public void setOrdersDao(OrdersDao ordersDao) {
        this.ordersDao = ordersDao;
    }

    //调用dao方法
    /*
    业务逻辑层
     */

    //转账业务
    public void accountMonry(){

        ordersDao.lessMoney();  //减少金额

        int i = 10/0;   //模拟出现异常

        ordersDao.moreMoney();  //增加金额
    }
}

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

    <!--配置C3P0连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--注入属性值-->
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///test"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

    <!--service注入dao-->
    <bean class="spring.service.OrdersService" id="ordersService">
        <property name="ordersDao" ref="ordersDao"></property>
    </bean>
    <!--dao注入jdbcTemplate-->
    <bean class="spring.dao.OrdersDao" id="ordersDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <!--jdbcTemplate注入数据源-->
    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>


</beans>

如果不清楚上面类中方法的内容的,或者对Spring中的Bean管理,注入属性不是很明白的,可以看下博主的这几篇文章

2、配置事务管理器

<!--第一步 配置事务管理器-->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源,指定对哪个数据库进行操作-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

3、开启事务注解

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

4、在事务所在类上加注解

这里写图片描述

@Transactional
public class OrdersService {

5、测试

TestService类:

package spring.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.service.OrdersService;

public class TestService {

    @Test
    public void testService(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        OrdersService ordersService = (OrdersService) applicationContext.getBean("ordersService");
        ordersService.accountMonry();
    }

}

测试前数据库表:
这里写图片描述

测试结果:
这里写图片描述
测试失败:因为我们在Service类中进行了手动模拟异常

测试后数据库:
这里写图片描述

数据库中表的数据并没有变化,说明在异常发生之后,进行了事务回滚,事务管理成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值