亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解在Spring?Boot中使用數(shù)據(jù)庫(kù)事務(wù)

 更新時(shí)間:2017年05月19日 11:39:05   作者:_江南一點(diǎn)雨  
本篇文章主要介紹了詳解在Spring?Boot中使用數(shù)據(jù)庫(kù)事務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下<BR>

我們?cè)谇懊嬉呀?jīng)分別介紹了如何在spring Boot中使用JPA以及如何在Spring Boot中輸出REST資源。那么關(guān)于數(shù)據(jù)庫(kù)訪(fǎng)問(wèn)還有一個(gè)核心操作那就是事務(wù)的處理了,前面兩篇博客小伙伴們已經(jīng)見(jiàn)識(shí)到Spring Boot帶給我們的便利了,其實(shí)不用猜,我們也知道Spring Boot在數(shù)據(jù)庫(kù)事務(wù)處理問(wèn)題上也給我們帶來(lái)驚喜,OK,廢話(huà)不多說(shuō),就來(lái)看看如何在Spring Boot中使用事務(wù)吧。

OK,那我們開(kāi)始今天愉快的coding旅程吧!

創(chuàng)建Project并添加數(shù)據(jù)庫(kù)依賴(lài)

這個(gè)沒(méi)啥好說(shuō)的,不懂如何創(chuàng)建一個(gè)Spring Boot工程的小伙伴請(qǐng)移步這里初識(shí)Spring Boot框架。創(chuàng)建的時(shí)候選擇依賴(lài)時(shí)選擇Web和JPA,如下圖:

OK,工程創(chuàng)建成功之后接下來(lái)我們來(lái)添加數(shù)據(jù)庫(kù)驅(qū)動(dòng),和前文一樣,我這里還是以MySQL數(shù)據(jù)庫(kù)為例,在pom.xml文件中添加如下依賴(lài):

<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.40</version>
    </dependency>

配置application.properties

配置方式還是和前文一模一樣,我這里直接貼代碼,含義不再贅述:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/rest
spring.datasource.username=root
spring.datasource.password=sang

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true

創(chuàng)建實(shí)體類(lèi)

實(shí)體類(lèi)還是一個(gè)Person,如下:

@Entity
public class Person {
  @Id
  @GeneratedValue
  private Long id;
  private String name;
  private String address;
  private Integer age;

  public Person() {
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }

  public Person(Long id, String name, String address, Integer age) {
    this.id = id;
    this.name = name;
    this.address = address;
    this.age = age;
  }
}

創(chuàng)建實(shí)體類(lèi)的Repository

public interface PersonRepository extends JpaRepository<Person,Long> {
}

這里因?yàn)槲覀兊哪康氖菧y(cè)試事務(wù),所以Repository中暫時(shí)先不寫(xiě)任何東西。

創(chuàng)建業(yè)務(wù)服務(wù)Service

創(chuàng)建Service接口

public interface DemoService {
  public Person savePersonWithRollBack(Person person);

  public Person savePersonWithoutRollBack(Person person);
}

創(chuàng)建Service實(shí)現(xiàn)類(lèi)

@Service
public class DemoServiceImpl implements DemoService {
  @Autowired
  PersonRepository personRepository;

  @Transactional(rollbackFor = {IllegalArgumentException.class})
  @Override
  public Person savePersonWithRollBack(Person person) {
    Person p = personRepository.save(person);
    if (person.getName().equals("sang")) {
      throw new IllegalArgumentException("sang 已存在,數(shù)據(jù)將回滾");
    }
    return p;
  }

  @Transactional(noRollbackFor = {IllegalArgumentException.class})
  @Override
  public Person savePersonWithoutRollBack(Person person) {
    Person p = personRepository.save(person);
    if (person.getName().equals("sang")) {
      throw new IllegalArgumentException("sang已存在,但數(shù)據(jù)不會(huì)回滾");
    }
    return p;
  }
}

在這里我們使用到了@Transactional注解,該注解中有一個(gè)rollbackFor屬性,該屬性的值為數(shù)組,表示當(dāng)該方法中拋出指定的異常時(shí)數(shù)據(jù)回滾,該注解還有個(gè)屬性叫noRollbackFor,表示當(dāng)該方法中拋出指定的異常時(shí)數(shù)據(jù)不回滾,這兩個(gè)屬性我們分別在兩個(gè)方法中體現(xiàn)。

創(chuàng)建控制器

@RestController
public class MyController {
  @Autowired
  private DemoService demoService;

  @RequestMapping("/norollback")
  public Person noRollback(Person person) {
    return demoService.savePersonWithoutRollBack(person);
  }

  @RequestMapping("/rollback")
  public Person rollback(Person person) {
    return demoService.savePersonWithRollBack(person);
  }
}

控制器創(chuàng)建成功之后接下來(lái)我們就可以直接在瀏覽器中訪(fǎng)問(wèn)這兩個(gè)地址看看效果了。

測(cè)試

首先在瀏覽器中輸入http://localhost:8080/rollback?name=sang&age=100,我們來(lái)測(cè)試回滾的情況,訪(fǎng)問(wèn)結(jié)果如下:

看看控制臺(tái)拋出的異常:

這個(gè)時(shí)候再去查看數(shù)據(jù)庫(kù),發(fā)現(xiàn)數(shù)據(jù)表中并沒(méi)有插數(shù)據(jù)。

再在地址欄輸入http://localhost:8080/norollback?name=sang&age=100,測(cè)試結(jié)果如下:

瀏覽器依然報(bào)錯(cuò):

控制臺(tái)也打印了錯(cuò)誤,但是這個(gè)時(shí)候再去看數(shù)據(jù)庫(kù),數(shù)據(jù)已成功了。如下圖:

OK,以上就是數(shù)據(jù)庫(kù)事務(wù)在Spring Boot中的簡(jiǎn)單使用。

本文案例GitHub地址https://github.com/lenve/JavaEETest/tree/master/Test24-Transaction。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論