最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatisPlus+Spring實現(xiàn)聲明式事務(wù)的方法實現(xiàn)

 更新時間:2024年07月04日 11:16:28   作者:二十多歲想退休  
本文主要介紹了MyBatisPlus+Spring實現(xiàn)聲明式事務(wù)的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

事務(wù)介紹

數(shù)據(jù)庫的事務(wù)(Transaction)是一種機制、一個操作序列,包含了一組數(shù)據(jù)庫操作命令。事務(wù)把所有的命令作為一個整體一起向系統(tǒng)提交或撤銷操作請求,即這一組數(shù)據(jù)庫命令要么都執(zhí)行,要么都不執(zhí)行,因此事務(wù)是一個不可分割的工作邏輯單元。

在數(shù)據(jù)庫系統(tǒng)上執(zhí)行并發(fā)操作時,事務(wù)是作為最小的控制單元來使用的,特別適用于多用戶同時操作的數(shù)據(jù)庫系統(tǒng)。例如,航空公司的訂票系統(tǒng)、銀行、保險公司以及證券交易系統(tǒng)等。

情景模擬

業(yè)務(wù):轉(zhuǎn)賬業(yè)務(wù)
角色:轉(zhuǎn)賬方、收款方
完成事物:轉(zhuǎn)賬過程中出現(xiàn)意外時,將數(shù)據(jù)庫回滾

一、新建數(shù)據(jù)庫

對于精度比較高的東西,比如money,建議使用decimal類型,不要考慮float,double, 因為他們?nèi)菀桩a(chǎn)生誤差,numeric和decimal同義,numeric將自動轉(zhuǎn)成decimal。

對于精度比較高的東西,比如money,建議使用decimal類型,不要考慮float,double, 因為他們?nèi)菀桩a(chǎn)生誤差,numeric和decimal同義,numeric將自動轉(zhuǎn)成decimal。

設(shè)置兩個角色,A是借款方,B是收款方

二、新建Spring項目

三、在pom.xml中導(dǎo)入依賴

導(dǎo)入MyBatisPlus相關(guān)依賴

				<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

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

根據(jù)自己的情況配置

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/jdbctest?useSSL=true&useUnicode=true&characterEncoding=utf-8
    username: root
    password: 123

五、創(chuàng)建實體類

package demo.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

@TableName("acount")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account {
    @TableId(type = IdType.AUTO)
    private Integer id;

    private String name;

    private BigDecimal money; 
}

六、創(chuàng)建Mapper

package demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import demo.entity.Account;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface AccountMapper extends BaseMapper<Account> {
}

七、創(chuàng)建Service

AccountService

package demo.service;

import com.baomidou.mybatisplus.extension.service.IService;
import demo.entity.Account;
import java.math.BigDecimal;

public interface AccountService extends IService<Account> {

    boolean transfer(String source, String target, BigDecimal money);
}

AccountServiceImpl

package demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import demo.entity.Account;
import demo.mapper.AccountMapper;
import demo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {
    @Autowired
    private AccountMapper accountMapper;

    private Integer i1;
    private Integer i2;

    public boolean transfer(String source, String target, BigDecimal money) {
        //獲取匯款方
        QueryWrapper<Account> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("name", source);
        Account one1 = accountMapper.selectOne(wrapper1);

        QueryWrapper<Account> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("name", target);
        Account one2 = accountMapper.selectOne(wrapper2);

        one1.setMoney(one1.getMoney().subtract(money)); //十進制減法
        i1 = accountMapper.updateById(one1);
        

        one2.setMoney(one2.getMoney().add(money)); //十進制加法
        i2 = accountMapper.updateById(one2);
        if (i1 > 0 && i2 > 0) {
            return true;
        }
        return false;
    }
}

TestController

package demo.controller;

import demo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
public class TestController {

    @Autowired
    private AccountService accountService;

    @RequestMapping("/test")
    public String test(){
        String result = null;
        boolean b = accountService.transfer("A", "B", BigDecimal.valueOf(700D));
        if(b){
            result = "轉(zhuǎn)賬成功!";
        } else {
            result = "轉(zhuǎn)賬異常!";
        }
        return result;
    }
}

上圖代碼為正常的轉(zhuǎn)賬過程情況,我通過postman發(fā)送請求時

正常

顯示結(jié)果正常,在數(shù)據(jù)庫中,

數(shù)據(jù)顯示正常

數(shù)據(jù)顯示正常。

八、聲明式事務(wù)的實現(xiàn)

通過@Transactional注解實現(xiàn)事務(wù)的聲明
在service的實現(xiàn)層中,A用戶轉(zhuǎn)出錢后,我添加一個模擬異常 int y = 1/0
通過事務(wù)的聲明,讓數(shù)據(jù)庫實現(xiàn)回滾
將數(shù)據(jù)庫的中的money數(shù)據(jù)初始為1000

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

package com.example.mabatistransaction.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mabatistransaction.Mapper.AccountMapper;
import com.example.mabatistransaction.Service.AccountService;
import com.example.mabatistransaction.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Transactional
    @Override
    public boolean transferTo(String source, String target, BigDecimal money) {
        boolean isOK = false;

        //獲取匯款方
        QueryWrapper<Account> wrapper1 = new QueryWrapper<Account>();
        wrapper1.eq("name", source);
        Account sourcePerson = accountMapper.selectOne(wrapper1);

        //獲取收款方賬戶
        QueryWrapper<Account> wrapper2 = new QueryWrapper<Account>();
        wrapper2.eq("name", target);
        Account tatgetPerson = accountMapper.selectOne(wrapper2);

        //轉(zhuǎn)賬

        //源賬戶取出700
        sourcePerson.setMoney(sourcePerson.getMoney().subtract(money)); //進行十進制的減法
        int a = accountMapper.updateById(sourcePerson);

        //模擬異常
        int y = 1/0;

        //目標賬戶存入700
        tatgetPerson.setMoney(tatgetPerson.getMoney().add(money));  //進行十進制加法
        int b = accountMapper.updateById(tatgetPerson);

        //判斷是否成功
        if(a>0 && b>0){
            isOK=true;
        }

        return isOK;
    }
}

再次通過Postman發(fā)送請求

服務(wù)器異常

顯示服務(wù)器異常,此時因為聲明過事務(wù),所以數(shù)據(jù)庫的內(nèi)容會回滾

數(shù)據(jù)庫回滾

但事務(wù)也有失效的場景,具體的失效場景如下圖所示

在這里插入圖片描述

到此這篇關(guān)于MyBatisPlus+Spring實現(xiàn)聲明式事務(wù)的方法實現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatisPlus Spring聲明式事務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • Java中Lambda表達式之Lambda語法與作用域解析

    Java中Lambda表達式之Lambda語法與作用域解析

    這篇文章主要介紹了Java中Lambda表達式之Lambda語法與作用域解析重點介紹Lambda表達式基礎(chǔ)知識,需要的朋友可以參考下
    2017-02-02
  • Struts2學(xué)習(xí)筆記(7)-訪問Web元素

    Struts2學(xué)習(xí)筆記(7)-訪問Web元素

    這篇文章主要介紹Struts2中訪問Web元素的方法,希望能給大家做一個參考。
    2016-06-06
  • SpringBoot+VUE實現(xiàn)前后端分離的實戰(zhàn)記錄

    SpringBoot+VUE實現(xiàn)前后端分離的實戰(zhàn)記錄

    這篇文章主要介紹了SpringBoot+VUE實現(xiàn)前后端分離的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • java實現(xiàn)下載文件到默認瀏覽器路徑

    java實現(xiàn)下載文件到默認瀏覽器路徑

    這篇文章主要介紹了java實現(xiàn)下載文件到默認瀏覽器路徑,具有很好的參考價值,希望對的大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Java 精煉解讀數(shù)據(jù)結(jié)構(gòu)邏輯控制

    Java 精煉解讀數(shù)據(jù)結(jié)構(gòu)邏輯控制

    在程序開發(fā)的過程之中一共會存在有三種程序邏輯:順序結(jié)構(gòu)、分支結(jié)構(gòu)、循環(huán)結(jié)構(gòu),對于之前所編寫的代碼大部分都是順序結(jié)構(gòu)的定義,即:所有的程序?qū)凑斩x的代碼順序依次執(zhí)行
    2022-03-03
  • Java BigDecimal類用法詳解

    Java BigDecimal類用法詳解

    BigDecimal 由任意精度的整數(shù)非標度值 和32 位的整數(shù)標度 (scale) 組成。如果為零或正數(shù),則標度是小數(shù)點后的位數(shù)。如果為負數(shù),則將該數(shù)的非標度值乘以 10 的負scale 次冪。
    2016-06-06
  • spring中@Transactional注解和事務(wù)的實戰(zhàn)

    spring中@Transactional注解和事務(wù)的實戰(zhàn)

    本文主要介紹了spring中@Transactional注解和事務(wù)的實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-07-07
  • Java之Spring簡單的讀取和存儲對象

    Java之Spring簡單的讀取和存儲對象

    這篇文章主要介紹了Spring的讀取和存儲對象,獲取 bean 對象也叫做對象裝配,是把對象取出來放到某個類中,有時候也叫對象注?,想進一步了解的同學(xué)可以參考本文
    2023-04-04
  • maven 刪除下載失敗的包的方法

    maven 刪除下載失敗的包的方法

    本文介紹了當Maven包報紅時,使用刪除相關(guān)文件的方法來解決該問題,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • Java?8?Stream?處理數(shù)據(jù)方法匯總

    Java?8?Stream?處理數(shù)據(jù)方法匯總

    這篇文章主要介紹了Java?8?Stream處理數(shù)據(jù),Stream是Java?8?新引入的一個包它讓我們能用聲明式的方式處理數(shù)據(jù),Stream流式處理相較于傳統(tǒng)方法簡潔高效,也便于進行并發(fā)編程,更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章內(nèi)容
    2022-06-06

最新評論

吐鲁番市| 班玛县| 东阳市| 泸水县| 琼结县| 鹤山市| 新密市| 宜阳县| 盐亭县| 霍山县| 西华县| 民丰县| 汕头市| 莆田市| 清水河县| 泾阳县| 济阳县| 乳源| 江北区| 广饶县| 平陆县| 如皋市| 太康县| 普宁市| 巴青县| 南皮县| 城步| 新竹县| 望江县| 青岛市| 达州市| 灵山县| 阳谷县| 林甸县| 涞水县| 瑞安市| 成安县| 宣威市| 莱州市| 葵青区| 德昌县|