zoukankan      html  css  js  c++  java
  • Spring基于注解的事务控制

    准备jar包
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>

    开始配置
    <?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
          https://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context
          https://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/tx
          https://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--配置扫描路径-->
    <context:component-scan base-package="com.aiitec"></context:component-scan>

    <!--事务控制begin-->
      <!--配置事务管理器-->
      <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
      </bean>
      <!--开启Spring对注解事务的支持-->
      <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    <!--事务控制end-->

    <!--配置spring-jdbc-template数据源 begin-->
      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/eesy"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
      </bean>

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

    在Service的实现类中使用@Transactional注解
    事务的属性都改为在@Transactional(这里)配置,
    用在类上,该类的所有public方法都将支持事务
    方法上的@Transactional优先级较

  • 相关阅读:
    AngularJS 1.x系列:AngularJS过滤器(4)
    AngularJS 1.x系列:AngularJS控制器(3)
    AngularJS 1.x系列:AngularJS简介及第一个应用(2)
    AngularJS 1.x系列:Node.js安装及npm常用命令(1)
    MySQL系列:索引基本操作(4)
    基于 Mathematica 的机器人仿真环境(机械臂篇)[转]
    使用耳切法将多边形三角化【转】
    asp.net调用非托管dll,无法加载 DLL,找不到指定模块解决方法。【转】
    ASP.NET与非托管DLL的那些事儿【转+增】
    cesium常用设置【转】
  • 原文地址:https://www.cnblogs.com/zou-rong/p/12072116.html
Copyright © 2011-2022 走看看