spring-210729-03---事务操作-Spring声明式事务管理-注解方式

spring-210729-03—事务操作-Spring声明式事务管理-注解方式


1. 在Spring配置文件,配置事务管理器
2. 在Spring配置文件,开启事务注解
	引入名称空间	xmlns:tx="http://www.springframework.org/schema/tx"
	开启事务注解
3. 在service类上面添加事务注解(或者service类里方法上面添加事务注解),@Transactional
	事务注解@Transactional在类上面,表示把这个类中所有方法都添加该注解。
	事务注解@Transactional在方法上面,表示为这个方法添加事务。

演示

UserDao02.java

package com.bgy.spring.dao;

public interface UserDao02 {

    // 多钱
    void addMoney();

    // 少钱
    void reduceMoney();
}

UserDao02.java

package com.bgy.spring.dao;

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

@Repository
public class UserDaoImpl02 implements UserDao02{

    // 注入JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 多钱
     */
    @Override
    public void addMoney() {
        String sql = "update t_account set money=money-? where id=?";

        int update = jdbcTemplate.update(sql, "100","1001");
        System.out.println(update);
    }

    /**
     * 少钱
     */
    @Override
    public void reduceMoney() {
        String sql = "update t_account set money=money+? where id=?";

        int update = jdbcTemplate.update(sql, "100", "1002");
        System.out.println(update);
    }
}

UserService02.java(在这儿开启事务)

package com.bgy.spring.service;

import com.bgy.spring.dao.UserDao02;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional  // 事务注解
public class UserService02 {

    // 注入UserDao
    @Autowired
    private UserDao02 userDao02;

    /**
     * 转账
     */
    public void accountMoney(){
        userDao02.reduceMoney();

        // 手动创建一个异常,测试事务
        int a=10/0;

        userDao02.addMoney();
    }
}

bean02.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.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">


    <!-- 开启组件扫描 -->
    <context:component-scan base-package="com.bgy.spring"></context:component-scan>


    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/spring_test_210728_01?serverTimezone=UTC&amp;useSSL=false" />
        <property name="username" value="root" />
        <property name="password" value="admin" />
    </bean>


    <!-- 创建JdbcTemplate对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- 注入DataSource -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>


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


    <!-- 开启事务注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

TestUser02.java

import com.bgy.spring.service.UserService02;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestUser02 {

    /**
     * 测试转账功能
     */
    @Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean02.xml");

        UserService02 userService02 = context.getBean("userService02", UserService02.class);
        userService02.accountMoney();
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值