什么是jpa
jpa(java persistence api)是sun官方提出的java持久化規(guī)范。它為java開發(fā)人員提供了一種對象/關(guān)聯(lián)映射工具來管理java應(yīng)用中的關(guān)系數(shù)據(jù)。他的出現(xiàn)主要是為了簡化現(xiàn)有的持久化開發(fā)工作和整合orm技術(shù)
spring data jpa 是 spring 基于 orm 框架、jpa 規(guī)范的基礎(chǔ)上封裝的一套jpa應(yīng)用框架,可使開發(fā)者用極簡的代碼即可實現(xiàn)對數(shù)據(jù)的訪問和操作。它提供了包括增刪改查等在內(nèi)的常用功能,且易于擴展!學習并使用 spring data jpa 可以極大提高開發(fā)效率!
spring boot中使用jdbctemplate訪問數(shù)據(jù)庫
數(shù)據(jù)源配置
首先,為了連接數(shù)據(jù)庫需要引入jdbc支持,在pom.xml中引入如下配置
1
2
3
4
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-jdbc</artifactid> </dependency> |
嵌入式數(shù)據(jù)庫支持
嵌入式數(shù)據(jù)庫通常用于開發(fā)和測試環(huán)境。spring-boot提供自動配置的嵌入式數(shù)據(jù)庫有h2、hsql、derby,你不需要提供任何連接配置就能使用。
如h2的依賴
1
2
3
4
5
|
<dependency> <groupid>com.h2database</groupid> <artifactid>h2</artifactid> <scope>runtime</scope> </dependency> |
mysql數(shù)據(jù)庫支持
1
2
3
4
5
|
<dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version> 5.1 . 38 </version> </dependency> |
編輯配置信息
在 src/main/resources/application.properties 中配置數(shù)據(jù)源信息
1
2
3
4
|
spring.datasource.url=jdbc:mysql: //localhost:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driver- class -name=com.mysql.jdbc.driver |
使用jdbctemplate操作數(shù)據(jù)庫
spring的jdbctemplate是自動配置的,你可以直接使用@autowired來注入到你自己的bean中來使用。
通過jdbctemplate實現(xiàn)demoservice中定義的數(shù)據(jù)訪問操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@service public class demoserivce { @autowired private jdbctemplate jdbctemplate; public void create(string name, integer age) { jdbctemplate.update( "insert into demo(name, age) values(?, ?)" , name, age); } public void deletebyname(string name) { jdbctemplate.update( "delete from demowhere name = ?" , name); } public integer getalldemo() { return jdbctemplate.queryforobject( "select count(1) from demo" , integer. class ); } public void deletealldemo() { jdbctemplate.update( "delete from demo" ); } } |
創(chuàng)建對userservice的單元測試用例,通過創(chuàng)建、刪除和查詢來驗證數(shù)據(jù)庫操作的正確性。
測試用例要增加依賴
1
2
3
4
5
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> |
測試代碼
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
|
@runwith (springjunit4classrunner. class ) @springapplicationconfiguration (main. class ) public class applicationtests { @autowired private demoserivce demoserivce; @before public void setup() { // 準備,清空表 demoserivce.deletealldemo(); } @test public void test() throws exception { // 插入5個 demoserivce.create( "a" , 1 ); demoserivce.create( "b" , 2 ); demoserivce.create( "c" , 3 ); demoserivce.create( "d" , 4 ); demoserivce.create( "e" , 5 ); assert .assertequals( 5 , demoserivce.getalldemo().intvalue()); demoserivce.deletebyname( "a" ); demoserivce.deletebyname( "e" ); // 查數(shù)據(jù)庫,應(yīng)該有5個 assert .assertequals( 3 , demoserivce.getalldemo().intvalue()); } } |
spring boot中使用spring-data-jpa
為了解決這些大量枯燥的數(shù)據(jù)操作語句,我們第一個想到的是使用orm框架,比如:hibernate。通過整合hibernate之后,我們以操作java實體的方式最終將數(shù)據(jù)改變映射到數(shù)據(jù)庫表中。
為了解決抽象各個java實體基本的“增刪改查”操作,我們通常會以泛型的方式封裝一個模板dao來進行抽象簡化,但是這樣依然不是很方便,我們需要針對每個實體編寫一個繼承自泛型模板dao的接口,再編寫該接口的實現(xiàn)。雖然一些基礎(chǔ)的數(shù)據(jù)訪問已經(jīng)可以得到很好的復(fù)用,但是在代碼結(jié)構(gòu)上針對每個實體都會有一堆dao的接口和實現(xiàn)。
由于模板dao的實現(xiàn),使得這些具體實體的dao層已經(jīng)變的非常“薄”,有一些具體實體的dao實現(xiàn)可能完全就是對模板dao的簡單代理,并且往往這樣的實現(xiàn)類可能會出現(xiàn)在很多實體上。spring-data-jpa的出現(xiàn)正可以讓這樣一個已經(jīng)很“薄”的數(shù)據(jù)訪問層變成只是一層接口的編寫方式。
使用方法
添加依賴
1
2
3
4
|
<dependency <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-jpa</artifactid> </dependency> |
編輯配置信息
在 src/main/resources/application.properties 中配置數(shù)據(jù)源信息
1
2
3
4
5
|
spring.datasource.url=jdbc:mysql: //localhost:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driver- class -name=com.mysql.jdbc.driver spring.jpa.properties.hibernate.hbm2ddl.auto=update |
spring.jpa.properties.hibernate.hbm2ddl.auto是hibernate的配置屬性,其主要作用是:自動創(chuàng)建、更新、驗證數(shù)據(jù)庫表結(jié)構(gòu)。該參數(shù)的幾種配置如下
- create: 每次加載hibernate時都會刪除上一次的生成的表,然后根據(jù)你的model類再重新來生成新表
- create-drop:每次加載hibernate時根據(jù)model類生成表,但是sessionfactory一關(guān)閉,表就自動刪除
- update:最常用的屬性,第一次加載hibernate時根據(jù)model類會自動建立起表的結(jié)構(gòu)(前提是先建立好數(shù)據(jù)庫),以后加載hibernate時根據(jù)model類自動更新表結(jié)構(gòu)
- validate:每次加載hibernate時,驗證創(chuàng)建數(shù)據(jù)庫表結(jié)構(gòu),只會和數(shù)據(jù)庫中的表進行比較,不會創(chuàng)建新表,但是會插入新值
創(chuàng)建實體
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@entity public class demoentity { @id @generatedvalue private long id; private string title; private string content; public demoentity() { } public demoentity(string title, string content) { this .title = title; this .content = content; } // get set 略 } |
創(chuàng)建dao
1
2
3
4
5
6
7
|
public interface demorepository extends jparepository<demoentity, long > { demoentity findbytitle(string title); demoentity findbytitleandcontent(string title, string content); // @query("select u from demoentity u where u.content=:content") @query ( "from demoentity u where u.content=:content" ) demoentity sqlfind( @param ( "content" ) string content); } |
sql中不要寫表名,要寫實體名,他會自動轉(zhuǎn)化為表名的。
通過解析方法名創(chuàng)建查詢
上面 findbytitle(string title) 與 findbytitleandcontent(string title, string content) ,沒有寫sql,但框架會自動按名字對上面的方對創(chuàng)建sql。
單元測試
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
|
@runwith (springjunit4classrunner. class ) @springapplicationconfiguration (main. class ) public class unittest { @autowired demorepository demorepository; @test public void test() { for ( int i= 0 ;i< 10 ;i++) { demorepository.save( new demoentity( "title" +i, "content" +i)); } assert .assertequals( 10 , demorepository.findall().size()); } @test public void testfindbytitle() { demoentity res = demorepository.findbytitle( "title8" ); assert .assertequals( "title8" , res.gettitle()); } @test public void testfindbytitleandcontent() { demoentity res = demorepository.findbytitleandcontent( "title9" , "content9" ); assert .assertequals( "title9" , res.gettitle()); assert .assertequals( "content9" , res.getcontent()); } @test public void testsqlfind() { demoentity res = demorepository.sqlfind( "content7" ); assert .assertequals( "content7" , res.getcontent()); } } |
總結(jié)
以上所述是小編給大家介紹的spring boot中使用spring-data-jpa方便快捷的訪問數(shù)據(jù)庫,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://blog.fengcl.com/2018/05/03/spring-data-jpa/