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

 

测试结果符合事务的特征

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

你在狗叫什么、

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

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

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

打赏作者

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

抵扣说明:

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

余额充值