1、EhCache的主要特性:java
快速 2. 簡單 3. 多種緩存策略 4. 緩存數據有兩級:內存和磁盤,所以無需擔憂容量問題sql
5. 緩存數據會在虛擬機重啓的過程當中寫入磁盤 6. 能夠經過RMI、可插入API等方式進行分佈式緩存數據庫
7. 具備緩存和緩存管理器的偵聽接口 8. 支持多緩存管理器實例,以及一個實例的多個緩存區域 緩存
9. 提供Hibernate的緩存實現session
2、hibernate.cfg.xml 內容:oracle
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Hibernate 鏈接數據庫的基本信息 --> <property name="connection.username">scott</property> <property name="connection.password">java</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <!-- Hibernate 的基本配置 --> <!-- Hibernate 使用的數據庫方言 --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- 運行時是否打印 SQL --> <property name="show_sql">true</property> <!-- 運行時是否格式化 SQL --> <property name="format_sql">true</property> <!-- 生成數據表的策略 --> <property name="hbm2ddl.auto">update</property> <!-- 設置 Hibernate 的事務隔離級別 --> <property name="connection.isolation">2</property> <!-- 刪除對象後, 使其 OID 置爲 null --> <property name="use_identifier_rollback">true</property> <!-- 配置 C3P0 數據源 --> <!-- <property name="hibernate.c3p0.max_size">10</property> <property name="hibernate.c3p0.min_size">5</property> <property name="c3p0.acquire_increment">2</property> <property name="c3p0.idle_test_period">2000</property> <property name="c3p0.timeout">2000</property> <property name="c3p0.max_statements">10</property> --> <!-- 設定 JDBC 的 Statement 讀取數據的時候每次從數據庫中取出的記錄條數 --> <property name="hibernate.jdbc.fetch_size">100</property> <!-- 設定對數據庫進行批量刪除,批量更新和批量插入的時候的批次大小 --> <property name="jdbc.batch_size">30</property> <!-- 啓用二級緩存 --> <property name="cache.use_second_level_cache">true</property> <!-- 配置使用的二級緩存的產品 --> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <!-- 配置啓用查詢緩存 --> <property name="cache.use_query_cache">true</property> <!-- 配置管理 Session 的方式 --> <property name="current_session_context_class">thread</property> <!-- 須要關聯的 hibernate 映射文件 .hbm.xml --> <mapping resource="com/atguigu/hibernate/entities/Department.hbm.xml"/> <mapping resource="com/atguigu/hibernate/entities/Employee.hbm.xml"/> <class-cache usage="read-write" class="com.atguigu.hibernate.entities.Employee"/> <class-cache usage="read-write" class="com.atguigu.hibernate.entities.Department"/> <collection-cache usage="read-write" collection="com.atguigu.hibernate.entities.Department.emps"/> </session-factory> </hibernate-configuration>
3、ehcache.xml 內容:app
<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 --> <!-- 指定一個目錄:當 EHCache 把數據寫到硬盤上時, 將把數據寫到這個目錄下. --> <diskStore path="d:\\tempDirectory"/> <!--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" /> <!-- 設定具體的命名緩存的數據過時策略。每一個命名緩存表明一個緩存區域 緩存區域(region):一個具備名稱的緩存塊,能夠給每個緩存塊設置不一樣的緩存策略。 若是沒有設置任何的緩存區域,則全部被緩存的對象,都將使用默認的緩存策略。即:<defaultCache.../> Hibernate 在不一樣的緩存區域保存不一樣的類/集合。 對於類而言,區域的名稱是類名。如:com.atguigu.domain.Customer 對於集合而言,區域的名稱是類名加屬性名。如com.atguigu.domain.Customer.orders --> <!-- name: 設置緩存的名字,它的取值爲類的全限定名或類的集合的名字 maxElementsInMemory: 設置基於內存的緩存中可存放的對象最大數目 eternal: 設置對象是否爲永久的, true表示永不過時, 此時將忽略timeToIdleSeconds 和 timeToLiveSeconds屬性; 默認值是false timeToIdleSeconds:設置對象空閒最長時間,以秒爲單位, 超過這個時間,對象過時。 當對象過時時,EHCache會把它從緩存中清除。若是此值爲0,表示對象能夠無限期地處於空閒狀態。 timeToLiveSeconds:設置對象生存最長時間,超過這個時間,對象過時。 若是此值爲0,表示對象能夠無限期地存在於緩存中. 該屬性值必須大於或等於 timeToIdleSeconds 屬性值 overflowToDisk:設置基於內存的緩存中的對象數目達到上限後,是否把溢出的對象寫到基於硬盤的緩存中 --> <cache name="com.atguigu.hibernate.entities.Employee" maxElementsInMemory="1" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" /> <cache name="com.atguigu.hibernate.entities.Department" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" /> <cache name="com.atguigu.hibernate.entities.Department.emps" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" /> </ehcache>
4、Employee.java 和 Department.java 實體類dom
public class Employee { private Integer id; private String name; private float salary; private String email; private Department dept; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Department getDept() { return dept; } public void setDept(Department dept) { this.dept = dept; } @Override public String toString() { return "Employee [id=" + id + "]"; } public Employee(String email, float salary, Department dept) { super();
public class Department { private Integer id; private String name; private Set<Employee> emps = new HashSet<>(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Employee> getEmps() { return emps; } public void setEmps(Set<Employee> emps) { this.emps = emps; } @Override public String toString() { return "Department [id=" + id + "]"; } }
5、Employee.hbm.xml 和 Department.hbm.xml 的配置文件分佈式
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.atguigu.hibernate.entities.Employee" table="GG_EMPLOYEE"> <cache usage="read-write"/> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="salary" type="float"> <column name="SALARY" /> </property> <property name="email" type="java.lang.String"> <column name="EMAIL" /> </property> <many-to-one name="dept" class="com.atguigu.hibernate.entities.Department"> <column name="DEPT_ID" /> </many-to-one> </class> <query name="salaryEmps"><![CDATA[FROM Employee e WHERE e.salary > :minSal AND e.salary < :maxSal]]></query> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.atguigu.hibernate.entities.Department" table="GG_DEPARTMENT"> <cache usage="read-write"/> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <set name="emps" table="GG_EMPLOYEE" inverse="true" lazy="true"> <key> <column name="DEPT_ID" /> </key> <one-to-many class="com.atguigu.hibernate.entities.Employee" /> </set> </class> </hibernate-mapping>