• Hibernate二级缓存


    1. 基本配置:
      pom.xml中引入与当前hibernate对应版本的hibernate-ehcache包
      1. <!-- hibernate缓存包 -->
      2. <dependency>
      3. <groupId>org.hibernate</groupId>
      4. <artifactId>hibernate-ehcache</artifactId>
      5. <version>${hibernate.version}</version>
      6. </dependency>
    2. 在hibernate.cfg.xml中开启二级缓存


        true

      org.hibernate.cache.ehcache.internal.EhcacheRegionFactory
      注:查看该 类的方法一般是到引入的包中找到EhcacheRegionFactory.class这个类

    3. 根目录中创建ehcache.xml



       
      1. "1.0" encoding="UTF-8"?>
      2. <ehcache>
      3. <diskStore path="G:/ehcache/"/>
      4. <defaultCache maxElementsInMemory="10000"
      5. eternal="false"
      6. timeToIdleSeconds="300"
      7. timeToLiveSeconds="600"
      8. overflowToDisk="true"/>
      9. <cache name="testCache"
      10. eternal="false"
      11. maxElementsInMemory="100"
      12. timeToIdleSeconds="1200"
      13. timeToLiveSeconds="1200"
      14. overflowToDisk="false">
      15. cache>
      16. ehcache>

    4. 配置指定要使用二级缓存的方式有两种
      a) 在hibernate.cfg.xml中指定要使用二级缓存的类
      1. <!-- 类级二级缓存:指定哪些类要使用二级缓存 -->
      2. <class-cache usage="read-write" class="com.restfullDemo.model.User"/>
      3. <class-cache usage="read-write" class="com.restfullDemo.model.Department"/>
      4. <!-- 集合级二级缓存,指定哪个类中的集合属性使用二级缓级,前提是该属性的类必须已设置二级缓存中 -->
      5. <collection-cache usage="read-write" collection="com.restfullDemo.model.Department.users"/>

      b) 也可以在类的对应映射文件中时行配置
       

      1. 类的二级缓存配置
      2. <class name="com.restfullDemo.model.User" table="t_user">
      3. <cache usage="read-write"/>
      4. ......
      5. </class>
      1. 集合级的二级缓存配置
      2. <hibernate-mapping>
      3. <class name="com.restfullDemo.model.Department" table="t_department">
      4. <id name="id" type="int">
      5. <column name="id" />
      6. <generator class="assigned" />
      7. </id>
      8. <property name="name" type="java.lang.String">
      9. <column name="name" />
      10. </property>
      11. <set name="users" table="t_user" inverse="false" lazy="false" >
      12. <cache usage="read-write"/>
      13. <key>
      14. <column name="dpt_id" />
      15. </key>
      16. <one-to-many class="com.restfullDemo.model.User" />
      17. </set>
      18. </class>
      19. </hibernate-mapping>


      实例
       

      1. public class User {
      2. private int id;
      3. private String name;
      4. private int age;
      5. private Department dpt;
      6. setter、getter.....
      7. }
      1. public class Department {
      2. private int id;
      3. private String name;
      4. private Set<User> users = new HashSet<User>();
      5. setter、getter......
      6. }

      hibernet.cfg.xml
       

      1. "1.0" encoding="UTF-8"?>
      2. hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
      4. <hibernate-configuration>
      5. <session-factory name="">
      6. <property name="hibernate.dialect">xxxxxproperty>
      7. <property name="hibernate.connection.driver_class">xxxxproperty>
      8. <property name="hibernate.connection.url">xxxxproperty>
      9. <property name="hibernate.connection.username">xxxxproperty>
      10. <property name="hibernate.connection.password">123456property>
      11. <property name="hibernate.show_sql">trueproperty>
      12. <property name="hibernate.format_sql">trueproperty>
      13. <property name="hibernate.hbm2ddl.auto">updateproperty>
      14. <property name="hibernate.cache.use_second_level_cache">trueproperty>
      15. <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.internal.EhcacheRegionFactoryproperty>
      16. <mapping resource="com/restfullDemo/mappingResources/User.hbm.xml"/>
      17. <mapping resource="com/restfullDemo/mappingResources/Department.hbm.xml"/>
      18. <class-cache usage="read-write" class="com.restfullDemo.model.User"/>
      19. <class-cache usage="read-write" class="com.restfullDemo.model.Department"/>
      20. <collection-cache usage="read-write" collection="com.restfullDemo.model.Department.users"/>
      21. session-factory>
      22. hibernate-configuration>

    查询缓存

    查询缓存依赖于二级缓存,二级缓存没有开,查询缓存会失效,同理当设置完类级、集合集的二级缓存后,二级缓存没有开,在进行HQL的Query查询时,缓存也会失效,配置如下:
    在hibernet.cfg.xml中添加

    1. <!-- 缓存设置 -->
    2. <property name="hibernate.cache.use_second_level_cache">true</property>
    3. <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.internal.EhcacheRegionFactory</property>
    4. <!-- 开启查询二级缓存 -->
    5. <property name="hibernate.cache.use_query_cache">true</property>

    在查询语句中开启查询缓存
     

    1. public void getallUser() {
    2. String hql="from User";
    3. Query query=session.createQuery(hql);
    4. //开启查询缓存
    5. query.setCacheable(true);
    6. List<User> users1=query.list();
    7. System.out.println(Arrays.toString(users1.toArray()));
    8. List<User> users2=query.list();
    9. System.out.println(Arrays.toString(users2.toArray()));
    10. }

  • 相关阅读:
    NLP 自古以来的各预训练模型 (PTMs) 和预训练任务小结
    Orcad Capture Cadence 新建原理图多部分smybol和Homogeneous、Heterogeneous类型介绍教程
    2022年中科大细胞生物学实验原理往年题复习参考
    软件定义汽车,通信连接世界 | 2024汽车软件与通信大会开幕
    计算机毕业设计(附源码)python医院预约挂号系统
    go语言中的文件file操作
    XFF漏洞利用([SWPUCTF 2021 新赛]Do_you_know_http)
    Java实现负载均衡算法--轮询和加权轮询
    但凡项目团队有个“二舅”,项目交付哪用得着又焦又愁
    flask+Pyecharts+ajax实现分tab页展示多图
  • 原文地址:https://blog.csdn.net/jingde528/article/details/127591732