SpringBoot的XML和注解实现事务

SpringBoot事务管理
本文介绍SpringBoot中两种事务管理方式:注解方式和XML方式。注解方式通过在Service类或方法上添加@Transactional注解实现;XML方式则通过定义XML文件配置事务管理策略。

1、注解方式

1.1、主启动类

加注解@EnableTransactionManagement
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@SpringBootApplication
@MapperScan("cn.yx.zg.mapper")
public class HelloWorldApplication{
    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }
}

## 1.2、service类

在service类上加注解@Transactional(rollbackFor = Exception.class),表示该方法开启事务。 如果把注解加到类上,表示该类的所有方法开启事务。

@Service("userService")
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
    @Override
    public int inserUser(User user) {
        return userMapper.inserUser(user);
    }
}

2、XML方式

虽然注解方式实现了SpringBoot的事务管理,很简单吧。 但是烦人的是,需要在每个service类上或者方法上加上@Transactional注解,让我感觉非常不友好。如果一个新手程序员,不知道@Transactional注解是干啥的,所以我干脆用XML配置一次,不让他们关注事务了。感觉还是不错的。

2.1、主启动类

加注解@EnableTransactionManagement

2.2、transaction.xml详解

新建transaction.xml, 放在resource目录下。

<?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: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/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">

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

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!--isolation="DEFAULT"隔离级别
            read-only 只读(提示数据库驱动程序和数据库系统,这个事务并不包含更改数据的操作)
            rollback-for 发生这些异常回滚
            no-rollback-for 发生这些异常不回滚
         -->
        <tx:method name="query*"  propagation="SUPPORTS" read-only="true"></tx:method>
        <tx:method name="get*" propagation="SUPPORTS" read-only="true"></tx:method>
        <tx:method name="select*" propagation="SUPPORTS" read-only="true"></tx:method>
        <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" ></tx:method>
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:pointcut id="allManagerMethod" expression="execution (* cn.yx.zg.service.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" order="0"/>
</aop:config>
</beans>
1、 name: 指定切入点方法,属于之前配置的切入点表达式;可以使用通配符进行匹配。
2、 isolation: 用于指定事务的隔离级别。默认值是 DEFAULT ,表示使用数据库的默认隔离级别。
3、 propagation: 用于指定事务的传播行为。默认值是 REQUIRED 表示一定会有事务,增删改的选择。查询方法可以选择 SUPPORTS。
4、 read-only: 用于指定事务是否只读。只有查询方法才能设置为 true .默认值是 false ,表示读写。
5、 timeout: 用于指定事务的超时时间,默认值是 -1,表示永不超时。如果指定了数值,以秒为单位。
6、 rollback-for: 用于指定一个异常,当产生该异常时,事务回滚;产生其他异常时,事务不会滚;没有默认值(设置值),任何异常都会回滚。
7、 no-rollback-for: 用于指定一个异常,当产生该异常时,事务不会滚;产生其他异常时事务回滚;没有默认值(设置值),任何异常都会回滚

2.3、加载transaction.xml

启动类新加@ImportResource

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@MapperScan("cn.yx.zg.mapper")
@SpringBootApplication
@ImportResource("classpath:transaction.xml")
@EnableTransactionManagement
public class HelloWorldApplication {

    public static void main(String[] args) {
        // 启动spring 应用
        SpringApplication.run(HelloWorldApplication.class, args);
    }
}

如果需要了解事务隔离级别相关概念,请参考:
事务隔离级别,Spring、SpringBoot事务传播行为

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北漂IT民工_程序员_ZG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值