一、导入依赖
1 <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache --> 2 <dependency> 3 <groupId>org.hibernate</groupId> 4 <artifactId>hibernate-ehcache</artifactId> 5 <version>5.6.7.Final</version> 6 </dependency>
二、ehcache.xml
1 <?xml version="1.0" encoding="utf-8" ?> 2 <ehcache> 3 4 <diskStore path="d:/hibernate_cache"></diskStore> 5 <!-- 默认缓存配置 --> 6 <defaultCache maxElementsInMemory="10000" 7 maxElementsOnDisk="0" 8 eternal="false" 9 timeToIdleSeconds="120" 10 timeToLiveSeconds="120" 11 diskPersistent="false" 12 diskSpoolBufferSizeMB="30" 13 overflowToDisk="true" 14 memoryStoreEvictionPolicy="LRU" 15 /> 16 17 <!-- user缓存配置 --> 18 <cache name = "UserTest" 19 maxElementsInMemory="10000" 20 eternal="false" 21 timeToIdleSeconds="120" 22 timeToLiveSeconds="120" 23 overflowToDisk="true"/> 24 <!--maxElementsInMemory 在内存中缓存的最大对象数量(正整数)--> 25 <!--maxElementsOnDisk 在磁盘中缓存的最大对象数量(正整数)默认为0 表示不限制 --> 26 <!--eternal 设置缓存对象是否永久保存--> 27 <!--timeToIdleSeconds 设置对象在多长时间没有被访问就会失效 单位为秒 --> 28 <!--timeToLiveSeconds 设置对象的存活时间 单位为秒 --> 29 <!--overflowToDisk 如果内存中数据超出内存限制 是否要缓存到磁盘上--> 30 <!--diskPersistent 设置是否在磁盘上持久化--> 31 <!--diskSpoolBufferSizeMB 设置使用的磁盘大小 每个cache使用各自的spool 默认值为30 --> 32 <!--memoryStoreEvictionPolicy 设置向磁盘缓存的策略 默认为LRU 可选FIFO、LFU--> 33 <!--FIFO: 先进先出 / LFU: 最少被使用 / LRU: 最近最少使用(默认)--> 34 35 </ehcache>
使用user级别ehcache示例(一般就用默认的)
<collection-cache region="UserTest" collection="com.sdkj.hibernate.domain.HTeacherEntity.studentEntities" usage="read-write"/>
三、hibernate.cfg.xml(全粘进来 注意配置所在位置)
1 package com.sdkj.hibernate.controller; 2 3 import com.sdkj.hibernate.domain.HTeacherEntity; 4 import com.sdkj.hibernate.util.CommonUtil; 5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 import org.hibernate.Transaction; 8 import org.junit.jupiter.api.Test; 9 10 /** 11 * @Author wangshuo 12 * @Date 2022/4/10, 16:55 13 * Please add a comment 14 */ 15 public class TestCache extends CommonUtil { 16 17 //开启类级别的二级缓存就会只查一次 18 @Test 19 public void testLei(){ 20 21 SessionFactory sessionFactory = getSessionFactory(); 22 Session session = sessionFactory.openSession(); 23 Transaction transaction = session.beginTransaction(); 24 25 System.out.println(session.get(HTeacherEntity.class, 6).getTname()); 26 27 transaction.commit(); 28 session.close(); 29 30 session = sessionFactory.openSession(); 31 transaction = session.beginTransaction(); 32 33 //一级缓存时session级别的缓存,我关闭了session再开启一级缓存就会失效 34 System.out.println(session.get(HTeacherEntity.class, 6).getTname()); 35 } 36 37 //开启类级别的二级缓存 并且 开启集合级别的二级缓存就会只查一次 38 @Test 39 public void testJiHe(){ 40 41 SessionFactory sessionFactory = getSessionFactory(); 42 Session session = sessionFactory.openSession(); 43 Transaction transaction = session.beginTransaction(); 44 45 HTeacherEntity load = session.load(HTeacherEntity.class, 2); 46 System.out.println(load.getStudentEntities()); 47 48 session = sessionFactory.openSession(); 49 transaction = session.beginTransaction(); 50 51 HTeacherEntity load1 = session.load(HTeacherEntity.class, 2); 52 System.out.println(load1.getStudentEntities()); 53 } 54 }
1.类级别的二级缓存
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="connection.url">jdbc:mysql://localhost:3306/678</property> 8 <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> 9 <property name="connection.username">root</property> 10 <property name="connection.password">678678</property> 11 <!-- <property name="connection.username"/> --> 12 <!-- <property name="connection.password"/> --> 13 14 <!-- DB schema will be updated if needed --> 15 <!-- <property name="hibernate.hbm2ddl.auto">update</property> --> 16 <!-- hibernate配置 --> 17 <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property> 18 <property name="hibernate.show_sql">true</property> 19 <property name="hibernate.format_sql">true</property> 20 <!-- 表结构变化时执行修改表命令 不删表重建 --> 21 <property name="hibernate.hbm2ddl.auto">update</property> 22 <!-- 设置每次查询从数据库中读取的条数 MySQL数据库不支持(所以企业级涉及大数据量数据的存储基本采用Oracle数据库) --> 23 <property name="hibernate.jdbc.fetch_size">100</property> 24 <!-- 设置批量删除和批量更新 --> 25 <property name="hibernate.jdbc.batch_size">30</property> 26 27 <!-- c3p0属性配置 --> 28 <!-- 配置启用c3p0连接池 --> 29 <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property> 30 <property name="hibernate.c3p0.max_size">500</property> 31 <property name="hibernate.c3p0.min_size">1</property> 32 <!-- 设置缓存里最多有多少个statement对象 --> 33 <property name="hibernate.c3p0.max_statements">100</property> 34 <!-- 超时时间 --> 35 <property name="hibernate.c3p0.timeout">5000</property> 36 <!-- 配置每隔多少秒 扫描连接池所有连接,清理超时连接 --> 37 <property name="hibernate.c3p0.idle_test_period">2000</property> 38 <!-- 配置当连接池连接数耗尽时,每次向MySQL申请多少条连接 --> 39 <property name="hibernate.c3p0.acquire_increment">100</property> 40 41 <!--缓存配置--> 42 <property name="hibernate.cache.use_second_level_cache">true</property> 43 <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.internal.EhcacheRegionFactory</property> 44 45 <mapping resource="T2UserEntity.hbm.xml"/> 46 <mapping class="com.sdkj.hibernate.domain.T2UserEntity"/> 47 <mapping resource="HStudentEntity.hbm.xml"/> 48 <mapping class="com.sdkj.hibernate.domain.HStudentEntity"/> 49 <mapping resource="HTeacherEntity.hbm.xml"/> 50 <mapping class="com.sdkj.hibernate.domain.HTeacherEntity"/> 51 <mapping class="com.sdkj.hibernate.domain.HCardEntity"/> 52 <mapping resource="CardEntity.hbm.xml"/> 53 <mapping class="com.sdkj.hibernate.domain.HPersonEntity"/> 54 <mapping resource="PersonEntity.hbm.xml"/> 55 <!--<mapping class="com.sdkj.hibernate.domain.TeacherEntity_Many_To_Many"/>--> 56 <mapping class="com.sdkj.hibernate.domain.CourseEntity"/> 57 <mapping resource="CourseEntity.hbm.xml"/> 58 <mapping class="com.sdkj.hibernate.domain.CourseEntity_Many_To_Many"/> 59 <mapping resource="CourseEntityMTM.hbm.xml"/> 60 <mapping class="com.sdkj.hibernate.domain.SXTeacherEntity_Many_To_Many"/> 61 <mapping resource="SXTeacherEntityMTM.hbm.xml"/> 62 <mapping class="com.sdkj.hibernate.domain.Test_Student"/> 63 <mapping resource="TestStudent.hbm.xml"/> 64 <mapping class="com.sdkj.hibernate.domain.TYhOrder1Entity"/> 65 <mapping resource="TYhOrder1Entity.hbm.xml"/> 66 67 <!-- 配置开启二级缓存的类 --> 68 <class-cache class="com.sdkj.hibernate.domain.HTeacherEntity" usage="read-write"></class-cache> 69 </session-factory> 70 </hibernate-configuration>
2.集合级别的二级缓存
1 <class-cache class="com.sdkj.hibernate.domain.HStudentEntity" usage="read-write"/> 2 <collection-cache collection="com.sdkj.hibernate.domain.HTeacherEntity.studentEntities" usage="read-write"/>
四、查询级别的二级缓存
1 //带条件的查询 2 //开启类级别的二级缓存 并且 开启集合级别的二级缓存 并且 开启查询级别的二级缓存 就会只查一次 3 @Test 4 public void testChaXun(){ 5 6 SessionFactory sessionFactory = getSessionFactory(); 7 Session session = sessionFactory.openSession(); 8 Transaction transaction = session.beginTransaction(); 9 10 String hql = " select new TYhOrder1Entity(e.id,e.price,e.date) from TYhOrder1Entity e where 1 = 1 and e.id < 50"; 11 Query<TYhOrder1Entity> query = session.createQuery(hql); 12 13 //query.setCacheable(true);开启查询二级缓存(依赖于二级缓存) 14 query.setCacheable(true); 15 for (TYhOrder1Entity tYhOrder1Entity : query.list()) { 16 17 System.out.println("id:"+tYhOrder1Entity.getId()+" price:"+tYhOrder1Entity.getPrice() 18 +" date:"+tYhOrder1Entity.getDate() 19 ); 20 } 21 22 //再调用一次 23 for (TYhOrder1Entity tYhOrder1Entity : query.list()) { 24 25 System.out.println("id:"+tYhOrder1Entity.getId()+" price:"+tYhOrder1Entity.getPrice() 26 +" date:"+tYhOrder1Entity.getDate() 27 ); 28 } 29 }
1 <!-- 查询缓存 --> 2 <property name="hibernate.cache.use_query_cache">true</property>
1 @Test 2 public void testShiJianChuo(){ 3 4 SessionFactory sessionFactory = getSessionFactory(); 5 Session session = sessionFactory.openSession(); 6 Transaction transaction = session.beginTransaction(); 7 8 String hql = " select new TYhOrder1Entity(e.id,e.price,e.date) from TYhOrder1Entity e where 1 = 1 and e.id < 10"; 9 Query<TYhOrder1Entity> query = session.createQuery(hql); 10 11 //query.setCacheable(true);开启查询二级缓存(依赖于二级缓存) 12 query.setCacheable(true); 13 for (TYhOrder1Entity tYhOrder1Entity : query.list()) { 14 15 System.out.println("id:"+tYhOrder1Entity.getId()+" price:"+tYhOrder1Entity.getPrice() 16 +" date:"+tYhOrder1Entity.getDate() 17 ); 18 } 19 20 //两次查询中间改一下结果之一 数据的值 21 TYhOrder1Entity load = session.load(TYhOrder1Entity.class, 6); 22 load.setDate(new Date()); 23 24 //再调用一次 25 //此时hibernate发现了,小伙想搞事情?? hibernate就会再查一次 26 for (TYhOrder1Entity tYhOrder1Entity : query.list()) { 27 28 System.out.println("id:"+tYhOrder1Entity.getId()+" price:"+tYhOrder1Entity.getPrice() 29 +" date:"+tYhOrder1Entity.getDate() 30 ); 31 } 32 }
本文来自博客园,作者:荣慕平,转载请注明原文链接:https://www.cnblogs.com/rongmuping/articles/16127721.html