KKB :spring事务的配置和注解事务

不使用注解事务的配置事务

1、spring配置文件需要用到的标签 bean、context、aop、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:p="http://www.springframework.org/schema/p"
       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">

2、需要的pom依赖

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>

3、spring的配置文件基础配置信息

配置数据库源的配置文件信息,

配置ComboPooledDataSource的信息,

以及配置实现service接口的信息,这里是省略了service层,只使用了dao层,建议在service层中做逻辑处理

    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driver}"></property>
        <property name="jdbcUrl" value="${url}"></property>
        <property name="user" value="${username1}"></property>
        <property name="password" value="${password}"></property>
    </bean>
    <bean id="gdao" class="com.atshiyou.dao.impl.GradeDaoImpl">
        <property name="dataSource" ref="ds"></property>
    </bean>

配置事务的三大步骤

第一步:创建 平台事务管理器,需要的class是 DataSourceTransactionManager

用到的参数是dataSource,和dao层的对象创建一样

第二步:指定哪些方法需要进行事务管理

这里的 tx:advice ,使用到的参数包含了第一步的平台事务管理器 

REQUIRED :如果有事务,则在事务中执行;如果没有事务,则开启另一个新事务(默认)

第三步:使用面向切面的思想指定哪层需要事务管理,事务添加在service层最合理

这里用到的是aop的标签,指明了切面,里面的参数用的是特殊的前置增强方式aop:advisor

测试结果符合事务的特征

当使用了注解的方式

只需要修改配置文件,spring的基本配置不变,有数据源的基本配置ComboPooledDataSource,以及dao类中使用到的数据源对象

<?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:p="http://www.springframework.org/schema/p"
       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:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driver}"></property>
        <property name="jdbcUrl" value="${url}"></property>
        <property name="user" value="${username1}"></property>
        <property name="password" value="${password}"></property>
    </bean>
    <bean id="gdao" class="com.atshiyou.dao.impl.GradeDaoImpl">
        <property name="dataSource" ref="ds"></property>
    </bean>

还是分三步

第一步:创建 平台事务管理器,需要的class是 DataSourceTransactionManager

用到的参数是dataSource,和dao层的对象创建一样

第二步:启用注解事务

第三步:加注解

@Transactional

测试结果符合事务的特征

猜你喜欢

转载自blog.csdn.net/awodwde/article/details/112853991