Spring--Spring5事物管理(1)

「这是我参与2022首次更文挑战的第15天,活动详情查看:2022首次更文挑战

五、事务管理

5.1 事务操作(事务概念)

5.1.1 什么是事物?

事物是数据库操作最基本单元,逻辑上一组操作,要么都成功要么都失败,如果有一个失败所有操作都失败

5.1.2 事物的特性(ACID)

(1)原子性(Atomicity)

一组事物要么都成功要么都失败

(2)一致性(Consistency)

操作之前和操作之后它的总量是不变的

(3)隔离性(Isolation)

即一个事务内部的操作及正在操作的数据必须封锁起来,不被其它企图进行修改的事务看到。

两人同时去操作同一条数据,这个过程是不会产生影响的

(4)持久性(Durability)

事物提交,表中数据发生变化

5.2 事务操作(Spring 事务管理介绍)

事务添加到JavaEE三层结构里面Service层(业务逻辑层)因为是业务层对数据进行操作

在 Spring进行事务管理操作

(1)有两种方式:编程式事务管理和声明式事务管理(使用)

3、声明式事务管理

(1)基于注解方式(使用)

(2)基于xml 配置文件方式

4、在 Spring进行声明式事务管理,底层使用AOP原理

5、Spring事务管理API

(1)提供一个接口,代表事务管理器,这个接口针对不同的框架提供不同的实现类

image-20220110225351366

5.3 事务操作(注解声明式事务管理)

5.3.1 在spring配置文件配置事务管理器

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

5.3.2 在 spring 配置文件,开启事务注解

(1)在 spring 配置文件引入名称空间 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: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.caq"></context:component-scan>


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

复制代码

(2)开启事务注解

<!--    开启事物注解-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
复制代码

5.3.3 在 service 类上面(或者 service 类里面方法上面)添加事务注解

@Transactional,这个注解添加到类上面,也可以添加方法上面

如果把这个注解添加类上面,这个类里面所有的方法都添加事务

如果把这个注解添加方法上面,为这个方法添加事务

Guess you like

Origin juejin.im/post/7062164627592839176
Recommended