spring事务管理

1.直接上图

在这里插入图片描述

实现事务管理有2种方式:

1.声明式(基于aop),下面会赋上代码(有种常见的2种声明式,一种是xml配置文件,一种是注解)
2.编程式

1.基于xml的声明式事务管理

xml配置文件:

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

<!--    配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
                  value="jdbc:mysql://localhost:3306/hjx_jdbc?serverTimezone=GMT" />
        <property name="username" value="root" />
        <property name="password" value="123456" />
    </bean>
<!--    配置jdbc模板-->
    <!-- 配置Spring的jdbcTemplate
        并注入一个dataSource数据源-->
    <bean id="jdbcTemplate"
          class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!--   配置注入类-->
    <bean id="userService" class="com.jdbc.userImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

<!--    事务管理器-->
    <!-- 配置事务管理器,事务依赖于数据源所产生的连接对象,只有连接对象创建成功了才能够处理事务 -->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!--    编写通知,对事务进行增强-->
    <tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
        <tx:attributes>
            <!-- 给切入点方法添加事务详情,name表示方法名称,*表示任意方法名称,propagation用于设置传播行为,read-only表示隔离级别,是否只读 -->
            <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"
                       read-only="false" />
        </tx:attributes>
    </tx:advice>

<!--    编写AOP,切入点-->
    <!-- aop编写,让Spring自动对目标生成代理,需要使用AspectJ的表达式 -->
    <aop:config>
        <!-- 切入点 -->
        <aop:pointcut expression="execution(* com.jdbc.*.*(..))"
                      id="txPointCut" />
        <!-- 切面:将切入点与通知整合 -->
        <aop:advisor pointcut-ref="txPointCut" advice-ref="txAdvice" />
    </aop:config>


    <!--    使用context命名空间开启注解支持-->
    <context:annotation-config />
<!--    使用包扫描器 自动把类交由spring 容器管理-->
    <context:component-scan base-package="com.jdbc" />



</beans>

删除事务

  // 事务管理  对数据进行删除
    public int tranferUser_Del(int id) {
        String sql="delete from user where id=?";
        Object[] obj=new Object[]{
                id
        };
        int count=this.jdbcTemplate.update(sql,obj);
        // 如果事务管理成功,因为下一行出错则数据不会删除成功
        int i=1/0;
        return count;
    }

单元测试

 @Test
    public void transfer(){
        int count=user.tranferUser_Del(3);
        if(count>=0){
            System.out.println("事务处理成功!");
        }
        else{
            System.out.println("事务处理失败");
        }
    }

2.声明式注解管理事务

只需要做2件事情

1.在spring 容器中配置事务注解驱动

2.在需要使用事务管理的bean类或者方法上添加@Transactional注解。
可以通过对注解Transactional进行参数设置以配置事务详情。

注意:xml配置文件中依然要配置事务管理器(依赖于数据源)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值