day37 07-Hibernate二级缓存:查询缓存

查询缓存是比二级缓存功能更强大的缓存.必须把二级缓存配置好之后才能用查询缓存,否则是用不了的.二级缓存主要是对类的缓存/对象缓存.查询缓存针对对象也是可以的(因为功能比二级缓存更强大),而且还可以针对类中的属性.

select cname from Customer.这种二级缓存是缓存不了的.二级缓存只能缓存的是整个的对象.而我们查询缓存里面可以缓存对象的属性.这是查询缓存与二级缓存最大的区别.

 

 

 

 

    @Test
    // 查询缓存的测试
    public void demo9(){
        Session session = HibernateUtils.getCurrentSession();
        Transaction tx = session.beginTransaction();
        //from Customer然后.list()已经证明可以往二级缓存放数据了,像这种已经没有什么意义了.
        Query query = session.createQuery("select c.cname from Customer c");//缓存的是对象的属性.二级缓存无法缓存对象的属性.
        //查询某些字段必须得使用查询缓存才能提升你的效率了.
        // 使用查询缓存:
        query.setCacheable(true);
        query.list();//如果你直接query.list(),那它是缓存不了的.
        
        tx.commit();
        
        session = HibernateUtils.getCurrentSession();
        tx = session.beginTransaction();
        
        query = session.createQuery("select c.cname from Customer c");
        query.setCacheable(true);
        query.list();
        
        tx.commit();
    }
<?xml version="1.0" encoding="UTF-8"?>
<!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>
    <!-- 必须去配置的属性 -->
    <!-- 配置数据库连接的基本信息: -->
    <property name="hibernate.connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
    <property name="hibernate.connection.url">
        jdbc:mysql:///hibernate3_day03
    </property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"></property>
    <!-- Hibernate的方言 -->
    <!-- 生成底层SQL不同的 -->
    <property name="hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
    </property>

    <!-- 可选的属性 -->
    <!-- 显示SQL -->
    <property name="hibernate.show_sql">true</property>
    <!-- 格式化SQL -->
    <property name="hibernate.format_sql">true</property>

    <property name="hibernate.connection.autocommit">false</property>
    <!-- hbm:映射 to DDL: create drop alter -->
    <property name="hibernate.hbm2ddl.auto">update</property>
    <!-- 设置事务的隔离级别 -->
    <property name="hibernate.connection.isolation">4</property>
    <!-- 设置本地Session -->
    <property name="hibernate.current_session_context_class">thread</property>

    <!-- C3P0连接池设定-->
    <!-- 使用c3po连接池  配置连接池提供的供应商-->
    <property name="connection.provider_class">
        org.hibernate.connection.C3P0ConnectionProvider
    </property>
    <!-- Hibernate中开启二级缓存 -->
    <property name="hibernate.cache.use_second_level_cache">true</property>
    <!-- 配置二级缓存的提供商 -->
    <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
    <!-- 配置查询缓存 -->
    <property name="hibernate.cache.use_query_cache">true</property>

    <!--在连接池中可用的数据库连接的最少数目 -->
    <property name="c3p0.min_size">5</property>
    <!--在连接池中所有数据库连接的最大数目  -->
    <property name="c3p0.max_size">20</property>
    <!--设定数据库连接的过期时间,以秒为单位,
        如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
    <property name="c3p0.timeout">120</property>
    <!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
    <property name="c3p0.idle_test_period">3000</property>

    <!-- 通知Hibernate加载那些映射文件 -->
    <mapping resource="cn/itcast/hibernate3/demo1/Customer.hbm.xml" />
    <mapping resource="cn/itcast/hibernate3/demo1/Order.hbm.xml" />

    <!-- 配置哪些类使用二级缓存 -->
    <class-cache usage="read-write" class="cn.itcast.hibernate3.demo1.Customer"/>
    <!--查询订单的类缓存区-->
      
    <class-cache usage="read-write" class="cn.itcast.hibernate3.demo1.Order"/>
    
    <!-- 集合缓冲区 
    所以在获得客户的订单的数量的时候,就没有再去发送SQL语句了。因为它已经把我们的集合orders也给缓存了。集合缓存区用来缓存对象中的集合。
    我们现在用的就是对象中的集合,所以它没有发送SQL语句。证明集合缓存区的数据是依赖于类缓存区的。
    -->

    <collection-cache usage="read-write" collection="cn.itcast.hibernate3.demo1.Customer.orders"/>

</session-factory>
</hibernate-configuration>

    <!-- 配置查询缓存 -->
    <property name="hibernate.cache.use_query_cache">true</property>

 

 

 

posted on 2017-04-14 18:37  绿茵好莱坞  阅读(164)  评论(0编辑  收藏  举报

导航