框架學習之JPA(五)
JPA是Java Persistence API的簡稱,中文名Java持久層API,是JDK 5.0注解或XML描述對象-關系表的映射關系,並將運行期的實體對象持久化到數據庫中。
Sun引入新的JPA ORM規范出於兩個原因:其一,簡化現有Java EE和Java SE應用開發工作;其二,Sun希望整合ORM技術,實現天下歸一。
學習視頻:尚硅谷框架jpa學習(有興趣的同學留言郵箱)
使用軟件:eclipse
Java版本:jdk8
本節目錄
五、JPA_二級緩存
1.添加jar包
2.修改persistence.xml配置文件
3添加ehcache.xml配置文件
4.啟動ehcache.xml配置文件,在persistence.xml配置文件中配置
5.需要緩存的類上添加注解
五、JPA_二級緩存
一級緩存:同一個entityManager中不用重復在數據庫中查詢同一個數據
二級緩存:
- <shared-cache-mode> 節點:若 JPA 實現支持二級緩存,該節點可以配置在當前的持久化單元中是否啟用二級緩存,可配置如下值:
- ALL:所有的實體類都被緩存
- NONE:所有的實體類都不被緩存.
- ENABLE_SELECTIVE:標識 @Cacheable(true) 注解的實體類將被緩存
- DISABLE_SELECTIVE:緩存除標識 @Cacheable(false) 以外的所有實體類
- UNSPECIFIED:默認值,JPA 產品默認值將被使用
1.添加jar包
2.修改persistence.xml配置文件
- 查看第四步,防止一的persistence.xml文件
3.添加ehcache.xml配置文件
<ehcache> <!-- Sets the path to the directory where cache .data files are created. If the path is a Java System Property it is replaced by its value in the running VM. The following properties are translated: user.home - User's home directory user.dir - User's current working directory java.io.tmpdir - Default temp file path --> <diskStore path="java.io.tmpdir"/> <!--Default Cache configuration. These will applied to caches programmatically created through the CacheManager. The following attributes are required for defaultCache: maxInMemory - Sets the maximum number of objects that will be created in memory eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used if the element is not eternal. Idle time is now - last accessed time timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used if the element is not eternal. TTL is now - creation time overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit. --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> <!--Predefined caches. Add your cache configuration settings here. If you do not have a configuration for your cache a WARNING will be issued when the CacheManager starts The following attributes are required for defaultCache: name - Sets the name of the cache. This is used to identify the cache. It must be unique. maxInMemory - Sets the maximum number of objects that will be created in memory eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used if the element is not eternal. Idle time is now - last accessed time timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used if the element is not eternal. TTL is now - creation time overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit. --> <!-- Sample cache named sampleCache1 This cache contains a maximum in memory of 10000 elements, and will expire an element if it is idle for more than 5 minutes and lives for more than 10 minutes. If there are more than 10000 elements it will overflow to the disk cache, which in this configuration will go to wherever java.io.tmp is defined on your system. On a standard Linux system this will be /tmp" --> <cache name="sampleCache1" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" /> <!-- Sample cache named sampleCache2 This cache contains 1000 elements. Elements will always be held in memory. They are not expired. --> <cache name="sampleCache2" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" /> --> <!-- Place configuration for your caches following --> </ehcache>
4.啟動ehcache.xml配置文件,在persistence.xml配置文件中配置
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="SGG-jpa" transaction-type="RESOURCE_LOCAL"> <!-- 配置使用什么 ORM 產品來作為 JPA 的實現 1. 實際上配置的是 javax.persistence.spi.PersistenceProvider 接口的實現類 2. 若 JPA 項目中只有一個 JPA 的實現產品, 則也可以不配置該節點. --> <provider>org.hibernate.ejb.HibernatePersistence</provider> <!-- 添加持久化類 --> <class>hue.edu.xiong.jpa.Customer</class> <class>hue.edu.xiong.jpa.Order</class> <class>hue.edu.xiong.jpa.Manager</class> <class>hue.edu.xiong.jpa.Department</class> <class>hue.edu.xiong.jpa.Item</class> <class>hue.edu.xiong.jpa.Category</class> <!-- 配置二級緩存的策略 ALL:所有的實體類都被緩存 NONE:所有的實體類都不被緩存. ENABLE_SELECTIVE:標識 @Cacheable(true)注解的實體類將被緩存 DISABLE_SELECTIVE:緩存除標識 @Cacheable(false) 以外的所有實體類 UNSPECIFIED:默認值,JPA產品默認值將被使用 --> <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode> <properties> <!-- 連接數據庫的基本信息 --> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql:///jpa" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="admin" /> <!-- 配置JPA實現產品的基本屬性,配置hibernate --> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="update" /> <!-- 二級緩存相關 --> <property name="hibernate.cache.use_second_level_cache" value="true" /> <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" /> <property name="hibernate.cache.use_query_cache" value="true" /> </properties> </persistence-unit> </persistence>
5.需要緩存的類上添加注解
測試案例:
@Test public void testSecondLevelCache(){ Customer customer1 = entityManager.find(Customer.class, 1); transaction.commit(); entityManager.close(); entityManager = entityManagerFactory.createEntityManager(); transaction = entityManager.getTransaction(); transaction.begin(); Customer customer2 = entityManager.find(Customer.class, 1); }