spring新增了一個新的數據模塊:spring data jdbc。spring data jdbc背后的想法是提供對關系數據庫的訪問,而無需處理jpa的復雜性。jpa提供延遲加載,緩存和臟跟蹤等功能。果你需要這些功能會很很棒,但會讓猜測jpa的行為比非jpa更難。
延遲加載可能會在你不需要時觸發昂貴的語句,或者它可能會因異常而失敗。當你想要比較一個實體的兩個版本是哪個變成臟數據時,緩存可能會妨礙你,讓你很難找到所有持久性操作都通過的那個點。
spring data jdbc目標是實現更簡單的模型,不會有緩存,臟數據跟蹤或延遲加載。相反,只有在調用數據庫方法時才會發出sql語句。方法返回的對象會完全加載,不會有延遲。實體沒有“會話”和代理。所有這些都應該使spring data jdbc更易于推理。
當然,這種更簡單的方法會導致約束。
我們來看一個簡單的例子。
首先,我們需要一個實體:
1
2
3
4
5
6
|
class customer { @id long id; string firstname; localdate dob; } |
請注意,不需要getter或setter。如果您意,可以增加。實際上,唯一的要求是實體有一個注釋的屬性id(即@org.springframework.data.annotation.id,注意不是javax.persistence,后者是jpa)。
接下來,我們需要聲明一個存儲庫。最簡單的方法是擴展crudrepository:
1
|
interface customerrepository extends crudrepository<customer, long > {} |
最后,我們需要配置applicationcontext以啟用存儲庫的創建:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@configuration @enablejdbcrepositories ( 1 ) public class customerconfig extends jdbcconfiguration { ( 2 ) @bean namedparameterjdbcoperations operations() { ( 3 ) return new namedparameterjdbctemplate(datasource()); } @bean platformtransactionmanager transactionmanager() { ( 4 ) return new datasourcetransactionmanager(datasource()); } @bean datasource datasource(){ ( 5 ) return new embeddeddatabasebuilder() .generateuniquename( true ) .settype(embeddeddatabasetype.hsql) .addscript( "create-customer-schema.sql" ) .build(); } } |
讓我們一步一步地解釋。
1. enablejdbcrepositories可以創建存儲庫。由于它需要存在一些bean,我們需要其余的配置。
2. 繼承擴展的jdbcconfiguration將一些默認bean添加到applicationcontext。可以覆蓋其方法以自定義spring data jdbc的某些行為。現在,我們使用默認實現。
3. 真正重要的部分是namedparameterjdbcoperations,它在內部用于向數據庫提交sql語句。
4. 嚴格來說,事務管理器不是必需的。不支持跨越多個sql語句的事務。
5. spring data jdbc沒有直接使用datasource,但是,由于transactionmanager和namedparameterjdbcoperations需要,將datasource注冊為bean是一種確保兩者使用相同實例的簡單方法。
這就是一切。現在讓我們測試玩玩:
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
|
@runwith (springrunner. class ) @transactional @contextconfiguration (classes = customerconfig. class ) public class customerrepositorytest { @autowired customerrepository customerrepo; @test public void createsimplecustomer() { customer customer = new customer(); customer.dob = localdate.of( 1904 , 5 , 14 ); customer.firstname = "albert" ; customer saved = customerrepo.save(customer); assertthat(saved.id).isnotnull(); saved.firstname = "hans albert" ; customerrepo.save(saved); optional<customer> reloaded = customerrepo.findbyid(saved.id); assertthat(reloaded).isnotempty(); assertthat(reloaded.get().firstname).isequalto( "hans albert" ); } } |
@query 注解
你可能不會只使用基本的crud方法crudrepository。可以使用簡單的@query注釋來指定存儲庫方法的查詢:
1
2
|
@query ( "select id, first_name, dob from customer where upper(first_name) like '%' || upper(:name) || '%' " ) list<customer> findbyname( @param ( "name" ) string name); |
請注意,@param如果使用-parameters標志進行編譯,則不需要注釋。
如果要執行更新或刪除語句,可以使用@modifying向方法添加注釋。
讓我們創建另一個測試以試用新方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@test public void findbyname() { customer customer = new customer(); customer.dob = localdate.of( 1904 , 5 , 14 ); customer.firstname = "albert" ; customer saved = customerrepo.save(customer); assertthat(saved.id).isnotnull(); customer.id= null ; ( 1 ) customer.firstname = "bertram" ; customerrepo.save(customer); customer.id= null ; customer.firstname = "beth" ; customerrepo.save(customer); assertthat(customerrepo.findbyname( "bert" )).hassize( 2 ); ( 2 ) } |
由于java對象與其對應行之間的連接是id類型,因此設置id為null并再次保存它會在數據庫中創建另一行。
我們正在進行不區分大小寫(例如)搜索,因此,我們找到“albert”和“bertram”,但不是“beth”。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jdon.com/50191