• hibernate基础学习---hierbnate2级缓存


    1:开启二级缓存sessionFactory需要安装jar包

    2:在实体类配置文件添加(配置二级缓存)。我的配置文件是Account.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    <!-- name属性指定类名(全限定名) table指明表名,不指明table数据默认的表名和实体名一致 -->
    <class name="com.entity.Account" table="acc_tab">
    <!-- 配置二级缓存      重要点 -->
    <cache usage="read-only" region="com.entity.Account"/>
    <!-- 配置二级缓存  重要点 -->
    <!-- type指明当前字段的类型 name对应实体中的属性名 -->
    <id type="integer" name="id">
    <!-- 提供ID自增的策略 native会根据数据库自行判断 -->
    <generator class="native"/>
    </id>
    <property name="name" type="string"></property>
    <property name="age" type="integer"></property>
    </class>
    </hibernate-mapping>

    3:启用二级缓存 和启动二级缓存 ehcache插件。hibernate的核心配置文件hibernate.cfg.xml

    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
        
        <session-factory>
        
            <!--配置mysql数据库连接参数-->
            <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql:///test</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password"></property>
            
            <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
            
            <property name="hibernate.c3p0.max_size">10</property>
            <property name="hibernate.c3p0.min_size">5</property>
            <property name="hibernate.c3p0.timeout">5000</property>
            <property name="hibernate.c3p0.max_statements">10</property>
            <!-- 重点配制 -->
            <!-- 启用二级缓存 -->
            <property name="hibernate.cache.use_second_level_cache">true</property>
            <!-- 启动二级缓存 ehcache插件 -->
            <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
            <!-- 打开查询缓存 -->
            <property name="hibernate.cache.use_query_cache">true</property>
            <!-- 重点配制 -->    
            <property name="hibernate.show_sql">true</property>
            <property name="hibernate.format_sql">true</property>
            <mapping resource="com/entity/Account.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>

    4:再配置缓存文件。ehcache.xml

    对应的ehcache.xml内容文件如下

    <ehcache>
             <!-- 它的作用是在指定位置建数据表,当超过"maxElementsInMemory"设置的值时开始写数据 ,"maxElementsInMemory"是下面默认配置-->
        <diskStore path="/F:/"/>
        
        <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            />
       
        <cache name="com.entity.Account"
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="300"
            timeToLiveSeconds="600"
            overflowToDisk="true"
            />
    
         <!-- 设置默认的查询缓存区域的属性 -->
        <cache name="org.hibernate.cache.StandardQueryCache" 
            maxElementsInMemory="50"
            eternal="false" 
            timeToIdleSeconds="3600" 
            timeToLiveSeconds="7200" 
            overflowToDisk="true"/>
            
        <!-- 设置时间戳缓存区域的属性 -->
        <cache name="org.hibernate.cache.UpdateTimestampsCache" 
            maxElementsInMemory="5000"
            eternal="true" 
            overflowToDisk="true"/> 
            
        <!-- 设置自定义命名查询缓存区域的属性,一般可不配置 -->
        <cache name="myCacheRegion"
            maxElementsInMemory="1000"
            eternal="false"
            timeToIdleSeconds="300"
            timeToLiveSeconds="600"
            overflowToDisk="true"/>
    </ehcache>

    以上hibernate可是使用缓存

    5:启用查询缓存,用的比较少,适合在查询固定语句固定值时用

     

  • 相关阅读:
    极客互动极客技术专题【003期】:java mvc 增删改查 自动生成工具来袭
    协议命令网络工程试验一
    主题网站分享两套免费的超棒响应式HTML5网站模板
    算法结点图的多源点最短路问题和传递闭包之FloydWarshall算法 By ACReaper
    属性页面Flexbox布局的简单演示之二
    数据库性能Quest Performance Analysis Overview
    网站查看帮助查看本地表单元素样子的网站 Native Form Elements
    文件格式配置文件weka频繁模式挖掘使用方法
    风格希望分享8个超棒的免费界面UI设计
    方法事务applicationContext.xml
  • 原文地址:https://www.cnblogs.com/kaiwen/p/6720558.html
Copyright © 2020-2023  润新知