软件编程
位置:首页>> 软件编程>> java编程>> spring中的注解事务演示和添加步骤详情

spring中的注解事务演示和添加步骤详情

作者:是庸医啊  发布时间:2023-03-03 08:32:48 

标签:spring,注解,事务,演示

添加注解效果事务演示

注解我们经常会用到,或者在jdk源码中也会看到,例如: @Deprecated
以及我们在spring或者springboot中经常用到@Controller、@Service、@Repository、@Entity等注解。

spring中的注解事务演示和添加步骤详情

spring中的注解事务演示和添加步骤详情

spring中的注解事务演示和添加步骤详情

spring中的注解事务演示和添加步骤详情

spring中的注解事务演示和添加步骤详情

spring中的注解事务演示和添加步骤详情

spring中的注解事务演示和添加步骤详情

基于注解的事务添加步骤

1)在applicationContext_service.xml文件中添加事务管理器

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <!--因为事务必须关联数据库处理,所以要配置数据源-->
       <property name="dataSource" ref="dataSource"></property>
    </bean>

2)在applicationContext_service.xml文件中添加事务的注解驱动

<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

3)在业务逻辑的实现类上添加注解@Transactional(propagation = Propagation.REQUIRED)
REQUIRED表示增删改操作时必须添加的事务传播特性

添加不回滚属性设置

spring中的注解事务演示和添加步骤详情

spring中的注解事务演示和添加步骤详情

@Transactional注解参数详解

spring中的注解事务演示和添加步骤详情

 @Transactional(propagation = Propagation.REQUIRED,//事务的传播特性
            noRollbackForClassName = "ArithmeticException", //指定发生什么异常不回滚,使用的是异常的名称
            noRollbackFor = ArithmeticException.class,//指定发生什么异常不回滚,使用的是异常的类型
            rollbackForClassName = "",//指定发生什么异常必须回滚
            rollbackFor = ArithmeticException.class,//指定发生什么异常必须回滚
            timeout = -1, //连接超时设置,默认值是-1,表示永不超时
            readOnly = false, //默认是false,如果是查询操作,必须设置为true.
            isolation = Isolation.DEFAULT//使用数据库自已的隔离级别        
    )

来源:https://blog.csdn.net/weixin_48826996/article/details/126002987

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com