Spring Data JPA是更大的Spring Data系列的一部分官紫,可以輕松實(shí)現(xiàn)基于JPA的存儲(chǔ)庫(kù)。它用來(lái)處理對(duì)基于JPA的數(shù)據(jù)訪問(wèn)層支持增強(qiáng)胞谈。有了它薇宠,我們能更加容易構(gòu)建出,使用數(shù)據(jù)訪問(wèn)技術(shù)的Spring應(yīng)用程序统舀。
在相當(dāng)長(zhǎng)的一段時(shí)間內(nèi)匆骗,實(shí)現(xiàn)應(yīng)用程序的數(shù)據(jù)訪問(wèn)層一直很麻煩。必須編寫(xiě)太多樣板代碼來(lái)執(zhí)行簡(jiǎn)單查詢以及執(zhí)行分頁(yè)和審計(jì)誉简。Spring Data JPA旨在通過(guò)減少實(shí)際需要的工作量來(lái)顯著改善數(shù)據(jù)訪問(wèn)層的實(shí)現(xiàn)碉就。作為開(kāi)發(fā)人員,只需編寫(xiě)存儲(chǔ)庫(kù)接口闷串,包括自定義查找器方法铝噩,Spring將自動(dòng)提供實(shí)現(xiàn)。
特征
- 基于Spring和JPA構(gòu)建存儲(chǔ)庫(kù)的復(fù)雜支持
- 支持Querydsl謂詞窿克,從而支持類型安全的JPA查詢
- 透明審核域類
- 分頁(yè)支持骏庸,動(dòng)態(tài)查詢執(zhí)行,集成自定義數(shù)據(jù)訪問(wèn)代碼的能力
- 支持基于XML的實(shí)體映射
- 基于JavaConfig的存儲(chǔ)庫(kù)配置年叮,介紹@EnableJpaRepositories
1具被、依賴
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
implementation('org.hibernate:hibernate-jcache')
implementation('org.ehcache:ehcache')
此處需要注意,如果高版本的 SpringBoot只损,我們需要使用org.hibernate:hibernate-jcache一姿,而不是org.hibernate:hibernate-ehcache。否則啟動(dòng)異常
2跃惫、配置
2.1 ehcache配置
在resources 目錄中創(chuàng)建ehcache.xml配置ehcache叮叹。
這里需要注意的是,百度里面的文章大部分都是告訴大家怎么配置ehcache2.x,但是ehcache的2.x和3.x完全就兩碼事情了爆存,所以如果不經(jīng)過(guò)思考蛉顽,一通 copy 那栽跟頭是肯定得了。
下面我貼出我項(xiàng)目中使用的配置:
<?xml version="1.0" encoding="UTF-8"?>
<eh:config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:eh='http://www.ehcache.org/v3'
xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.3.xsd">
<!--指定緩存目錄-->
<eh:persistence directory="${java.io.tmpdir}/cfa-cache-data"/>
<!--緩存模板-->
<eh:cache-template name="default">
<eh:expiry>
<eh:ttl unit="seconds">600</eh:ttl>
</eh:expiry>
<eh:resources>
<!--堆內(nèi)內(nèi)存可以放2000個(gè)條目先较,超出部分堆外100MB-->
<eh:heap unit="entries">2000</eh:heap>
<eh:offheap unit="MB">100</eh:offheap>
</eh:resources>
</eh:cache-template>
<!--實(shí)際的緩存區(qū)間携冤,繼承了default緩存模板,cfa 完全使用模板默認(rèn)-->
<eh:cache alias="cfa" uses-template="default">
</eh:cache>
<!--下面兩個(gè)繼承了default緩存模板悼粮,但覆蓋了緩存的過(guò)期時(shí)間-->
<eh:cache alias="authority" uses-template="default">
<eh:expiry>
<eh:ttl unit="hours">1</eh:ttl>
</eh:expiry>
</eh:cache>
<eh:cache alias="lapp_service" uses-template="default">
<eh:expiry>
<eh:ttl unit="hours">24</eh:ttl>
</eh:expiry>
</eh:cache>
</eh:config>
2.2 SpringBoot配置
由于data-jpa 使用了 hibernate 作為底層實(shí)現(xiàn),所以我們配置二級(jí)緩存其實(shí)就是基于 hibernate 的二級(jí)緩存曾棕。
hibernate 在啟動(dòng)時(shí)扣猫,會(huì)默認(rèn)讀取 classpath 目錄下面的hibernate.properties配置。所以我們需要在resources目錄中創(chuàng)建這個(gè)文件翘地。
文件內(nèi)容:
hibernate.format_sql=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.region_prefix=cfa_repo_
hibernate.cache.region.factory_class=org.hibernate.cache.jcache.internal.JCacheRegionFactory
hibernate.cache.provider_configuration_file_resource_path=ehcache.xml
hibernate.cache.use_structured_entries=true
hibernate.generate_statistics=false
hibernate.javax.cache.missing_cache_strategy=create
此處我們需要注意的是factory_class的配置申尤,如果你去百度hibernate的二級(jí)緩存配置,就這一項(xiàng)你可以百度出五個(gè)不同的版本衙耕。然并卵瀑凝,基本沒(méi)有對(duì)的。因?yàn)檫@個(gè)版本更新太快臭杰,類也變得有點(diǎn)快粤咪。經(jīng)過(guò)一番嘗試之后,得出我當(dāng)前這個(gè)版本factory_class配置有效的類渴杆,所以注意寥枝,不一定 copy我的就有用。
2.3 啟用注解
//在Boot 項(xiàng)目的啟動(dòng)類上標(biāo)注@EnableCaching來(lái)開(kāi)啟緩存功能
@SpringBootApplication
@EnableCaching
public class LappApplication {
public static void main(String[] args) {
SpringApplication.run(LappApplication.class, args);
}
}
3磁奖、應(yīng)用緩存
在需要開(kāi)啟二級(jí)緩存的實(shí)體類上大標(biāo)注
@Entity
@Table(name = "lapp_unit"))
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Unit {
....
}
嗯囊拜,然后就沒(méi)有然后了。在使用 jpa 的 findById 方法進(jìn)行查詢和 save 進(jìn)行更新數(shù)據(jù)的時(shí)候比搭,hibernate 就會(huì)使用到緩存了冠跷。具體測(cè)試,可以自行玩耍身诺。