溫馨提示×

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

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

怎么在springboot中使用事物注解

發(fā)布時(shí)間:2021-03-23 15:38:53 來源:億速云 閱讀:112 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關(guān)怎么在springboot中使用事物注解,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

1.在啟動(dòng)類Application中添加注解@EnableTransactionManagement

import tk.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement //開啟書屋管理注解模式 最新的版本可以省略
@MapperScan("com.xz.springboot.mapper") //掃描該包下所有的接口并為該接口生成實(shí)現(xiàn)類
public class Springboot01Application {
  public static void main(String[] args) {
    SpringApplication.run(Springboot01Application.class, args);
  }
}

2.在業(yè)務(wù)層添加@Transactional

import com.xz.springboot.bean.User;
import com.xz.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    public List<User> queryAll(){
      System.out.println("熱部署");
      return userMapper.findAll();
    }
  @Transactional
  public void deleteById(Integer id) {
      userMapper.deleteById(id);
     // int c=10/0;
  }
}

看完上述內(nèi)容,你們對(duì)怎么在springboot中使用事物注解有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

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

AI