1.要实现二级缓存,需另加以下jar包:
ehcache-core-2.4.3.jar、hibernate-core-4.2.3.Final.jar、slf4j-api-1.6.6.jar
2.需要更改配置文件
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">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate_db</property>
<property name="connection.username">root</property>
<property name="connection.password">beanGou</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="hibernate.cache.provider_configuration_file_resource_path">ehcache2.xml</property>
<!-- 产生统计信息 -->
<property name="generate_statistics">true</property>
<!-- Disable the second-level cache -->
<!--<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
--><!--<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
--><!--<class-cache class="com.sinoi.domain.Employee" usage="read-only"/>
--><property name="format_sql">true</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/sinoi/domain/Department.hbm.xml"/>
<mapping resource="com/sinoi/domain/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
如果要给某个类设置二级缓存,则相应的改其配置文件,如
Employee.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">
<hibernate-mapping package="com.sinoi.domain">
<class name="Employee" table="hib_employee">
<cache usage="read-only"/>
<id name="id" column="EMPLOYEE_ID">
<generator class="native"/>
</id>
<property name="name"/>
<!--<many-to-one name="department" column="department_id" not-null="false"></many-to-one>
--></class>
</hibernate-mapping>
3.测试代码
@Test public void get() { Session session = HibernateUtil.getSession(); Employee employee = (Employee) session.get(Employee.class, 1); employee = (Employee) session.get(Employee.class, 1); System.out.println(employee.getName()); session.evict(employee); employee = (Employee) session.get(Employee.class, 1); session.close(); Statistics statistics = HibernateUtil.getSessionFactory().getStatistics(); System.out.println(statistics); System.out.println("命中数:" + statistics.getSecondLevelCacheHitCount()); System.out.println("错过数:" + statistics.getSecondLevelCacheMissCount()); System.out.println("放入数:" + statistics.getSecondLevelCachePutCount()); }
注:增加了HibernateUtil类以及getSessionFactory方法
package com.sinoi.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory factory; private static Session session; private HibernateUtil() { } public static SessionFactory getSessionFactory() { if (factory == null) { factory = new Configuration().configure().buildSessionFactory(); } return factory; } public static Session getSession() { if (session == null) { return getSessionFactory().openSession(); } else { return session; } } }