Spring5中用注解方式声明事物管理(@Transaction的具体使用)

Service: 业务操作  创建转账的方法,调用dao两个方法

Dao:数据库操作,不写主要代码,  创建两个方法(少钱、多钱)

Spring事务管理API:提供一个接口,代表事物管理器,这个接口针对不同的矿建提供不同的实现类

我们此处要用的实现类:DataSourceTransationManager

第一步: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: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 http://www.springframework.org/schema/context/spring-context.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">
    <!--    开启扫描-->
    <context:component-scan base-package="com.company.Spring5.Shiwu"> </context:component-scan>

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="url" value="jdbc:mysql://localhost:3306/user_db" />
        <property name="username" value="root" />
        <property name="password" value="admin" />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
        <!--用set方法注入 dataSource属性-->
    </bean>

    <!--创建事物管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--开启事物注解-->
<!--  transaction-manager后面的值,就是我们要开启的书屋管理器的id值  -->
         <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

第二步:创建service、搭建dao,完成对象创建和注入关系

package com.company.Spring5.Shiwu.dao;

public interface UserDao {
//多钱的方法
    void addMoney();
//    少钱的方法
    void reduceMoney();
}

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao{
//注入JdbcTemplate对象
    @Autowired  //根据类型注入  此类型在配置文件中写过
    private JdbcTemplate jdbcTemplate;

//实现Lucy转账100给Mary
    @Override//多钱的方法
    public void addMoney() {
        String sql = "update t_account set money=money+? where username=? ";
        jdbcTemplate.update(sql,100,"mary");
    }

    @Override//少钱的方法
    public void reduceMoney() {
        String sql = "update t_account set money=money-? where username=?";
        jdbcTemplate.update(sql,100,"lucy");
    }
}

import com.company.Spring5.Shiwu.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


//如果把这个注解加到类上面,表示类中的所有方法都添加事物
//如果把这个注解添加到方法上面,仅仅是为这个方法添加事物
@Service
@Transactional
public class UserService {
//    注入dao对象
    @Autowired  // 根据类型注入
    private UserDao userDao;

//    转账的操作
    public void accountMoney(){
//        lucy要少一百
        userDao. reduceMoney();
//        手动模拟异常
//    int i = 10/0;
//        Mary要多一百
        userDao.addMoney();
    }
}

测试类:

import com.company.Spring5.Shiwu.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    @org.junit.Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean13Shiwu.xml");
        UserService userService = context.getBean("userService",UserService.class);

        userService.accountMoney();
    }
}

运行结果:

 

当我们把手动异常添加进去之后,运行结果:

出现异常,数据库中的数据没有改变 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱布朗熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值