`
吕金含
  • 浏览: 87880 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

hibernate中使用ehcache缓存框架

 
阅读更多

1.在项目中配置ehcache文件


2.ehcache.xml的配置文件

<ehcache>
<!-- Sets the path to the directory where cache .data files are created.
If the path is a Java System Property it is replaced by
its value in the running VM.
The following properties are translated:
user.home - User's home directory
user.dir - User's current working directory
java.io.tmpdir - Default temp file path -->
<diskStore path="java.io.tmpdir"/>
<!--Default Cache configuration. These will applied to caches programmatically created through
the CacheManager.
The following attributes are required for defaultCache:
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<!--Predefined caches. Add your cache configuration settings here.
If you do not have a configuration for your cache a WARNING will be issued when the
CacheManager starts
The following attributes are required for defaultCache:
name - Sets the name of the cache. This is used to identify the cache. It must be unique.
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
-->
<!-- Sample cache named sampleCache1
This cache contains a maximum in memory of 10000 elements, and will expire
an element if it is idle for more than 5 minutes and lives for more than
10 minutes.
If there are more than 10000 elements it will overflow to the
disk cache, which in this configuration will go to wherever java.io.tmp is
defined on your system. On a standard Linux system this will be /tmp"
-->
<cache name="sampleCache1"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
<!-- Sample cache named sampleCache2
This cache contains 1000 elements. Elements will always be held in memory.
They are not expired. -->
<cache name="sampleCache2"
maxElementsInMemory="1000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/> -->
<!-- Place configuration for your caches following -->
</ehcache>

3.StudentTest.java

package com.ask.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Before;
import org.junit.Test;
import com.ask.pojo1.Classes;
import com.ask.pojo1.Student;
import com.ask.util.HibernateUtil;
public class StudentTest {
private SessionFactory sessionFactory;
@Before
public void setUp() throws Exception {
Configuration cfg=new Configuration().configure();
ServiceRegistry serviceRegistry=
new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
sessionFactory=cfg.buildSessionFactory(serviceRegistry);
}
@Test
public void getEHCacheStudent() {
Session session=sessionFactory.openSession();
session.beginTransaction();
Classes cls=(Classes) session.get(Classes.class,1);
System.out.println(cls);
System.out.println(session);
session.close();
System.out.println("--------------");
Session session1=sessionFactory.openSession();
session1.beginTransaction();
Classes cls1=(Classes) session1.get(Classes.class,1);
System.out.println(cls1);
System.out.println(session1);
session1.close();

}
//hibernate的hql的查询;
@Test
public void HQLGetStudent() {
Session session=sessionFactory.openSession();
// String hql="from Student s where s.id=?";
String hql="from Student s where s.id=:id";
Query query=session.createQuery(hql);
query.setInteger("id", 1);
Student s=(Student) query.uniqueResult();
System.out.println(s);

}
//Criteria的分页查询;
@Test
public void QBCGetStudent() {//query by Criteria
Session session=sessionFactory.openSession();
Criteria criteria=session.createCriteria(Student.class);
List<Student> stus=criteria.list();
for (Student student : stus) {
System.out.println(student);
}

}
//Query分页查询;
@Test
public void SQLGetStudent() throws NoSuchFieldException, SecurityException {
Session session=sessionFactory.openSession();
String sql="select * from student";
Query query=session.createSQLQuery(sql).addEntity(Student.class);

// query.setInteger(0, 7);
List<Student> stus=(List<Student>) query.list();
for (Student s : stus) {
System.out.println(s);
}

}

@Test
public void testSelect() {
Session session=sessionFactory.openSession();
Classes cls=(Classes) session.get(Classes.class, 1);
System.out.println(cls);
List<Student> stus=new ArrayList<Student>(cls.getStus());
for (Student student : stus) {
System.out.println(student);
}
}
}

分享到:
评论

相关推荐

    hibernate+ehcache

    4. **配置 Ehcache**:在 Hibernate 的 `hibernate.cfg.xml` 文件中,需要引入 Ehcache 的配置,包括指定缓存 provider、设置缓存区域、定义缓存策略(如时间过期、大小限制等)。 5. **缓存策略**:Ehcache 提供了...

    hibernate缓存ehcache用法

    这篇博客文章“hibernate缓存ehcache用法”可能详细介绍了如何在Hibernate中配置和使用Ehcache。 首先,我们需要理解什么是缓存。缓存是一种存储技术,用于临时保存经常访问的数据,以减少对主存储器(如数据库)的...

    Hibernate中二级缓存ehcache缓存案例

    ehcache是Hibernate官方推荐的二级缓存实现,它是一个高性能、轻量级的缓存框架。使用ehcache可以将频繁访问的数据存储在内存中,避免了频繁的数据库交互,从而提高了系统的响应速度。ehcache具有可扩展性,支持磁盘...

    Hibernate4 + Ehcache 例子

    标题 "Hibernate4 + Ehcache 例子" 暗示了我们将探讨如何在Java应用程序中集成Hibernate4 ORM框架和Ehcache缓存系统。这个例子可能是关于如何提高数据访问性能,通过缓存策略来减少数据库查询的次数。 描述中的链接...

    Hibernate + EhCache 实现数据缓存的处理

    本篇将详细探讨如何使用Hibernate ORM框架结合EhCache实现数据缓存的处理,从而提高系统的响应速度。 Hibernate是一个流行的Java持久化框架,它提供了一种便捷的方式来映射对象关系模型(ORM)到关系数据库。然而,...

    spring,spring mvc,hibernate,ehcache JavaWeb后台框架

    在Java Web开发领域,Spring、Spring MVC、Hibernate和Ehcache是四个非常关键的框架,它们共同构建了一个强大且高效的后台开发环境。下面将详细解释这些框架的核心功能和使用方式。 1. **Spring框架**:Spring是...

    Hibernate EhCache 二级缓存配置.docx

    Hibernate EhCache 二级缓存配置是 Hibernate 框架中的一种缓存机制,它可以提高应用程序的性能和效率。下面是关于 Hibernate EhCache 二级缓存配置的详细知识点: 一、简介 EhCache 是 Hibernate 的另一个项目,...

    缓存框架-Ehcache学习笔记

    Ehcache 是一个广泛使用的开源 Java 缓存框架,它在处理大量数据的高性能应用中扮演着重要角色。Ehcache 提供了本地内存缓存、磁盘存储以及分布式缓存的能力,使得应用程序能够快速访问频繁使用的数据,从而提高整体...

    cache/ehcache缓存使用

    本文将详细讲解"cache/ehcache缓存使用"的相关知识点,包括缓存的基本概念、Ehcache的介绍、以及如何在Java应用中使用Ehcache进行缓存操作。 首先,我们要理解什么是缓存。缓存是一种存储技术,它临时存储常用或...

    Struts2+Spring+Hibernate+Ehcache+AJAX+JQuery+Oracle 框架集成用户登录注册Demo工程

    6.Hibernate继承 HibernateDaoSupport。 7.Spring+Junit4单元测试,优点:不会破坏数据库现场,等等。 2)Demo 导入说明: 1.Eclipse Encoding:GBK 2.Eclipse 导入后可能需要在 Xml Catalog 手动添加:ehcache-...

    Java缓存框架 EhCache

    同时,EhCache与许多流行框架如Spring、Hibernate等有很好的集成,使得在这些框架中使用EhCache变得十分方便。在Hibernate中,EhCache作为默认的CacheProvider,可以为ORM操作提供高速缓存服务。 7. **扩展性**:...

    hibernate所需的ehcache的相关jar包,一共3个jar包

    在Java的持久化框架Hibernate中,缓存机制是提高数据访问效率的重要手段。Ehcache是一种广泛使用的开源Java缓存解决方案,它与Hibernate紧密结合,提供了第二级缓存的支持。本压缩包包含的是Hibernate使用Ehcache所...

    Ehcache缓存

    **Ehcache缓存** Ehcache是一种广泛使用的开源Java分布式缓存系统,它为高性能应用程序提供了内存存储和缓存解决方案。在Java世界中,尤其是在持久化框架如Hibernate的使用中,Ehcache扮演了至关重要的角色。由于...

    Hibernate4二级缓存Ehcache案例

    Ehcache是Java中广泛使用的内存缓存库,它支持内存和磁盘缓存,具有高效的缓存管理和数据持久化能力,因此被选为Hibernate的二级缓存提供商。要使用Ehcache,我们首先需要在项目中引入Ehcache的依赖。在Maven项目中...

    spring+springmvc+hibernate+ehcache JavaWeb后台框架

    spring+springmvc+hibernate+ehcache JavaWeb后台框架,不仅提高了开发程序的速度,且其中还是用到hibernate和ehcache缓存的使用,加快了程序运行的数据,该框架亲测好用。值得注意的是该种框架现在还算是用的比较多...

    基于org.hibernate-ehcache的Ehcache缓存查询工具设计源码

    本项目为基于org.hibernate-ehcache框架设计的Ehcache缓存查询工具源码,总计包含35个文件,涵盖6个XML配置文件、6个Java源文件、6个类文件、4个IDE偏好设置文件、3个Markdown文档、2个Excel表格、2个Manifest文件...

    Spring+Hibernate+ehcache整合

    Spring、Hibernate和Ehcache是Java开发中常用的三个框架,它们在企业级应用开发中扮演着重要的角色。Spring是一个全面的后端应用框架,提供依赖注入、AOP(面向切面编程)、MVC(模型-视图-控制器)等特性;...

    spring3 hibernate4 ehcache实例

    在Java企业级应用开发中,Spring、Hibernate和Ehcache是三个非常重要的框架和技术。Spring作为轻量级的IoC(Inversion of Control)和AOP(Aspect Oriented Programming)容器,提供了一个统一的管理组件的方式,...

    spring,spring mvc,hibernate,ehcache Java后台框架

    Spring,Spring MVC,Hibernate,以及Ehcache是Java开发中常用的四大框架,它们共同构建了高效、稳定的后台应用体系。 Spring框架是Java企业级应用的事实标准,它提供了一个全面的编程和配置模型,用于简化企业级...

    spring、 spring mvc、 hibernate、 ehcache Java后台框架

    Ehcache还可以与Spring集成,方便地在Spring应用中使用缓存功能。 在实际项目中,这四个框架的整合使用能够构建出高性能的Java后台系统。Spring作为基础架构,负责管理和协调各个组件;Spring MVC处理HTTP请求,...

Global site tag (gtag.js) - Google Analytics