Spring Data JPA是更大的Spring Data系列的一部分,可以輕松實現(xiàn)基于JPA的存儲庫。它用來處理對基于JPA的數(shù)據(jù)訪問層支持增強。有了它,我們能更加容易構建出,使用數(shù)據(jù)訪問技術的Spring應用程序。
在相當長的一段時間內,實現(xiàn)應用程序的數(shù)據(jù)訪問層一直很麻煩。必須編寫太多樣板代碼來執(zhí)行簡單查詢以及執(zhí)行分頁和審計。Spring Data JPA旨在通過減少實際需要的工作量來顯著改善數(shù)據(jù)訪問層的實現(xiàn)。作為開發(fā)人員,只需編寫存儲庫接口,包括自定義查找器方法,Spring將自動提供實現(xiàn)。
特征
- 基于Spring和JPA構建存儲庫的復雜支持
- 支持Querydsl謂詞,從而支持類型安全的JPA查詢
- 透明審核域類
- 分頁支持,動態(tài)查詢執(zhí)行,集成自定義數(shù)據(jù)訪問代碼的能力
- 支持基于XML的實體映射
- 基于JavaConfig的存儲庫配置,介紹@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。否則啟動異常
2、配置
2.1 ehcache配置
在resources 目錄中創(chuàng)建ehcache.xml配置ehcache。
這里需要注意的是,百度里面的文章大部分都是告訴大家怎么配置ehcache2.x,但是ehcache的2.x和3.x完全就兩碼事情了,所以如果不經過思考,一通 copy 那栽跟頭是肯定得了。
下面我貼出我項目中使用的配置:
<?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>
<!--堆內內存可以放2000個條目,超出部分堆外100MB-->
<eh:heap unit="entries">2000</eh:heap>
<eh:offheap unit="MB">100</eh:offheap>
</eh:resources>
</eh:cache-template>
<!--實際的緩存區(qū)間,繼承了default緩存模板,cfa 完全使用模板默認-->
<eh:cache alias="cfa" uses-template="default">
</eh:cache>
<!--下面兩個繼承了default緩存模板,但覆蓋了緩存的過期時間-->
<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 作為底層實現(xiàn),所以我們配置二級緩存其實就是基于 hibernate 的二級緩存。
hibernate 在啟動時,會默認讀取 classpath 目錄下面的hibernate.properties配置。所以我們需要在resources目錄中創(chuàng)建這個文件。
文件內容:
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的二級緩存配置,就這一項你可以百度出五個不同的版本。然并卵,基本沒有對的。因為這個版本更新太快,類也變得有點快。經過一番嘗試之后,得出我當前這個版本factory_class配置有效的類,所以注意,不一定 copy我的就有用。
2.3 啟用注解
//在Boot 項目的啟動類上標注@EnableCaching來開啟緩存功能
@SpringBootApplication
@EnableCaching
public class LappApplication {
public static void main(String[] args) {
SpringApplication.run(LappApplication.class, args);
}
}
3、應用緩存
在需要開啟二級緩存的實體類上大標注
@Entity
@Table(name = "lapp_unit"))
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Unit {
....
}
嗯,然后就沒有然后了。在使用 jpa 的 findById 方法進行查詢和 save 進行更新數(shù)據(jù)的時候,hibernate 就會使用到緩存了。具體測試,可以自行玩耍。