我們?cè)谇懊嬉呀?jīng)分別介紹了如何在spring boot中使用jpa以及如何在spring boot中輸出rest資源。那么關(guān)于數(shù)據(jù)庫訪問還有一個(gè)核心操作那就是事務(wù)的處理了,前面兩篇博客小伙伴們已經(jīng)見識(shí)到spring boot帶給我們的巨大便利了,其實(shí)不用猜,我們也知道spring boot在數(shù)據(jù)庫事務(wù)處理問題上也給我們帶來驚喜,ok,廢話不多說,就來看看如何在spring boot中使用事務(wù)吧。
ok,那我們開始今天愉快的coding旅程吧!
創(chuàng)建project并添加數(shù)據(jù)庫依賴
這個(gè)沒啥好說的,不懂如何創(chuàng)建一個(gè)spring boot工程的小伙伴請(qǐng)移步這里初識(shí)spring boot框架。創(chuàng)建的時(shí)候選擇依賴時(shí)選擇web和jpa,如下圖:
ok,工程創(chuàng)建成功之后接下來我們來添加數(shù)據(jù)庫驅(qū)動(dòng),和前文一樣,我這里還是以mysql數(shù)據(jù)庫為例,在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 |
創(chuàng)建實(shí)體類
實(shí)體類還是一個(gè)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; } } |
創(chuàng)建實(shí)體類的repository
1
2
|
public interface personrepository extends jparepository<person, long > { } |
這里因?yàn)槲覀兊哪康氖菧y(cè)試事務(wù),所以repository中暫時(shí)先不寫任何東西。
創(chuàng)建業(yè)務(wù)服務(wù)service
創(chuàng)建service接口
1
2
3
4
5
|
public interface demoservice { public person savepersonwithrollback(person person); public person savepersonwithoutrollback(person person); } |
創(chuàng)建service實(shí)現(xiàn)類
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 已存在,數(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)建控制器
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); } } |
控制器創(chuàng)建成功之后接下來我們就可以直接在瀏覽器中訪問這兩個(gè)地址看看效果了。
測(cè)試
首先在瀏覽器中輸入http://localhost:8080/rollback?name=sang&age=100,我們來測(cè)試回滾的情況,訪問結(jié)果如下:
看看控制臺(tái)拋出的異常:
這個(gè)時(shí)候再去查看數(shù)據(jù)庫,發(fā)現(xiàn)數(shù)據(jù)表中并沒有插入數(shù)據(jù)。
再在地址欄輸入http://localhost:8080/norollback?name=sang&age=100,測(cè)試結(jié)果如下:
瀏覽器依然報(bào)錯(cuò):
控制臺(tái)也打印了錯(cuò)誤,但是這個(gè)時(shí)候再去看數(shù)據(jù)庫,數(shù)據(jù)已成功插入了。如下圖:
ok,以上就是數(shù)據(jù)庫事務(wù)在spring boot中的簡(jiǎn)單使用。
本文案例github地址https://github.com/lenve/javaeetest/tree/master/test24-transaction。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/u012702547/article/details/54098190