春ブーツのトランザクション管理[春ブーツ4を学習]

環境
  のEclipse 4.7
  JDK 1.8
  春ブーツ1.5.2

、springboot統合事務
総務カテゴリー:業務のプログラミング、宣言的トランザクションのみに@Transactionalとmainメソッドでは(XML、注釈)、注釈を使用することを推奨し、springbootのデフォルト統合の事、
1、コントローラ

パッケージcom.wjy.controller。

輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.bind.annotation.RestController。

輸入com.wjy.test1.service.UserServiceTest1。


@RestController 
パブリック クラスUserControllerで{ 

    @Autowired 
    公共UserServiceTest1 userServiceTest1。
    
    (@RequestMapping "/ insertTest1ByService" パブリック文字列insertTest1ByService(文字列名、整数年齢){ 
        userServiceTest1.insertuser1(名前、年齢)。
        リターン「成功」
    } 
    
    
}

2、サービス

/ ** 
 * 
 * / 
パッケージcom.wjy.test1.service; 

インポートorg.springframework.beans.factory.annotation.Autowired;
 インポートorg.springframework.stereotype.Service;
 インポートorg.springframework.transaction.annotation.Transactional; 

インポートCOM .wjy.test1.dao.UserMapperTest1; 

/ ** 
 * @Desc 
 * @author wangjy15
  * / 
@Service 
パブリック クラスUserServiceTest1 { 
    
    @Autowired 
    プライベートuserMapperTest1 userMapperTest1; 
    
    / ** 
     * @description:なしトランザクション制御は、エラーの後にデータベースに挿入されていない場合データは@Transactionalをロールバックする注釈をロールバックされません場所
     * / 
    @Transactional 
    パブリック文字列insertuser1(文字列名、整数歳){ 
        userMapperTest1.insert(名前、年齢); 
        int型 I = 1/0 ;
        戻る「成功」
    } 

}

 

3、マッパー

/ ** 
 * 
 * / 
パッケージcom.wjy.test1.dao。

輸入org.apache.ibatis.annotations.Insert。
輸入org.apache.ibatis.annotations.Param。
輸入org.apache.ibatis.annotations.Select; 

輸入com.wjy.entity.User。

/ ** 
 * @Desc 
 * @author wangjy15
  * / 
パブリック インターフェイスUserMapperTest1 { 
    
    @Select( "NAME =#{名前}ユーザーSELECT * FROM" 
    ユーザーfindByName(@Param( "名前" )文字列名)。

    @Insert( "ユーザ(名前、年齢)の値(#{名前}、#{年齢})に挿入" INT(@Param( "名前")文字列名を、@ Paramの( "年齢"を挿入)整数歳); 
}

 

4、APP

パッケージcom.wjy。

輸入org.springframework.boot.SpringApplication。
輸入org.springframework.boot.autoconfigure.SpringBootApplication。

@SpringBootApplication 
パブリック クラスAPP { 

    公共 静的 ボイドメイン(文字列[]引数){ 
        SpringApplication.run(APP。クラス、引数)。
    } 

}

5、テストおよび認定

http:// localhostを:?8080 / insertTest1ByService名= wangsan0010&年齢= 1000

 

二、SpringBoot分散トランザクション管理
の伝統的なプロジェクトの:JTA +自動

 

おすすめ

転載: www.cnblogs.com/cac2020/p/11230967.html