详解在Spring Boot中使用数据库事务

时间:2022-07-02 16:56:47

我们在前面已经分别介绍了如何在spring boot中使用jpa以及如何在spring boot中输出rest资源。那么关于数据库访问还有一个核心操作那就是事务的处理了,前面两篇博客小伙伴们已经见识到spring boot带给我们的巨大便利了,其实不用猜,我们也知道spring boot在数据库事务处理问题上也给我们带来惊喜,ok,废话不多说,就来看看如何在spring boot中使用事务吧。

ok,那我们开始今天愉快的coding旅程吧!

创建project并添加数据库依赖

这个没啥好说的,不懂如何创建一个spring boot工程的小伙伴请移步这里初识spring boot框架。创建的时候选择依赖时选择web和jpa,如下图:

详解在Spring Boot中使用数据库事务

ok,工程创建成功之后接下来我们来添加数据库驱动,和前文一样,我这里还是以mysql数据库为例,在pom.xml文件中添加如下依赖:

?
1
2
3
4
5
<dependency>
      <groupid>mysql</groupid>
      <artifactid>mysql-connector-java</artifactid>
      <version>5.1.40</version>
    </dependency>

配置application.properties

配置方式还是和前文一模一样,我这里直接贴代码,含义不再赘述:

?
1
2
3
4
5
6
7
8
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

创建实体类

实体类还是一个person,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@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;
  }
}

创建实体类的repository

?
1
2
public interface personrepository extends jparepository<person,long> {
}

这里因为我们的目的是测试事务,所以repository中暂时先不写任何东西。

创建业务服务service

创建service接口

?
1
2
3
4
5
public interface demoservice {
  public person savepersonwithrollback(person person);
 
  public person savepersonwithoutrollback(person person);
}

创建service实现类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@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 已存在,数据将回滚");
    }
    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已存在,但数据不会回滚");
    }
    return p;
  }
}

在这里我们使用到了@transactional注解,该注解中有一个rollbackfor属性,该属性的值为数组,表示当该方法中抛出指定的异常时数据回滚,该注解还有个属性叫norollbackfor,表示当该方法中抛出指定的异常时数据不回滚,这两个属性我们分别在两个方法中体现。

创建控制器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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);
  }
}

控制器创建成功之后接下来我们就可以直接在浏览器中访问这两个地址看看效果了。

测试

首先在浏览器中输入http://localhost:8080/rollback?name=sang&age=100,我们来测试回滚的情况,访问结果如下:

详解在Spring Boot中使用数据库事务

看看控制台抛出的异常:

详解在Spring Boot中使用数据库事务

这个时候再去查看数据库,发现数据表中并没有插入数据。

再在地址栏输入http://localhost:8080/norollback?name=sang&age=100,测试结果如下:

浏览器依然报错:

详解在Spring Boot中使用数据库事务

控制台也打印了错误,但是这个时候再去看数据库,数据已成功插入了。如下图:

详解在Spring Boot中使用数据库事务

ok,以上就是数据库事务在spring boot中的简单使用。

本文案例github地址https://github.com/lenve/javaeetest/tree/master/test24-transaction

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/u012702547/article/details/54098190