Hibernate与ehcache二级缓存技术

------------------------------------------------------
Hibernate与ehcache二级缓存技术
------------------------------------------------------
package com;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;

import db.HibernateSessionFactory;
import db.Person;

public class Common {

/**
* @param args
*/
public static void main(String[] args) {

   Session s = HibernateSessionFactory.getSession();
   Criteria c = s.createCriteria(Person.class);
   c.setCacheable(true);// 这句必须要有
   System.out.println("第一次读取");
   List l = c.list();
   System.out.println(l.size());
   HibernateSessionFactory.closeSession();

   s = HibernateSessionFactory.getSession();
   c = s.createCriteria(Person.class);
   c.setCacheable(true);// 这句必须要有
   System.out.println("第二次读取");
   l = c.list();
   System.out.println(l.size());
   HibernateSessionFactory.closeSession();

   System.out.println("END");

}

}

------------------------------------------------------
Person.hbm.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="db.Person" table="person" catalog="e">
    <cache usage="read-only"/>
<id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="45" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

------------------------------------------------------
ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="10000" eternal="false"
   overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="180"
   diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />
</ehcache>
------------------------------------------------------
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>
<property name="connection.username">root</property>
<property name="connection.url">
   jdbc:mysql://localhost:3306/jbpmdb
</property>
<property name="dialect">
   org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">local_mysql</property>
<property name="connection.driver_class">
   com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<property name="cache.provider_class">
   org.hibernate.cache.EhCacheProvider
</property>
<property name="cache.use_query_cache">true</property>

<mapping resource="db/Person.hbm.xml" />

</session-factory>

</hibernate-configuration>
------------------------------------------------------
打印信息:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
第一次读取
Hibernate: select this_.id as id0_0_, this_.name as name0_0_ from e.person this_
1
第二次读取
1
END

------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
 <diskStore path="java.io.tmpdir"/> 
  <defaultCache
   maxElementsInMemory="10000" <!-- 缓存最大数目 -->
   eternal="false" <!-- 缓存是否持久 -->
   overflowToDisk="true" <!-- 是否保存到磁盘,当系统当机时-->
   timeToIdleSeconds="300" <!-- 当缓存闲置n秒后销毁 -->
   timeToLiveSeconds="180" <!-- 当缓存存活n秒后销毁-->
   diskPersistent="false"
   diskExpiryThreadIntervalSeconds= "120"/> 
</ehcache>
------------------------------------------------------

你可能感兴趣的:(log4j,Hibernate,mysql,xml,MyEclipse)