Spring-事务管理(使用注解方式实现声明式事务管理)

1.在spring配置文件中,配置事务管理器

2.在spring配置文件中,开启事务注解

(1)引入名称空间tx

(2)开启事务注解

3.在service类上面(获取service类里面方法上面),添加事务注解@Transactional

(1)@Transactional,这个注解可以添加到类上面,也可以添加到方法上面

(2)如果把这个类添加到类上面,这个类里面所有的方法都添加事务

(3)如果把这个注解添加到方法上面,为这个方法添加事务

 

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


<!--    开启组件扫描-->
    <context:component-scan base-package="com.orzjiangxiaoyu.spring"></context:component-scan>

    <!--  数据库连接池  -->
    <context:property-placeholder location="mysqljdbc.properties"></context:property-placeholder>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
    </bean>
    <!-- JdbcTemplate对象   -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
        <!-- 注入dataSource   -->
        <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:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
package com.orzjiangxiaoyu.spring.service;

import com.orzjiangxiaoyu.spring.dao.BankDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author orz
 * @create 2020-08-18 16:32
 */
@Service
@Transactional//添加事务注解
public class BankService {

    @Autowired
    private BankDao bankDao;

    //转账方法

    //声明式
    public void accountMoney()
    {
        //少钱操作
        bankDao.reduceMoney();

        //手动抛出异常
        System.out.println(1/0);

        //多钱操作
        bankDao.addMoney();
    }


}

测试

package com.orzjiangxiaoyu.spring.testdemo;

import com.orzjiangxiaoyu.spring.service.BankService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * @author orz
 * @create 2020-08-18 16:49
 */
public class Test1 {
    @Test
    public void test1()
    {
        ApplicationContext context=new ClassPathXmlApplicationContext("jdbcbean.xml");
        BankService bankService = context.getBean("bankService", BankService.class);
        bankService.accountMoney();


    }
      
}

 

posted @ 2020-08-19 11:07  orz江小鱼  阅读(378)  评论(0编辑  收藏  举报