spring 注解实现事务

6 篇文章 0 订阅


前言

提示:spring 实现事务有两种方式(XML和注解)。


一、注解实现过程

与XML实现的方式差不多,只是在使用事务时有一定的不同。

二、使用步骤

1.引入需要的包

代码如下(示例):

<!-- 引入JDBC -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.3.6</version>
    </dependency>
    <!-- 连接池 -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-dbcp2</artifactId>
      <version>2.8.0</version>
    </dependency>

2.配置XMR注入

代码如下(示例):

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

    <context:property-placeholder location="classpath:common.properties"/>

    <!--配置数据库连接信息,需要引入dbcp架包-->
    <bean id="dataSource" class="${dataSource}">
        <property name="driverClassName" value="${driverClass}"></property>
        <property name="url" value="${jdbcUrl}"></property>
        <property name="username" value="${user}"></property>
        <property name="password" value="${password}"></property>
    </bean>

    <!-- 配置JdbcTemplate -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置实体类 -->
    <bean id="UserTmpl" class="org.example.mysql.service.tmpl.UserTmpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <tx:annotation-driven transaction-manager="txManager"/>
    <!-- 配置事务通知 -->
    <!--<tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            &lt;!&ndash; 根据方法名指定事务的属性  &ndash;&gt;
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>-->
    <!-- 配置事务切入点,以及把事务切入点和事务属性关联起来 -->
    <!--<aop:config>
        <aop:pointcut expression="execution( * org.example.mysql.service.tmpl.UserTmpl.*(..))"   id="txPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>-->
</beans>

注释掉的xml配置即为xml实现事务的区别

3.实现类

代码如下(示例):

 @Transactional(readOnly = false,propagation = Propagation.REQUIRED,rollbackFor = {SQLException.class,RuntimeException.class})
    @Override
    public int updateUser(Users users,long userId)  {
        int update;
        //修改数据
        String sql = " update  te_users set integral =integral+1 where user_id= ?";
        Object[] params = new Object[]{userId};
        update =  jdbcTemplate.update(sql, params);

        sql = " update  te_users set balance =?  where user_id= ?";
        params = new Object[]{users.getToken(),userId};
        jdbcTemplate.update(sql, params);
        return update;
    }

主要是对@Transactional注解的使用,涉及事务的传播行为和隔离级别


总结

与xml实现事务的区别在于xml文件配置不需要配置切入路径的包,用注解替代xml实现事务的隔离级别设置,传播设置和回滚条件,在开发中更加方便灵活,易于维护项目代码,没有太多的xml配置
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值