溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用springboot如何實現(xiàn)開啟聲明式事務(wù)

發(fā)布時間:2020-11-18 16:12:56 來源:億速云 閱讀:206 作者:Leah 欄目:編程語言

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)使用springboot如何實現(xiàn)開啟聲明式事務(wù),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

springboot開啟事務(wù)很簡單,只需要一個注解@Transactional 就可以了。因為在springboot中已經(jīng)默認對jpa、jdbc、mybatis開啟了事事務(wù),引入它們依賴的時候,事物就默認開啟。當(dāng)然,如果你需要用其他的orm,比如beatlsql,就需要自己配置相關(guān)的事物管理器。

基于xml的來實現(xiàn),并開啟聲明式事務(wù)。

環(huán)境依賴

在pom文件中引入mybatis啟動依賴:

<dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.0</version>
</dependency>

引入MySQL 依賴

<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.29</version>
    </dependency>

初始化數(shù)據(jù)庫腳本

-- create table `account`
# DROP TABLE `account` IF EXISTS
CREATE TABLE `account` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(20) NOT NULL,
 `money` double DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `account` VALUES ('1', 'aaa', '1000');
INSERT INTO `account` VALUES ('2', 'bbb', '1000');
INSERT INTO `account` VALUES ('3', 'ccc', '1000');

配置數(shù)據(jù)源

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath*:mybatis/*Mapper.xml
mybatis.type-aliases-package=com.forezp.entity

通過配置mybatis.mapper-locations來指明mapper的xml文件存放位置,我是放在resources/mybatis文件下的。mybatis.type-aliases-package來指明和數(shù)據(jù)庫映射的實體的所在包。

經(jīng)過以上步驟,springboot就可以通過mybatis訪問數(shù)據(jù)庫來。

創(chuàng)建實體類

public class Account {
  private int id ;
  private String name ;
  private double money;

  getter..
  setter..

 }

數(shù)據(jù)訪問dao 層

接口:

public interface AccountMapper2 {
  int update( @Param("money") double money, @Param("id") int id);
}

mapper:

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.forezp.dao.AccountMapper2">


  <update id="update">
    UPDATE account set money=#{money} WHERE id=#{id}
  </update>
</mapper>

service層

@Service
public class AccountService2 {

  @Autowired
  AccountMapper2 accountMapper2;

  @Transactional
  public void transfer() throws RuntimeException{
    accountMapper2.update(90,1);//用戶1減10塊 用戶2加10塊
    int i=1/0;
    accountMapper2.update(110,2);
  }
}

@Transactional,聲明事務(wù),并設(shè)計一個轉(zhuǎn)賬方法,用戶1減10塊,用戶2加10塊。在用戶1減10 ,之后,拋出異常,即用戶2加10塊錢不能執(zhí)行,當(dāng)加注解@Transactional之后,兩個人的錢都沒有增減。當(dāng)不加@Transactional,用戶1減了10,用戶2沒有增加,即沒有操作用戶2 的數(shù)據(jù)??梢夽Transactional注解開啟了事物。

上述就是小編為大家分享的使用springboot如何實現(xiàn)開啟聲明式事務(wù)了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI