三,搭建环境:事务控制

三,搭建环境:事务控制


声明式事务配置

demo-module01-web 的模块下的,spring-persist.xml 配置文件中

开启基于注解的声明式事务支持

在这里插入图片描述

   <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 装配数据源 -->
        <property name="dataSource" ref="druidDataSource"/>
    </bean>

    <!-- 配置事务的注解驱动,开启基于注解的声明式事务功能 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!--    开启基于注解的声明式事务支持-->
    <!-- 配置事务的注解驱动,开启基于注解的声明式事务功能 -->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<?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:mybatis="http://mybatis.org/schema/mybatis-spring" 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 https://www.springframework.org/schema/context/spring-context.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--    加载外部配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!--    配置数据源-->
    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.passwords}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="initialSize" value="${jdbc.initialSize}"/>
        <property name="maxActive" value="${jdbc.maxActive}"/>
        <property name="maxWait" value="${jdbc.maxWait}"/>
    </bean>

<!--    配置 SqlSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--        指定 Mapper 配置文件的位置 , Set注入 /mapper 下的所有文件-->
        <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"></property>
<!--        装配数据源-->
        <property name="dataSource" ref="druidDataSource"></property>
    </bean>

<!--    配置 Mapper 接口的扫描-->
    <mybatis:scan base-package="com.rainbowsea.imperial.court.mapper"></mybatis:scan>


    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 装配数据源 -->
        <property name="dataSource" ref="druidDataSource"/>
    </bean>


    <!-- 配置事务的注解驱动,开启基于注解的声明式事务功能 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>


</beans>

如果报错:注意命名空间上的添加。

在这里插入图片描述

注解写法

查询操作

@Transactional(readOnly = true)

增删改操作

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)

在具体代码开发中可能会将相同设置的 @Transactional 注解提取到 Service 类上。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值