hibernate二级缓存

二级缓存:存放公有数据
   1、适用场合:
        1、数据不能频繁更新
        2、数据能公开,私密性不是很强
   2、hibernate本身并没有提供二级缓存的解决方案
   3、二级缓存的实现是依赖于第三方供应商完成的
         ehcache
         oscache
         jbosscache
         swamchache
   4、二级缓存的操作
         1、二级缓存存在sessionFactory中
         2、生命周期:与sessionFactory保持一致
         3、使用二级缓存的步骤
             1、在hibernate.cfg.xml
                  <property name="cache.use_second_level_cache">true</property>
                  <property name="cache.provider_class">
                        org.hibernate.cache.EhCacheProvider
                  </property>
             2、让某一个对象进入到二级缓存中
                 1. 为指定类配置二级缓存   (在配置文件中)
                   <class-cache usage="read-only" class="cn.itcast.hiberate.sh.domain.Classes"/>
                 2. 在指定的映射类中配置二级缓存  (在映射文件中)
                    <cache usage="read-only"/>
             3、使用
                  session.get/session.load

   5、查询缓存,在配置文件中配置,查询缓存是不用配置针对哪个类来进行指定的二级缓存配置(也就是上面的第二步不需要),查询单个对象的话,查询缓存无效,配置以下三条即可(都在配置文件中)。

                  <property name="cache.use_second_level_cache">true</property>
                  <property name="cache.provider_class">
                        org.hibernate.cache.EhCacheProvider
                  </property>

                 <property name="cache.use_query_cache">true</property>


测试类

public class QueryCacheTest {
	private static SessionFactory sessionFactory;

	static{
		Configuration con = new Configuration().configure();
		sessionFactory = con.buildSessionFactory();
	}
	
	@Test
	public void test(){
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		for(int i=0;i<20;i++){
			Class clzz = new Class();
			clzz.setCname("第一个班级,二级缓存");
			clzz.setDescription("描述1");
			session.save(clzz);
		}
		
		transaction.commit();
		session.close();
	}
	
	/**
	 * 因为session管理了,再次获取这个对象的信息也不会发出sql语句,所以这条数据是被存在了二级缓存里
	 * Hibernate: select class0_.cid as cid0_0_, class0_.cname as cname0_0_, class0_.description as descript3_0_0_ from Class class0_ where class0_.cid=?
	 */
	@Test
	public void testCache(){
		Session session = sessionFactory.openSession();
		
		Class clzz = (Class) session.get(Class.class, 1L);
		session.close();
		session = sessionFactory.openSession();
		clzz = (Class) session.get(Class.class, 1L);
		
		session.close();
	}
	
	/**
	 * Hibernate: select class0_.cid as cid0_, class0_.cname as cname0_, class0_.description as descript3_0_ from Class class0_
	 * 查询缓存
	 * 	需要在hibernate.cfg.xml中配置<property name="cache.use_query_cache">true</property>
	 */
	@Test
	public void testQueryCacheList(){
		Session session = sessionFactory.openSession();
		
		Query query = session.createQuery("from Class");
		//将Classes的数据往查询缓存中存放
		query.setCacheable(true);
		List<Class> list_clazz = query.list();
		
		query = session.createQuery("from Class ");
		query.setCacheable(true);
		list_clazz = query.list();
		
		
		session.close();
	}
}


class.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.xxc.one2many_bothway.Class">
		<!-- 配置此映射类的数据是否存放在二级缓存里 -->
		<cache usage="read-only"/>
		<id name="cid" type="long">
			<column name="cid" length="5"/>
			<generator class="identity"/>
		</id>
		<property name="cname" type="string" length="20"/>
		<property name="description" type="string" length="100"/>
		
		<set name="students"  inverse="false" cascade="all">
			<!-- key用于描述外键 -->
			<key column="cid"></key><!-- cid表示student表中的外键 -->
			<one-to-many class="com.xxc.one2many_bothway.Student"/><!-- 跟哪个类建立关系 -->
		</set>
	</class>
</hibernate-mapping>

hibernate.cfg.xml

<!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://localhost:3306/hibernate_zd</property>
	<property name="connection.username">root</property>
	<property name="connection.password">xxc</property>
	<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

	<!-- 
		开启二级缓存
	-->
	<property name="cache.use_second_level_cache">true</property>

	<!-- 
		二级缓存的提供商
	-->
	<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
	<!-- 开启查询缓存 -->
	<property name="cache.use_query_cache">true</property>



	<!-- 自动建表 -->
	<property name="hibernate.hbm2ddl.auto">update</property>
	<property name="show_sql">true</property>
	
	<mapping resource="com/xxc/one2many_bothway/Class.hbm.xml" />
	<mapping resource="com/xxc/one2many_bothway/Student.hbm.xml" />
	
	<!-- <class-cache usage="read-write" class="com.xxc.one2many_bothway.Class" /> -->
</session-factory>
</hibernate-configuration>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值