Spring框架事务处理


文章目录

  • 什么是事务
          • 1.什么是事务
          • 2.在什么时候想到使用事务
          • 3.你的业务方法需要什么样的事务,说明需要事务的类型。
            • 事务的隔离级别:
            • 事务的超时时间
            • 事务的传播行为
          • 4.事务提交事务,回滚事务的时机
  • Spring中使用事务处理步骤
      • Step0:创建数据库表
      • Step1: maven 依赖 pom.xml
      • Step2:创建实体类
      • Step3:定义 dao 接口
      • Step4:定义 dao 接口对应的 sql 映射文件
          • 定义异常类(不是必须的)
      • Step5:创建mybatis主配置文件
      • Step6:定义 Service 接口
      • Step7:定义 service 的实现类
      • Step8:创建 Spring 配置文件内容
        • 1. 使用@Transactional的步骤(中小项目适用):
            • 1).需要声明事务管理器对象
            • 2).开启事务注解驱动, 告诉spring框架,我要使用注解的方式管理事务。
            • 3).在你需要做事务的方法的上面加入@Trancational注解
        • 2.使用aspectj框架 (大型项目使用)
            • 1)maven 依赖 pom.xml
            • 2)在容器中添加事务管理器
            • 3)配置事务通知
            • 4)配置增强器
      • Step9:方法测试

aspectj框架中有介绍


通过aspectj框架实现事务处理的完整spring配置文件

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


    
    <context:property-placeholder location="classpath:jdbc.properties" />

    
    <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="${jdbc.maxActive}"/>
    bean>



    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="myDataSource"/>
        
        <property name="configLocation" value="springtrans.xml"/>

    bean>



    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        
        <property name="basePackage" value="com.changsha.dao"/>
    bean>



    
    <bean id="buyService" class="com.changsha.service.Impl.BuyGoodsServiceImpl">
        <property name="goodsDao" ref="goodsDao"/>
        <property name="saleDao" ref="saleDao"/>
    bean>


    
    <bean id="tansactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="myDataSource"/>
    bean>


    

    
    <tx:advice id="myAdvice" transaction-manager="tansactionManager">
        
        <tx:attributes>
            
            <tx:method name="buy" propagation="REQUIRED" isolation="DEFAULT"
                       rollback-for="java.lang.NullPointerException,com.bjpowernode.excep.NotEnoughException"/>

            
            <tx:method name="add*" propagation="REQUIRES_NEW" />
            
            <tx:method name="modify*" />
            
            <tx:method name="remove*" />
            
            <tx:method name="*" propagation="SUPPORTS" read-only="true" />
        tx:attributes>
    tx:advice>



    
    <aop:config>
        <aop:pointcut id="servicePt" expression="execution(* *..service..*.*(..))"/>
        <aop:advisor advice-ref="myAdvice" pointcut-ref="servicePt" />
    aop:config>

beans>

Step9:方法测试

package com.changsha;

import com.changsha.service.BuyGoodsService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest {
    @Test
    public void test(){
        String config = "applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        BuyGoodsService buyGoodsService = (BuyGoodsService) ctx.getBean("buyService");
        //变成了动态代理的对象
        System.out.println(buyGoodsService.getClass().getName());
        buyGoodsService.buy(1001,10);
    }
}