- 浏览: 204108 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (397)
- j2se (28)
- nio (3)
- 易错点 (3)
- 面试ssh (9)
- ssh整合 (11)
- jbpm+spring (2)
- js (15)
- 高级技术 (59)
- swing (3)
- 数据库 (16)
- hibernate (18)
- spring (19)
- 开发网站知识点 (9)
- jbpm (4)
- json (5)
- 设计模式 (22)
- 自定义标签 (1)
- j2ee (9)
- lucene (3)
- cahce (11)
- maven (5)
- html5 (1)
- 多数据源 (10)
- 页面聊天 (9)
- 富客户端 (1)
- android (13)
- aop+拦截器+jms (13)
- 框架整合 (1)
- 非阻塞io (24)
- 暂时不看 (13)
- webservice (3)
- oracle (3)
- 算法 (4)
- 协程 (2)
- netty (1)
- 爬虫 (0)
- 高级基础 (1)
- JVM调优总结 (12)
- 知识点技巧 (1)
- REST (0)
- 基础 io (2)
- dubbo (8)
- 线程 (1)
- spring源码 (2)
- git (1)
- office (2)
最新评论
-
sjzcmlt:
,写的挺好的啊
一个完整的负载均衡的例子 . -
他大姨妈:
网上大部分例子都是直接通过IdleStateHandler来实 ...
Netty的超时机制 心跳机制
数据库结构:
create table teamEH (id varchar(32),teamname varchar(32));
create table studentEH (id varchar(32),name varchar(32),team_id varchar(32));POJO:
package EHCache;
public class Student ...{
private String id; //标识id
private String name; //学生姓名
private Team team;//班级
public String getName() ...{
return name;
}
public void setId(String id) ...{
this.id = id;
}
public void setName(String stuName) ...{
this.name = stuName;
}
public String getId() ...{
return id;
}
public Student() ...{ //无参的构造函数
}
public Team getTeam() ...{
return team;
}
public void setTeam(Team team) ...{
this.team = team;
}
}
package EHCache;
import java.util.HashSet;
import java.util.Set;
public class Team ...{
private String id;
private Set students;
private String teamName;
public String getId() ...{
return id;
}
public void setId(String id) ...{
this.id = id;
}
public String getTeamName() ...{
return teamName;
}
public void setTeamName(String name) ...{
this.teamName = name;
}
public Set getStudents() ...{
return students;
}
public void setStudents(Set students) ...{
this.students = students;
}
}
Team.hbm.xml
其中<cache>标签表示对student集合缓存,但只缓存id,如果需要缓存student实例,则需要在student.hbm.xml中的
class标签中配置<cache>
<?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 - Hibernate Tools
-->
<hibernate-mapping package="EHCache" >
<class name="EHCache.Team" table="teamEH" lazy="false">
<id name="id" column="id">
<generator class="uuid.hex"></generator>
</id>
<property name="teamName" column="teamName"></property>
<set name="students"
lazy="true"
inverse="true"
outer-join="false"
batch-size="2"
cascade="save-update"
>
<!-- 对students集合缓存,但只是缓存student-id如果要对整个对象缓存,
还需要在Student.hbm.xml的class标签中加入<cache>标签 -->
<cache usage="read-write"/>
<key column="team_id"></key>
<one-to-many class="EHCache.Student"/>
</set>
</class>
</hibernate-mapping>
Student.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 - Hibernate Tools
-->
<hibernate-mapping package="EHCache" >
<class name="EHCache.Student" table="studentEH" lazy="false">
<cache usage="read-write"/>
<id name="id" column="id" unsaved-value="null">
<generator class="uuid.hex"></generator>
</id>
<property name="name" column="name"></property>
<many-to-one name="team"
column="team_id"
outer-join="true"
cascade="save-update"
class="EHCache.Team"></many-to-one>
</class>
</hibernate-mapping>
Hibernate.cfg.xml
配置hibernate.cache.provider_class以启用EHCache
<?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/schoolproject?characterEncoding=gb2312&useUnicode=true
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</property>
<mapping resource="EHCache/Student.hbm.xml" />
<mapping resource="EHCache/Team.hbm.xml" />
</session-factory>
</hibernate-configuration>EHCache.xml(放在classpath下)
<ehcache>
<diskStore path="c:/cache"/> <!--缓存文件存放位置-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<cache name="EHCache.Student"
maxElementsInMemory="500" <!---超过500实例,就将多出的部分放置缓存文件中->
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/> -->
<!-- Place configuration for your caches following -->
</ehcache>
测试代码(插入准备数据部分)
package EHCache;
import java.io.File;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test ...{
public static void main(String[] args) ...{
String filePath=System.getProperty("user.dir")+File.separator+"src/EHCache"+File.separator+"hibernate.cfg.xml";
File file=new File(filePath);
SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
// Team team=new Team();
// team.setTeamName("team1");
//
//
// for(int i=0;i<1000;i++){
// Student stu=new Student();
// stu.setName("tom"+i);
// stu.setTeam(team);
// session.save(stu);
// }
// tx.commit();
//
}
}
测试成功后,运行以下代码
package EHCache;
import java.io.File;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test ...{
public static void main(String[] args) ...{
String filePath=System.getProperty("user.dir")+File.separator+"src/EHCache"+File.separator+"hibernate.cfg.xml";
File file=new File(filePath);
SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
//模拟多用户访问数据
Session session1=sessionFactory.openSession();
Transaction tx1=session1.beginTransaction();
List list=session1.createQuery("from Student").list();
for(int i=0;i<list.size();i++)...{
Student stu=(Student)list.get(i);
System.out.println(stu.getName());
}
tx1.commit();
session1.close();
Session session2=sessionFactory.openSession();
Transaction tx2=session2.beginTransaction();
//这个uuid从刚才插入的数据中复制一个student的id
Student stu=(Student)session2.get(Student.class, "4028818316d184820116d184900e0001");
System.out.println(stu.getName());
tx2.commit();
session2.close();
}
}
结果如下:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select student0_.id as id0_, student0_.name as name0_, student0_.team_id as team3_0_ from studentEH student0_
Hibernate: select team0_.id as id1_0_, team0_.teamName as teamName1_0_ from teamEH team0_ where team0_.id=?
tom0
tom1
tom2
tom3
tom4
tom5
tom6
tom7
tom8
tom9
tom10
........................................
tom974
tom975
tom976
tom977
tom978
tom998
tom999
Hibernate: select team0_.id as id1_0_, team0_.teamName as teamName1_0_ from teamEH team0_ where team0_.id=?
tom0
可以看到,第二次查询,已经不再访问数据库了,而且,查看c:/cache文件夹,也可以看到,数据已经缓存成功了
create table teamEH (id varchar(32),teamname varchar(32));
create table studentEH (id varchar(32),name varchar(32),team_id varchar(32));POJO:
package EHCache;
public class Student ...{
private String id; //标识id
private String name; //学生姓名
private Team team;//班级
public String getName() ...{
return name;
}
public void setId(String id) ...{
this.id = id;
}
public void setName(String stuName) ...{
this.name = stuName;
}
public String getId() ...{
return id;
}
public Student() ...{ //无参的构造函数
}
public Team getTeam() ...{
return team;
}
public void setTeam(Team team) ...{
this.team = team;
}
}
package EHCache;
import java.util.HashSet;
import java.util.Set;
public class Team ...{
private String id;
private Set students;
private String teamName;
public String getId() ...{
return id;
}
public void setId(String id) ...{
this.id = id;
}
public String getTeamName() ...{
return teamName;
}
public void setTeamName(String name) ...{
this.teamName = name;
}
public Set getStudents() ...{
return students;
}
public void setStudents(Set students) ...{
this.students = students;
}
}
Team.hbm.xml
其中<cache>标签表示对student集合缓存,但只缓存id,如果需要缓存student实例,则需要在student.hbm.xml中的
class标签中配置<cache>
<?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 - Hibernate Tools
-->
<hibernate-mapping package="EHCache" >
<class name="EHCache.Team" table="teamEH" lazy="false">
<id name="id" column="id">
<generator class="uuid.hex"></generator>
</id>
<property name="teamName" column="teamName"></property>
<set name="students"
lazy="true"
inverse="true"
outer-join="false"
batch-size="2"
cascade="save-update"
>
<!-- 对students集合缓存,但只是缓存student-id如果要对整个对象缓存,
还需要在Student.hbm.xml的class标签中加入<cache>标签 -->
<cache usage="read-write"/>
<key column="team_id"></key>
<one-to-many class="EHCache.Student"/>
</set>
</class>
</hibernate-mapping>
Student.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 - Hibernate Tools
-->
<hibernate-mapping package="EHCache" >
<class name="EHCache.Student" table="studentEH" lazy="false">
<cache usage="read-write"/>
<id name="id" column="id" unsaved-value="null">
<generator class="uuid.hex"></generator>
</id>
<property name="name" column="name"></property>
<many-to-one name="team"
column="team_id"
outer-join="true"
cascade="save-update"
class="EHCache.Team"></many-to-one>
</class>
</hibernate-mapping>
Hibernate.cfg.xml
配置hibernate.cache.provider_class以启用EHCache
<?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/schoolproject?characterEncoding=gb2312&useUnicode=true
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</property>
<mapping resource="EHCache/Student.hbm.xml" />
<mapping resource="EHCache/Team.hbm.xml" />
</session-factory>
</hibernate-configuration>EHCache.xml(放在classpath下)
<ehcache>
<diskStore path="c:/cache"/> <!--缓存文件存放位置-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<cache name="EHCache.Student"
maxElementsInMemory="500" <!---超过500实例,就将多出的部分放置缓存文件中->
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/> -->
<!-- Place configuration for your caches following -->
</ehcache>
测试代码(插入准备数据部分)
package EHCache;
import java.io.File;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test ...{
public static void main(String[] args) ...{
String filePath=System.getProperty("user.dir")+File.separator+"src/EHCache"+File.separator+"hibernate.cfg.xml";
File file=new File(filePath);
SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
// Team team=new Team();
// team.setTeamName("team1");
//
//
// for(int i=0;i<1000;i++){
// Student stu=new Student();
// stu.setName("tom"+i);
// stu.setTeam(team);
// session.save(stu);
// }
// tx.commit();
//
}
}
测试成功后,运行以下代码
package EHCache;
import java.io.File;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test ...{
public static void main(String[] args) ...{
String filePath=System.getProperty("user.dir")+File.separator+"src/EHCache"+File.separator+"hibernate.cfg.xml";
File file=new File(filePath);
SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
//模拟多用户访问数据
Session session1=sessionFactory.openSession();
Transaction tx1=session1.beginTransaction();
List list=session1.createQuery("from Student").list();
for(int i=0;i<list.size();i++)...{
Student stu=(Student)list.get(i);
System.out.println(stu.getName());
}
tx1.commit();
session1.close();
Session session2=sessionFactory.openSession();
Transaction tx2=session2.beginTransaction();
//这个uuid从刚才插入的数据中复制一个student的id
Student stu=(Student)session2.get(Student.class, "4028818316d184820116d184900e0001");
System.out.println(stu.getName());
tx2.commit();
session2.close();
}
}
结果如下:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select student0_.id as id0_, student0_.name as name0_, student0_.team_id as team3_0_ from studentEH student0_
Hibernate: select team0_.id as id1_0_, team0_.teamName as teamName1_0_ from teamEH team0_ where team0_.id=?
tom0
tom1
tom2
tom3
tom4
tom5
tom6
tom7
tom8
tom9
tom10
........................................
tom974
tom975
tom976
tom977
tom978
tom998
tom999
Hibernate: select team0_.id as id1_0_, team0_.teamName as teamName1_0_ from teamEH team0_ where team0_.id=?
tom0
可以看到,第二次查询,已经不再访问数据库了,而且,查看c:/cache文件夹,也可以看到,数据已经缓存成功了
发表评论
-
hibernate抓取策略fetch=select /join/subselect
2016-04-10 11:24 546出处:http://blog.csdn.net/ychato ... -
hibernate缓存机制详细分析(一级、二级、查询缓存,非常清晰明白)
2016-04-09 22:23 460收藏自:http://www.360doc.com/cont ... -
hibernate实现JTA事物--代码
2014-11-08 16:15 446package com.ajita.jta; impo ... -
Hibernate的三种连接池设置C3P0、Proxool和DBCP
2014-11-08 16:16 415Xml代码 <!-- JDBC驱动程序 --& ... -
EHCache的使用
2014-05-22 11:39 463在开发高并发量,高性 ... -
缓存 hibernate
2014-06-17 09:40 4241. Session---单数据加载---load/ ge ... -
spring+ehcache实现的缓存查询
2014-06-17 09:40 527最近项目有一个需求,就是用户在查询界面,输入很多查询条件之后, ... -
hibernate + ehcache的例子
2014-05-12 11:23 474这是个hibernate + ehcache的例子,目前使用最 ... -
hibernate ehcache
2014-05-12 10:01 4011.EhCache是什么 EhCac ... -
Hibernate中cascade和inverse的作用
2014-05-07 10:38 502Inverse和cascade是Hibernate映射中最难掌 ... -
Hibernate中inverse的用法 .
2014-05-07 10:40 409一、Inverse是hibernate双向关系中的基本概念。i ... -
关联关系
2014-04-22 21:52 293一对一单向外键关联 (学生卡表里有“studentId”字段) ... -
Hibernate一对多,多对一,多对多,一对一关系汇总
2014-05-07 10:42 557一对多 ◆name:集合属性的名称(也可以理解为一对多中那个 ... -
HibernateTemplate+HibernateDaoSupport+SessionFactory
2014-04-19 20:13 469HibernateTemplate @Component ... -
数据库事物
2014-06-17 09:40 3301. 脏读 :脏读就是指当 ... -
Open Session In View
2014-06-17 09:40 430从昨天下午一直纠结到现在,原来是项目启用了Open Sessi ... -
Hibernate主键生成策略
2014-06-20 09:33 2581、自动增长identity 适用于MySQL、DB2、MS ...
相关推荐
Hibernate EhCache 二级缓存配置
Hibernate+ehcache二级缓存技术 Hibernate+ehcache二级缓存技术
struts2+hibernate+ehcache二级缓存的 步骤和需要的代码及jar包
ehcache 二级缓存 配置使用的jar包 配置如下: <!-- 启用二级缓存 --> <property name="hibernate.cache.use_second_level_cache">true <!-- 查询的二级缓存配置 --> <property name="hibernate....
此配置和包,是springMVC4.3.3 +spring4+hibernate5.1.3+二级缓存ehcache(不用可以关闭)+fastjson。 是正常运行的项目里拷出来的,方便大家使用。
配置 EhCache 的实例 1、 简介: EhCache缓存插件是Hibernate的另一个项目,Hibernate框架内置了对它 的支持,对于单机应用推荐使用它做为Hibernate的二级缓存。
SSH三层架构MVC(struts1.3+spring2.x+hibernate3.2),Hibernate(ehcache)二级缓存技术,Spring 注解形式依赖注入,ehcache缓存 源代码,内有MySql anbyke.sql文件,方便创建数据库演示效果!
hibernate4配置ehcache二级缓存,以及相关的jar都在源码中。
struts2+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,mysql数据,需要自己创建
第一步:修改Hibernate配置文件,启用Hibernate的第二级缓存.在配置文件中加入EhCache缓存插件的实现类. 第二步:拷贝ehcache.xml文件到类路径下(位于Hibernate3的etc文件夹下) 第三步:对需要进行缓存的...
Hibernate4二级缓存Ehcache案例,可参考博客:http://blog.csdn.net/coco2d_x2014/article/details/52927638
Hibernate中默认的二级缓存机制ehcache案例,属性ehcache环境搭建,配置问题。
struts1.x+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,数据库mysql,需要自己创建
hibernate二级缓存 ehcache
使用Java工程实现了Hibernate二级缓存(Ehcache),其中还有自己封装的一个jar包实现了简单的分页功能
2.4. 使用EhCache配置二级缓存: 2.4.1. 配置准备: 1) 把ehcache-1.2.3.jar加入到当前应用的classpath中。 2) 在hibernate.cfg.xml文件中加入EhCache缓存插件的提供类。 <!--配置缓存插件 --> ...
本人在做项目时用到了Hibernate的二级缓存,使用的是EhCache,结合本人自己的理解总结了如何在java web项目中配合Hibernate使用二级缓存,以提高程序的性能,附带需要的文件,参考的文件,和测试类以及说明。
Spring 整合 Hibernate 时启用二级缓存实例详解 写在前面: 1. 本例使用 Hibernate3 + Spring3; 2. 本例的查询使用了 HibernateTemplate; 1. 导入 ehcache-x.x.x.jar 包; 2. 在 applicationContext.xml ...
它可以使用不同的缓存实现,如 EhCache 、 JBossCache 、 OsCache 等 (二级缓存是缓存实体对象的) 还有一个类型的 CACHE 就是 QueryCache . 它的作用就是缓存一个 Query 以及 Query 返回对象的 Identifier 以及...
二级缓存 :是SessionFactory对象缓存,可以被创建出的多个 Session 对象共享,二级缓存默认是关闭的,如果要使用需要手动开启,并且依赖EhCache组件。 三级缓存 :查询缓存,配置开启该缓存的情况下,重复使用一...