欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

OK,那我們開始今天愉快的coding旅程吧!

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

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

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

<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)建實體類

實體類還是一個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)建實體類的Repository

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

這里因為我們的目的是測試事務,所以Repository中暫時先不寫任何東西。

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

創(chuàng)建Service接口

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

  public Person savePersonWithoutRollBack(Person person);
}

創(chuàng)建Service實現(xiàn)類

@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ù)不會回滾");
    }
    return p;
  }
}

在這里我們使用到了@Transactional注解,該注解中有一個rollbackFor屬性,該屬性的值為數(shù)組,表示當該方法中拋出指定的異常時數(shù)據(jù)回滾,該注解還有個屬性叫noRollbackFor,表示當該方法中拋出指定的異常時數(shù)據(jù)不回滾,這兩個屬性我們分別在兩個方法中體現(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)建成功之后接下來我們就可以直接在瀏覽器中訪問這兩個地址看看效果了。

測試

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

看看控制臺拋出的異常:

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

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

瀏覽器依然報錯:

控制臺也打印了錯誤,但是這個時候再去看數(shù)據(jù)庫,數(shù)據(jù)已成功了。如下圖:

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

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論