1、注解方式
1.1、主启动类
加注解@EnableTransactionManagementimport 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、主启动类
加注解@EnableTransactionManagement2.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事务传播行为