@Transactionalコメントのロールバック

1.データベースフィールドの対応が所有するプロパティを持つエンティティクラスを作成します。

import lombok.Data;
@Data
public class InterfaceTest {
private Integer interfaceId;
private String interfaceName;
private String interfaceType;
private String interfaceMethod;
private String interfaceAliasName;

}

2つのデータベーステーブルがフィールドがしているここに画像を挿入説明
インターフェイスを作成:CRUDインターフェイスメソッドを記述し、新しいメソッドを書きます

import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface MyInterfaceTestMapper {
void addInterfaceTest(InterfaceTest interfaceTest);
}

xmlファイルを作成します:CRUDのSQLファイルを書き込み、新しいSQLを記述

<mapper namespace="com.longteng.lesson2.my.mybatis.MyInterfaceTestMapper">
    <!--id就是接口中的方法名-->
    <insert id="addInterfaceTest"
            useGeneratedKeys="true"
            keyColumn="interface_test_id"
            keyProperty="interfaceId"
            parameterType="com.longteng.lesson2.my.mybatis.InterfaceTest">
            <!--给哪个表插入数据,就写哪个表-->
            insert into interface_test
            <!--给哪些字段插入数据,就写哪些字段名-->
            (interface_name,interface_method,interface_type,interface_alias_name)
            <!--要插入的数据是对应的哪些字段,就是实体类里的字段,要与字段名的先后顺序一一对应,用#{}接收-->
            values (#{interfaceName},#{interfaceMethod},#{interfaceType},#{interfaceAliasName})
    </insert>

物事:物事は@Transactionalによりロールバックされました

複数の操作を実行するために一緒に入れての事と呼ばれる
、このようなビジネスの下位をとして
最初に行われたが、データベースに挿入されるが、実際には、これはすでに順番で成功した場合のフォローアップ事業は、次の順番のビジネスが完全に終了していない、成功しなかった後にデータベースが挿入されています失敗したデータの一部のデータを挿入すると、ダーティ・データで、その後、行うためのデータのものはロールバック操作を行うことで成功していない
プログラム管理、物事に何か、物事管理文で、ステートメントが実装何かの一例である
プログラミングの事:複雑なシステムのために使用され、自分自身のロールバック処理を制御する
もの宣言:簡単なプロセスのために@Transactionalロールバックアノテーションフロー制御、および多くを
輸入com.longteng.lesson2.my.mybatis.InterfaceTest、
インポートcom.longteng.lesson2.my.mybatis.MyInterfaceTestMapper ;
インポートorg.springframework.beans.factory.annotation.Autowired;
インポートorg.springframework.stereotype.Service;
インポートorg.springframework.transaction.annotation.Transactional;
@Service
パブリッククラスInterfaceService {
//注入インターフェイス
@Autowired
MyInterfaceTestMapper myInterfaceTest マッパー;
//異常なロールバック
@Transactional(rollbackFor = Exception.class)は、
例外がスローされ、パラメータを使用してObject型方法を戻す//
パブリックオブジェクトテスト(整数I)は、例外{スロー
一例として、データベースにデータを追加、//エンティティ・クラスのインスタンスを作成
InterfaceTest interfaceTest ;新)(=新しいInterfaceTest
interfaceTest.setInterfaceName( "QWER");
;( "GET")interfaceTest.setInterfaceMethod
interfaceTest.setInterfaceType( "HTTP");
interfaceTest.setInterfaceAliasName( "QWER");
//コールインターフェースインターフェースクラスのインスタンス新しい方法は、データベースにデータを追加する
myInterfaceTestMapper.addInterfaceTest(interfaceTest);
// iは正常に戻るが1に等しい場合、転送方法は、例外をスロー基準に等しくない場合、成功を添加
{IF(I = 1!)
スロー新しい新しい例外();
}他{
;リターン"が正常に追加しました"
}
}
}

コントローラ

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyInterfaceController {
//注入回滚的业务类
@Autowired
InterfaceService interfaceService;
@RequestMapping("/interfaceService")
public @ResponseBody Object test(Integer i)throws Exception{
    return interfaceService.test(i);
}
}

ブラウザが要求しhttp://127.0.0.1:8080/interfaceService?i=1私は、データベースにデータを追加します。1 =成功

ここに画像を挿入説明
ブラウザが要求しhttp://127.0.0.1:8080/interfaceService?i=2 I = 2は、データベースにデータを追加することができませんでした

あなたが@Transactionalない場合でも、私は、データベースに1と等しくない要求した場合、成功することができ
、例えば、コールaddInterfaceTest方法は、実際にデータベース操作に挿入され、およびデバッグ言葉され、SQL実際に印刷された、しかしであるため、この時間は、データベースに挿入されていませんでしたノートをロールバックし、全体のことは実行するコードを読んでいない提出、で、その全体の試験方法は、SQLデータベースを挿入するかどうかを決定する前に終了しています

おすすめ

転載: blog.csdn.net/qq_41767337/article/details/89397191