Spring-Transactions-基础入门级介绍

1.声明式事务的简单介绍:

1.1、编程式事务:

TranscationFilter{
    try{
        //获取连接
        //设置非自动提交
        chain.doFilter();
        //提交
    }catch(Exception e){
        //回滚
    }finally{
        //关闭连接,释放资源
    }
}

1.2、声明式事务:

  • 以前通过复杂的编程来编写一个事务,替换为只需要告诉Spring哪个方法是事务方法即可。Spring自动进行事务控制。
  • 主要原理还是AOP环绕通知的原理。只需要一个注解。
  • 事务管理代码的固定模式作为一种横切关注点,可以通过AOP方法模块化,进而借助Spring AOP框架实现声明式事务管理。但是自己写切面还是很麻烦的。这个切面已经有了,在Spring中称为事务切面、事务管理器。PlatFormTransactionManager,平台事务管理器,是一个接口。这个事务管理器就可以在目标方法运行前后进行事务控制(事务切面)。

1.3、快速的为某个方法添加事务:(以DataSourceTransactionManager为例):

1)、配置出这个事务管理器让其进行事务控制:

  • 注意:头名称空间的配置:
xmlns:tx="http://www.springframework.org/schema/tx"
<?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"
       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">

    <context:component-scan base-package="com.atguigu"></context:component-scan>

    <context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>
    <!-- 配置数据源 -->
    <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
    </bean>

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

    <!-- 事务控制   -->
    <!-- 1.配置事务管理器(切面)让其进行控制,所以一定要导入面向切面的那几个包
     com.springsource.net.sf.cglib-2.2.0.jar
    com.springsource.org.aopalliance-1.0.0.jar
    com.springsource.org.aspectj.weaver-1.6.4.RELEASE.jar
    spring-aspects-5.2.3.RELEASE.jar
     -->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 控制数据源 -->
        <property name="dataSource" ref="comboPooledDataSource"></property>
    </bean>
    <!-- 2.开启基于注解的事务控制模式:依赖tx名称空间 -->
    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>
    <!-- 3.给事务方法加注解即可 -->

</beans>
  • 最后只要在你写的事务方法上加上@Transactional就可以了:
    在这里插入图片描述

2.声明式事务-环境搭建-初步测试:

2.1、先创建数据库和三张表:

在这里插入图片描述

  • 注意几个依赖包的导入(我是用c3p0的数据池):
    在这里插入图片描述

2.2、写几个类和方法,模拟结账操作:

在这里插入图片描述

package com.atguigu.dao;

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

@Repository
public class BookDao {

    @Autowired
    JdbcTemplate jdbcTemplate;
    /**
     * 1.减余额:
     *  减去某个用户的余额
     */
    public void updateBalance(String userName, double price){
        String sql = "UPDATE account SET balance = balance - ? WHERE username = ?";
        jdbcTemplate.update(sql, price, userName);
    }

    /**
     * 2.通过图书的isbn来获取某本图书的价格
     * @return
     */
    public Double getPrice(String isbn){
        String sql = "SELECT price FROM book WHERE isbn = ?";
        return jdbcTemplate.queryForObject(sql, Double.class, isbn);
    }

    /**
     * 3.减库存,减去某本书的库存;为了简单起见,每次减一
     */
    public void updateStock(String isbn){
        String sql = "UPDATE book_stock SET stock = stock-1 WHERE isbn = ?";
        jdbcTemplate.update(sql, isbn);
    }

}
package com.atguigu.service;

import com.atguigu.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookService {

    @Autowired
    BookDao bookDao;
    /**
     * 结账方法:
     */
    public void checkout(String username, String isbn){
//        第一步:减库存
        bookDao.updateStock(isbn);
//        第二步:减余额,先查询图书价格
        Double price = bookDao.getPrice(isbn);
        bookDao.updateBalance(username, price);
    }
}
<?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"
       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">

    <context:component-scan base-package="com.atguigu"></context:component-scan>

    <context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>
    <!--    配置数据源-->
    <bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
    </bean>

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

</beans>
jdbc.user=root
jdbc.password=root
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/tx
jdbc.driverClass=com.mysql.jdbc.Driver
import com.atguigu.service.BookService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TxTest {

    ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-config.xml");

    @Test
    public void test(){
        BookService bookService = ioc.getBean(BookService.class);
        bookService.checkout("Tom","ISBN-001");
        System.out.println("结账完成!");
    }
}

盖木欧瓦~

发布了52 篇原创文章 · 获赞 1 · 访问量 2239

猜你喜欢

转载自blog.csdn.net/Shen_R/article/details/105216907
今日推荐