hibernate的二级缓存配置

来源:互联网 发布:全国计算机编程大赛 编辑:程序博客网 时间:2024/05/16 19:28
 首先进行hibernate的二级缓存配置
第一步:.hibernate.cfg.xml 配置文件

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
 <session-factory>
  <!-- JDBC驱动程序-->
  <property name="connection.driver_class">
   com.mysql.jdbc.Driver
  </property>
  <!-- 连接数据库的URL-->
  <property name="connection.url">

   jdbc:mysql://localhost:3306/wy17113?characterEncoding=utf8
  </property>

  <!--连接的登录名-->
  <property name="connection.username">root</property>

  <!--登录密码-->
  <property name="connection.password">root</property>

  <!-- C3P0连接池设定-->
  <property name="hibernate.connection.provider_class">
   org.hibernate.connection.C3P0ConnectionProvider
  </property>

  <property name="hibernate.c3p0.max_size">30</property>
  <property name="hibernate.c3p0.min_size">5</property>
  <property name="hibernate.c3p0.timeout">1800</property>
  <property name="hibernate.c3p0.max_statements">100</property>
  <property name="hibernate.c3p0.idle_test_period">120</property>
  <property name="hibernate.c3p0.acquire_increment">2</property>
  <property name="hibernate.c3p0.autoReconnect">true</property>

  <!--是否将运行期生成的SQL输出到日志以供调试-->
  <property name="show_sql">true</property>

  <!--指定连接的语言-->
  <property name="dialect">
   org.hibernate.dialect.MySQLDialect
  </property>
  
 
 <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
  <!-- 设置查询缓存-->
  <property name="hibernate.cache.use_query_cache">true</property>
  <!-- 设置二级缓存-->
  <property name="hibernate.cache.use_second_level_cache">
   true
  </property>

  <mapping resource="com/wy17113/model/entity/Wy17113.hbm.xml"></mapping>
 </session-factory>
</hibernate-configuration>

第二步:定义*.hbm.xml 在xml开始部分定义二级缓存

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
     <class name="com.wy17113.model.entity.Jok" table="joks" >
      
  <cache usage="read-write"/>
        <id name="id" type="java.lang.String">
            <column name="id" precision="10" scale="0" />
            <generator class="uuid" />
        </id>
        <property name="title" type="java.lang.String">
            <column name="title" length="300" />
        </property>
        <property name="content" type="java.lang.String">
            <column name="content" length="2000" />
        </property>
               
        <property name="subDate" type="java.lang.String">
            <column name="subDate" length="22" />
        </property>
  <property name="type" type="java.lang.Integer">
            <column name="type"/>
        </property>
        <property name="ding" type="java.lang.Integer">
            <column name="ding"/>
        </property>
        <property name="cai" type="java.lang.Integer">
            <column name="cai"/>
        </property>  
       
        <set name="commentSet" inverse="false">
    <key column="jokId" on-delete="noaction" />
    <one-to-many class="com.wy17113.model.entity.Comment" not-found="exception" embed-xml="true" />
  </set>
    </class></hibernate-mapping>

第三步:定义ehcache.xml文件

<ehcache>

    <diskStore path="c:\\ehcache\"/>
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        />

    <cache name="com.wy17113.model.entity.Jok"  缓存名字
        maxElementsInMemory="10000"   缓存里可存放的最大对象数.
        eternal="false"  缓存对象是否永久有效(true表示是).  
        timeToIdleSeconds="300"      对象在缓存中存活的空闲时间,即空闲多久它就失效,单位是秒.  
        timeToLiveSeconds="600"   对象在缓存中存活的时间,单位是秒.  
        overflowToDisk="true"      当缓存溢出时,对象是否保存到磁盘上.保存的磁盘路径由<diskStore>中的path指定.
        /> 
 
</ehcache>
dao里面查询代码:

// 查询所有
 @SuppressWarnings({ "deprecation", "unchecked" })
 public List<Jok> getAllJok(int type, String startTime, String endTime,
   int start, int count) throws SQLException {
  Session session = getSpringSession();
  Transaction ts = session.beginTransaction();
  List<Jok> jokList = null;
  try {
   Criteria criteria = session.createCriteria(Jok.class);
   
criteria.setCacheable(true);
   
   if (type != 0)
    criteria.add(Expression.eq("type", type));
   if (null != startTime && !startTime.equals(""))
    criteria.add(Expression.ge("subDate", java.sql.Date
      .valueOf(startTime)));
   if (null != endTime && !endTime.equals(""))
    criteria.add(Expression.le("subDate", java.sql.Date
      .valueOf(endTime)));
   criteria.addOrder(Property.forName("subDate").desc());

   criteria.setFirstResult(start);
   criteria.setMaxResults(count);
   
   jokList = criteria.list();
  } catch (HibernateException ex) {
   ts.rollback();
   ex.printStackTrace();
  }
  return jokList;
 }

到此就配置完成。

 

注意:

1,jar包版本问题、不能使用1.1版本、这样会报异常信息、本文用的版本是ehcache-1.2.3.jar、在tomcat中、如果存在

两个版本的jar包、他会默认加载低版本的、所以最好把相同的jar包低版本删掉、

2,测试的时候 可以吧hql语句打印出来、在hibernate配置文件里面吧 show_sql属性设置成true就行。你会发现进行第一次查询的时候会打印sql语句、第二次查询的时候就不会打印sql语句、因为走的是二级缓存。

原创粉丝点击