春ブーツ単一のデータベーストランザクション

1:スタートクラス構成

@EnableAspectJAutoProxy(exposeProxy =真)
@EnableTransactionManagement
@SpringBootApplication(除外= {SecurityAutoConfiguration.class})
パブリッククラスShoppingmallApplication {

パブリック静的無効メイン(文字列[] args){
SpringApplication.run(ShoppingmallApplication.class、引数)。
}
}

 

2:ビジネスメソッドを呼び出します

   @Override
     @Transactional
    公共HttpResult simpleAdd(EquipmentSimpleDto ES){
        HttpResult R = 新しいHttpResult()。
        logger.info(" ========== ==========簡単スタートが追加... " );
         試み{
            文字列のcreateUser = ショッピングモール
            備考文字列を使用 = シンプルなアドオンを
            日付日付 = 新しい日付();

            // 店铺 
            ショップショップ= 新しいショップ(es.getShopName()、es.getOrgId());
            shops.setCreateUser(のcreateUser)。
            shops.setCreateTime(日付);
            shops.setRemark(備考)。
            ((ShopsService)AopContext.currentProxy())の保存(店舗);. // このメソッド呼び出しの 
            logger.info(" ショップが作成されました" );

            // 模板 
            ShoppingReceiptTemplateテンプレート= 新しいShoppingReceiptTemplate(es.getOrgId()、es.getShopName());
            template.setIsCommon(Boolean.FALSE)。
            template.setCreateUser(のcreateUser)。
            template.setCreateTime(日付);
           templateService.save(テンプレート); //他のビジネス層の呼び出しは、 
            (logger.info テンプレートが作成されました);

            // 装置 
            EquipmentInfo equipmentInfo = 新しい新しいEquipmentInfo(es.getEquipmentId()、es.getOrgId());
             // デバイス-関連ストア 
            equipmentInfo.setBindings(2 )。
            equipmentInfo.setShopsId(shops.getId())。
            equipmentInfo.setShopsName(shops.getName())。
            // デバイス-関連テンプレート
            equipmentInfo.setReceiptId(template.getId()のtoString() )。
            equipmentInfo.setReceiptName(template.getName())。

            equipmentInfo.setCreateUser(のcreateUser)。
            equipmentInfo.setRemark(備考)。
            equipmentInfo.setCreateTime(日付);
            equipmentInfoService.save(equipmentInfo)。
            logger.info(デバイスが作成されました);

            r.setCode(0 );
             // テストロールバック
            / *
            私は0を= int型。
            = 123 INT / I;
           System.out.println(A)。* / 
        } キャッチ(例外e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus()のsetRollbackOnly();  //关键 
            r.setCode( - 1 )。
            r.setMsg(" 异常:" + e.getMessage());
        } 最後に{
            logger.info(" ========== ==========シンプル追加終わり... " );
        }
        戻りR;
    }

 

おすすめ

転載: www.cnblogs.com/eason-d/p/11834824.html