SSH中Hibernate配置二级缓存

为Hibernate配置二级缓存,当第一次从数据库读取文件后其会在指定的文件夹位置进行缓存。这样利于读取数据。

下面开始配置二级缓存:

1.在Spring的beans.xml加入以下几句代码:

<!-- 解决问题是加入了二级缓存的配置 -->
hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=false
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider


2.在bean的映射文件中加入配置缓存文件代码:

<cache usage="read-write" region="cn.itcast.bean.Person"/> 

3.在类路径下配置ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
  <diskStore path="D:\cache"/>
  <defaultCache maxElementsInMemory="1000" eternal="false" overflowToDisk="true"
      timeToIdleSeconds="120"
      timeToLiveSeconds="180"
      diskPersistent="false"
      diskExpiryThreadIntervalSeconds="60"/>
      <cache name="cn.itcast.bean.Person" maxElementsInMemory="100" eternal="false"
      overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" diskPersistent="false"></cache>
</ehcache>


4.编写测试类:testGetPerson:

@Test
	public void testGetPerson() {
		System.out.println(personService.getPerson(1).getName());
		try {
			System.out.println("请关闭数据库");
			Thread.sleep(1000*15);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("第二次获取");
		System.out.println(personService.getPerson(1).getName());
	}

运行,看在关闭数据库后,第二次获取数据能否得到。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值