开启声明式事务
1.在启动类上增加注解来
@EnableTransactionManagement
2.在需要使用事务的方法上增加注解
@Transactional
注解的参数说明
value/transactionManager 传递事务管理器 接受string类型数据 事务管理器一般在在我们使用数据源并自定义配置的注入的时候,需要注入 数据源、数据源池对象、和事务管理器,事务管理器就是从这里注入(自动配置的时候就是spring帮忙注入的了)
propation 事务传播的机制 接受Propagation类型数据 默认传播机制是Propagation.REQUIRED /** * Enumeration that represents transaction propagation behaviors for use * with the {@link Transactional} annotation, corresponding to the * {@link TransactionDefinition} interface. * 该枚举类作为向Transactional 注解传递的参数,主要决定事务的传播机制 * @author Colin Sampaleanu * @author Juergen Hoeller * @since 1.2 */ public enum Propagation { /** *支持现有的事务,如果没有事务就创建新的事务 * Support a current transaction, create a new one if none exists. * Analogous to EJB transaction attribute of the same name. * <p>This is the default setting of a transaction annotation. 是默认的事务传递机制 */ REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED), /** *支持当前已经存在的事务,如果没有事务就以非事务的方式来执行 * Support a current transaction, execute non-transactionally if none exists. * 和EJB的同名事务属性相似 * Analogous to EJB transaction attribute of the same name. *注意supports的事务传播机制和完全没有事务的执行是不同的,因为它定义了需要同步的事务范围 *因此,相同的资源将共享整个需要同步的事务范围。注意,