zoukankan      html  css  js  c++  java
  • Hibernate的二级缓存(SessionFaction的外置缓存)-----Helloword

    1. 使用 Hibernate 二级缓存的步骤:

    1). 加入二级缓存插件的 jar 包及配置文件:

    I. 复制 hibernate-release-4.2.4.Finalliboptionalehcache*.jar 到当前 Hibrenate 应用的类路径下.
    II. 复制 hibernate-release-4.2.4.Finalprojectetcehcache.xml 到当前 WEB 应用的类路径下

    2). 配置 hibernate.cfg.xml

    I. 配置启用 hibernate 的二级缓存
    <property name="cache.use_second_level_cache">true</property>

    II. 配置hibernate二级缓存使用的产品
    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

    III. 配置对哪些类使用 hibernate 的二级缓存
    <class-cache usage="read-write" class="com.atguigu.hibernate.entities.Employee"/>

    实际上也可以在 .hbm.xml 文件中配置对哪些类使用二级缓存, 及二级缓存的策略是什么. (优先推荐使用这种)
    在<class>的标签里加一个这样的标签<cache usage="read-write"/>
    测试代码:

    @Test
        public void TestSecondLevelCache(){
            Employee employee=(Employee) session.get(Employee.class, 10);
            System.out.println(employee.getName());
            transaction.commit();
            session.close();
            
            session=sessionFactory.openSession();
            transaction=session.beginTransaction();
            
            Employee employee2=(Employee) session.get(Employee.class, 10);
            System.out.println(employee2.getName());
            
        }

    测试效果:

    INFO: HHH000232: Schema update complete
    Hibernate: 
        select
            employee0_.ID as ID1_1_0_,
            employee0_.NAME as NAME2_1_0_,
            employee0_.SALARY as SALARY3_1_0_,
            employee0_.EMAIL as EMAIL4_1_0_,
            employee0_.DEPT_ID as DEPT_ID5_1_0_ 
        from
            GG_EMPLOYEE employee0_ 
        where
            employee0_.ID=?
    JJ
    JJ
  • 相关阅读:
    利用js在Table中追加数据
    C#API配置跨域
    C#linq查询DataTable
    erlang格式化输出
    erlang 的源代码保护机制
    MP3格式音频文件结构解析
    使用异步 I/O 大大提高应用程序的性能
    虚拟机安装mac 关键是换引导
    C/C++规则整理
    字节对齐
  • 原文地址:https://www.cnblogs.com/jeremy-blog/p/4021703.html
Copyright © 2011-2022 走看看