- 浏览: 118068 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (116)
- Jetspeed (7)
- maven (1)
- JAAS (2)
- SVN (1)
- Spring (10)
- Hibernate (4)
- 问题记录 (1)
- Javascript (2)
- java (6)
- 技术书签 (1)
- temp (5)
- 设计模式 (5)
- struts2 (1)
- 笔试面试 (8)
- oracle (19)
- 并发 (6)
- cache (6)
- 排序算法 (1)
- 感悟心得 (5)
- 内存泄露 (1)
- 产品设计 (1)
- Exception (2)
- 工作小结 (1)
- 上传资料 (2)
- ldap (1)
- SQL (1)
- 分布式 (2)
- jvm (2)
- SSO (2)
- SqlServer (1)
- mysql (4)
- osgi (1)
- UML (1)
- 项目管理 (3)
- IO (1)
- BPM (1)
最新评论
-
LD_21:
两道题会让你知道谁爱你 -
yuantong:
你的人生就是精彩的,一段十二年过去了,后面更多的十二年一样会精 ...
金蝶妈妈 -
xingqinstar:
努力,加油哦!
jetspeed2.2技术升级预研 -
xingqinstar:
要继续研究哦,亲
jetspeed2.2技术升级预研
本文主要讲一讲Hibernate+EhCache配置二级缓存的基本使用方法,主要分以下两个方面介绍:
(有关EhCache的基础介绍可参见:http://sjsky.iteye.com/blog/1288257 )
* Cache的多种配置方法
* Hibernate+EhCache集成demo
[一]、Cache的多种配置方法
Javabean cache的配置有三种,下面将一一介绍,具体如下::
(1).bean中注解配置的方式: @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
(2).hibernate.cfg.xml中标签配置方式: <class-cache class="" usage="" />
(3).映射文件*.hb.xml中标签配置方式: <cache usage=" />
1. classpath:ehcahce.xml配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true" monitoring="autodetect"
dynamicConfig="true">
<diskStore path="java.io.tmpdir"/>
<transactionManagerLookup class="net.sf.ehcache.transaction.manager.DefaultTransactionManagerLookup"
properties="jndiName=java:/TransactionManager" propertySeparator=";"/>
<cacheManagerEventListenerFactory class="" properties=""/>
<defaultCache
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="100"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
statistics="false"
/>
<cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="5"
eternal="false"
timeToLiveSeconds="120"
overflowToDisk="true" />
<cache name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000"
eternal="true"
overflowToDisk="true" />
<cache name="sampleCache1"
maxElementsInMemory="10000"
maxElementsOnDisk="1000"
eternal="false"
overflowToDisk="true"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off"
/>
<cache name="sampleCache2"
maxElementsInMemory="1000"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="FIFO"
/>
</ehcache>
2.hibernate配置文件:hibernate.cfg.xml
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:ora11g
</property>
<property name="connection.username">mytest</property>
<property name="connection.password">111111</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<property name="connection.SetBigStringTryClob">true</property>
<property name="connection.pool_size">10</property>
<property name="hibernate.jdbc.batch_size">10</property>
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<property name="current_session_context_class">thread</property>
<property name="hbm2ddl.auto">update</property>
<!-- Hibernate 3.3 and higher -->
<!--
<property name="hibernate.cache.region.factory_class">
net.sf.ehcache.hibernate.EhCacheRegionFactory
</property>
<property name="hibernate.cache.region.factory_class">
net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
</property>
-->
<!-- hibernate3.0-3.2 cache config-->
<!--
<property name="hibernate.cache.region.factory_class">
net.sf.ehcache.hibernate.EhCacheProvider
</property>
-->
<property name="hibernate.cache.provider_class">
net.sf.ehcache.hibernate.SingletonEhCacheProvider
</property>
<!-- Enable Second-Level Cache and Query Cache Settings -->
<property name="hibernate.cache.use_second_level_cache">
true
</property>
<property name="hibernate.cache.use_query_cache">
true
</property>
<!-- 注解配置 -->
<mapping class="michael.cache.ehcache.hibernate.EhUserInfo" />
<mapping class="michael.cache.ehcache.hibernate.EhBlogTopic" />
<mapping class="michael.cache.ehcache.hibernate.EhBlogTopic2" />
<!-- 映射文件 -->
<mapping
resource="michael/cache/ehcache/hibernate/tb_EhBlogTopic3.hb.xml" />
<!-- class-cache config -->
<class-cache class="michael.cache.ehcache.hibernate.EhBlogTopic"
usage="read-write" />
</session-factory>
</hibernate-configuration>
3.相关javabean代码片段如下:
(1).hibernate.cfg.xml中<calss-cache>标签配置的:EhBlogTopic.java:
/**
* @blog http://sjsky.iteye.com
* @author Michael
*/
@Entity
@Table(name = "MY_TB_EH_BLOG_TOPIC")
public class EhBlogTopic implements Serializable {
/**
* serialVersionUID
*/
private static final long serialVersionUID = -570936907944909799L;
private Integer id;
private String userId;
private String topic;
private String site;
//其他省略
}
(2). bean中注解的方式配置cache的:EhBlogTopic2.java
/**
* @blog http://sjsky.iteye.com
* @author Michael
*/
@Entity
@Table(name = "MY_TB_EH_BLOG_TOPIC2")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class EhBlogTopic2 implements Serializable {
//属性和EhBlogTopic一样
//其他省略
}
(3). 映射文件*.hb.xml中添加cache标签的: EhBlogTopic3.java
/**
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class EhBlogTopic3 implements Serializable {
//属性和EhBlogTopic一样
//其他省略
}
tb_EhBlogTopic3.hb.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="michael.cache.ehcache.hibernate">
<class name="EhBlogTopic3" table="MY_TB_EH_BLOG_TOPIC3">
<cache usage="read-write" />
<id name="id" type="int" unsaved-value="null">
<generator class="increment" />
</id>
<property name="userId" column="USER_ID" type="string"
not-null="false" length="20" />
<property name="topic" column="TOPIC" type="string"
not-null="false" length="100" />
<property name="site" column="SITE" type="string"
not-null="false" length="100" />
</class>
</hibernate-mapping>
(4). 没有配置cache的bean:EhUserInfo.java
/**
* @blog http://sjsky.iteye.com
* @author Michael
*/
@Entity
@Table(name = "MY_TB_EH_USER_INFO")
public class EhUserInfo implements Serializable {
/**
* serialVersionUID
*/
private static final long serialVersionUID = 930384253681679239L;
private Integer id;
private String userId;
private String userName;
private String otherInfo;
/**
* @return the id
*/
@Id
@GeneratedValue
@Column(name = "ID")
public Integer getId() {
return id;
}
//其他省略。。。
}
4.测试运行代码如下:
package michael.cache.ehcache.hibernate;
import java.util.List;
import michael.hibernate.bigstring.oracle.BigStrBlob;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
*
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class TestEhcacheHibernate {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
testMulitConfigMethod();
}
/**
* 测试多种配置缓存的方法
*/
public static void testMulitConfigMethod() {
SessionFactory sessionFactory = null;
try {
System.out.println("ehcache - hibernate Test ...");
Configuration config = new AnnotationConfiguration()
.configure("michael/cache/ehcache/hibernate/hibernate.cfg.xml");
System.out.println("hibernate config successful :" + config);
sessionFactory = config.buildSessionFactory();
Transaction ta = null;
try {
Session session = sessionFactory.getCurrentSession();
ta = session.beginTransaction();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
}
String[] cacheNames = CacheManager.getInstance().getCacheNames();
System.out.println("缓存的key cacheNames length := "
+ cacheNames.length + " 具体详细列表如下:");
for (String name : cacheNames) {
System.out.println("name := " + name);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("ehcache - hibernate Test end.");
}
}
运行结果如下:
ehcache - hibernate Test ...
hibernate config successfulrg.hibernate.cfg.AnnotationConfiguration@193c0cf
2011-12-15 11:32:36 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic]; using defaults.
2011-12-15 11:32:36 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic2]; using defaults.
2011-12-15 11:32:36 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic3]; using defaults.
2011-12-15 11:32:37 net.sf.ehcache.util.UpdateChecker doCheck
信息: New update(s) found: 2.4.6 [http://www.terracotta.org/confluence/display/release/Release+Notes+Ehcache+Core+2.4]. Please check http://ehcache.org for the latest version.
缓存的key cacheNames length := 7 具体详细列表如下:
name := sampleCache2
name := michael.cache.ehcache.hibernate.EhBlogTopic2
name := org.hibernate.cache.UpdateTimestampsCache
name := sampleCache1
name := michael.cache.ehcache.hibernate.EhBlogTopic
name := org.hibernate.cache.StandardQueryCache
name := michael.cache.ehcache.hibernate.EhBlogTopic3
ehcache - hibernate Test end.
从运行结果可见:三种方式的缓存配置都已经成功。
[二]、Hibernate+EhCache集成demo
1.分别初始化EhUserInfo(没有配置cache的)和EhBlogTopic(配置过cache的)数据如下:
EhUserInfo:
EhBlogTopic:
2.演示代码:
package michael.cache.ehcache.hibernate;
import java.util.List;
import michael.hibernate.bigstring.oracle.BigStrBlob;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
*
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class TestEhcacheHibernate {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
SessionFactory sessionFactory = null;
try {
System.out.println("ehcache - hibernate Test ...");
Configuration config = new AnnotationConfiguration()
.configure("michael/cache/ehcache/hibernate/hibernate.cfg.xml");
sessionFactory = config.buildSessionFactory();
System.out.println("buildSessionFactory===============");
System.out.println("====================================");
System.out.println("session open ....");
System.out.println("同一个session(一级缓存默认的)中,没有配置cache的Bean");
Transaction ta = null;
try {
Session session = sessionFactory.getCurrentSession();
String hsql = "select t from EhUserInfo t ";
ta = session.beginTransaction();
Query query = session.createQuery(hsql);
System.out.println("查询全部query.list().size:"
+ query.list().size());
System.out.println("再根据ID=1查询某记录时不会再去查数据库,故不会打印hsql");
EhUserInfo vo1 = (EhUserInfo) session.get(EhUserInfo.class, 1);
System.out.println("根据ID=1找到的记录:" + vo1);
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
}
System.out.println("session closed.");
System.out.println("session open ....");
try {
Session session = sessionFactory.getCurrentSession();
ta = session.beginTransaction();
System.out.println("第一次根据ID=1查询某记录时,会打印hsql");
EhUserInfo vo1 = (EhUserInfo) session.get(EhUserInfo.class, 1);
System.out.println("根据ID=1找到的记录:" + vo1);
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
}
System.out.println("session closed.");
System.out.println("====================================");
System.out.println("当前同一个sessionFactory,没有配置cache的Bean,");
System.out.println("session open ....");
try {
Session session = sessionFactory.getCurrentSession();
ta = session.beginTransaction();
System.out.println("第一次根据ID=1查询记录时会再去查数据库,故打印hsql如下:");
EhUserInfo vo1 = (EhUserInfo) session.get(EhUserInfo.class, 1);
System.out.println("根据ID=1找到的记录:" + vo1);
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
}
System.out.println("session closed.");
System.out.println("====================================");
String[] cacheNames = CacheManager.getInstance().getCacheNames();
System.out.println("缓存的key cacheNames length := "
+ cacheNames.length);
for (String name : cacheNames) {
System.out.println("name := " + name);
}
System.out.println("====================================");
System.out.println("同一个session(一级缓存默认的)中,配置cache的Bean");
System.out.println("session open ....");
try {
Session session = sessionFactory.getCurrentSession();
String hsql = "select t from EhBlogTopic t ";
ta = session.beginTransaction();
Query query = session.createQuery(hsql);
query.setCacheable(true);
System.out.println("查询全部query.list().size:"
+ query.list().size());
Cache myCache1 = CacheManager.getInstance().getCache(
"michael.cache.ehcache.hibernate.EhBlogTopic");
System.out.println("查询到EhBlogTopic cache size:"
+ myCache1.getKeys().size());
myCache1 = CacheManager.getInstance().getCache(
"michael.cache.ehcache.hibernate.EhBlogTopic");
System.out.println("EhBlogTopic cache size:"
+ myCache1.getKeys().size() + ",详细记录如下:");
for (Object str : myCache1.getKeys()) {
System.out.println(str);
}
System.out
.println("在同一个session,再根据ID=1查询记录时不会再去查数据库,故不会打印hsql");
EhBlogTopic vo1 = (EhBlogTopic) session.get(EhBlogTopic.class,
1);
System.out.println("根据ID=1找到的记录:" + vo1);
EhBlogTopic vo2 = new EhBlogTopic();
vo2.setId(10);
vo2.setUserId("michael");
vo2.setTopic("Myblog:11");
vo2.setSite("http://sjsky.iteye.com");
session.save(vo2);
ta.commit();
System.out.println("新增加一条记录ID=10");
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
}
System.out.println("session closed.");
System.out.println("====================================");
Cache myCache1 = CacheManager.getInstance().getCache(
"michael.cache.ehcache.hibernate.EhBlogTopic");
System.out.println("EhBlogTopic cache size:"
+ myCache1.getKeys().size() + ",详细记录如下:");
for (Object str : myCache1.getKeys()) {
System.out.println(str);
}
System.out.println("====================================");
System.out.println("在当前同一个sessionFactory,配置cache的bean");
System.out.println("session open ....");
try {
Session session = sessionFactory.getCurrentSession();
ta = session.beginTransaction();
System.out
.println("不管是否第一次查询ID=1的记录,如果cache中存在的,则不会再查数据库,故不打印hsql");
EhBlogTopic vo1 = (EhBlogTopic) session.get(EhBlogTopic.class,
1);
System.out.println("根据ID=1找到的记录:" + vo1);
System.out.println("查询之前session保存的数据ID=10,也不会再查数据库,故不打印hsql");
EhBlogTopic vo2 = (EhBlogTopic) session.get(EhBlogTopic.class,
10);
System.out.println("根据之前save的ID=10找到的记录:" + vo2);
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
}
System.out.println("session closed.");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != sessionFactory) {
sessionFactory.close();
}
}
System.out.println("sessionFactory closed.");
}
}
运行结果如下:
ehcache - hibernate Test ...
2011-12-15 13:27:15 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic]; using defaults.
2011-12-15 13:27:15 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic2]; using defaults.
2011-12-15 13:27:15 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic3]; using defaults.
buildSessionFactory===============
====================================
session open ....
同一个session(一级缓存默认的)中,没有配置cache的Bean
Hibernate: select ehuserinfo0_.ID as ID1_, ehuserinfo0_.USER_ID as USER2_1_, ehuserinfo0_.USER_NAME as USER3_1_, ehuserinfo0_.OHTER_INFO as OHTER4_1_ from MY_TB_EH_USER_INFO ehuserinfo0_
查询全部query.list().size:2
再根据ID=1查询某记录时不会再去查数据库,故不会打印hsql
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhUserInfo@63b2e6
session closed.
session open ....
第一次根据ID=1查询某记录时,会打印hsql
Hibernate: select ehuserinfo0_.ID as ID1_0_, ehuserinfo0_.USER_ID as USER2_1_0_, ehuserinfo0_.USER_NAME as USER3_1_0_, ehuserinfo0_.OHTER_INFO as OHTER4_1_0_ from MY_TB_EH_USER_INFO ehuserinfo0_ where ehuserinfo0_.ID=?
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhUserInfo@123a389
session closed.
====================================
当前同一个sessionFactory,没有配置cache的Bean,
session open ....
第一次根据ID=1查询记录时会再去查数据库,故打印hsql如下:
Hibernate: select ehuserinfo0_.ID as ID1_0_, ehuserinfo0_.USER_ID as USER2_1_0_, ehuserinfo0_.USER_NAME as USER3_1_0_, ehuserinfo0_.OHTER_INFO as OHTER4_1_0_ from MY_TB_EH_USER_INFO ehuserinfo0_ where ehuserinfo0_.ID=?
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhUserInfo@429c19
session closed.
====================================
缓存的key cacheNames length := 7
name := sampleCache2
name := michael.cache.ehcache.hibernate.EhBlogTopic2
name := org.hibernate.cache.UpdateTimestampsCache
name := sampleCache1
name := michael.cache.ehcache.hibernate.EhBlogTopic
name := org.hibernate.cache.StandardQueryCache
name := michael.cache.ehcache.hibernate.EhBlogTopic3
====================================
同一个session(一级缓存默认的)中,配置cache的Bean
session open ....
Hibernate: select ehblogtopi0_.ID as ID2_, ehblogtopi0_.USER_ID as USER2_2_, ehblogtopi0_.TOPIC as TOPIC2_, ehblogtopi0_.SITE as SITE2_ from MY_TB_EH_BLOG_TOPIC ehblogtopi0_
查询全部query.list().size:9
查询到EhBlogTopic cache size:9
EhBlogTopic cache size:9,详细记录如下:
michael.cache.ehcache.hibernate.EhBlogTopic#6
michael.cache.ehcache.hibernate.EhBlogTopic#5
michael.cache.ehcache.hibernate.EhBlogTopic#7
michael.cache.ehcache.hibernate.EhBlogTopic#8
michael.cache.ehcache.hibernate.EhBlogTopic#2
michael.cache.ehcache.hibernate.EhBlogTopic#9
michael.cache.ehcache.hibernate.EhBlogTopic#1
michael.cache.ehcache.hibernate.EhBlogTopic#4
michael.cache.ehcache.hibernate.EhBlogTopic#3
在同一个session,再根据ID=1查询记录时不会再去查数据库,故不会打印hsql
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhBlogTopic@11a0d35
Hibernate: insert into MY_TB_EH_BLOG_TOPIC (USER_ID, TOPIC, SITE, ID) values (?, ?, ?, ?)
新增加一条记录ID=10
session closed.
====================================
EhBlogTopic cache size:10,详细记录如下:
michael.cache.ehcache.hibernate.EhBlogTopic#6
michael.cache.ehcache.hibernate.EhBlogTopic#5
michael.cache.ehcache.hibernate.EhBlogTopic#7
michael.cache.ehcache.hibernate.EhBlogTopic#8
michael.cache.ehcache.hibernate.EhBlogTopic#2
michael.cache.ehcache.hibernate.EhBlogTopic#9
michael.cache.ehcache.hibernate.EhBlogTopic#10
michael.cache.ehcache.hibernate.EhBlogTopic#1
michael.cache.ehcache.hibernate.EhBlogTopic#4
michael.cache.ehcache.hibernate.EhBlogTopic#3
====================================
在当前同一个sessionFactory,配置cache的bean
session open ....
不管是否第一次查询ID=1的记录,如果cache中存在的,则不会再查数据库,故不打印hsql
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhBlogTopic@1321f5
查询之前session保存的数据ID=10,也不会再查数据库,故不打印hsql
根据之前save的ID=10找到的记录:michael.cache.ehcache.hibernate.EhBlogTopic@1a6518
session closed.
sessionFactory closed.
我们从上面的详细运行日志中就可以看出cache的效果。
(有关EhCache的基础介绍可参见:http://sjsky.iteye.com/blog/1288257 )
* Cache的多种配置方法
* Hibernate+EhCache集成demo
[一]、Cache的多种配置方法
Javabean cache的配置有三种,下面将一一介绍,具体如下::
(1).bean中注解配置的方式: @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
(2).hibernate.cfg.xml中标签配置方式: <class-cache class="" usage="" />
(3).映射文件*.hb.xml中标签配置方式: <cache usage=" />
1. classpath:ehcahce.xml配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true" monitoring="autodetect"
dynamicConfig="true">
<diskStore path="java.io.tmpdir"/>
<transactionManagerLookup class="net.sf.ehcache.transaction.manager.DefaultTransactionManagerLookup"
properties="jndiName=java:/TransactionManager" propertySeparator=";"/>
<cacheManagerEventListenerFactory class="" properties=""/>
<defaultCache
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="100"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
statistics="false"
/>
<cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="5"
eternal="false"
timeToLiveSeconds="120"
overflowToDisk="true" />
<cache name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000"
eternal="true"
overflowToDisk="true" />
<cache name="sampleCache1"
maxElementsInMemory="10000"
maxElementsOnDisk="1000"
eternal="false"
overflowToDisk="true"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off"
/>
<cache name="sampleCache2"
maxElementsInMemory="1000"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="FIFO"
/>
</ehcache>
2.hibernate配置文件:hibernate.cfg.xml
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:ora11g
</property>
<property name="connection.username">mytest</property>
<property name="connection.password">111111</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<property name="connection.SetBigStringTryClob">true</property>
<property name="connection.pool_size">10</property>
<property name="hibernate.jdbc.batch_size">10</property>
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<property name="current_session_context_class">thread</property>
<property name="hbm2ddl.auto">update</property>
<!-- Hibernate 3.3 and higher -->
<!--
<property name="hibernate.cache.region.factory_class">
net.sf.ehcache.hibernate.EhCacheRegionFactory
</property>
<property name="hibernate.cache.region.factory_class">
net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
</property>
-->
<!-- hibernate3.0-3.2 cache config-->
<!--
<property name="hibernate.cache.region.factory_class">
net.sf.ehcache.hibernate.EhCacheProvider
</property>
-->
<property name="hibernate.cache.provider_class">
net.sf.ehcache.hibernate.SingletonEhCacheProvider
</property>
<!-- Enable Second-Level Cache and Query Cache Settings -->
<property name="hibernate.cache.use_second_level_cache">
true
</property>
<property name="hibernate.cache.use_query_cache">
true
</property>
<!-- 注解配置 -->
<mapping class="michael.cache.ehcache.hibernate.EhUserInfo" />
<mapping class="michael.cache.ehcache.hibernate.EhBlogTopic" />
<mapping class="michael.cache.ehcache.hibernate.EhBlogTopic2" />
<!-- 映射文件 -->
<mapping
resource="michael/cache/ehcache/hibernate/tb_EhBlogTopic3.hb.xml" />
<!-- class-cache config -->
<class-cache class="michael.cache.ehcache.hibernate.EhBlogTopic"
usage="read-write" />
</session-factory>
</hibernate-configuration>
3.相关javabean代码片段如下:
(1).hibernate.cfg.xml中<calss-cache>标签配置的:EhBlogTopic.java:
/**
* @blog http://sjsky.iteye.com
* @author Michael
*/
@Entity
@Table(name = "MY_TB_EH_BLOG_TOPIC")
public class EhBlogTopic implements Serializable {
/**
* serialVersionUID
*/
private static final long serialVersionUID = -570936907944909799L;
private Integer id;
private String userId;
private String topic;
private String site;
//其他省略
}
(2). bean中注解的方式配置cache的:EhBlogTopic2.java
/**
* @blog http://sjsky.iteye.com
* @author Michael
*/
@Entity
@Table(name = "MY_TB_EH_BLOG_TOPIC2")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class EhBlogTopic2 implements Serializable {
//属性和EhBlogTopic一样
//其他省略
}
(3). 映射文件*.hb.xml中添加cache标签的: EhBlogTopic3.java
/**
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class EhBlogTopic3 implements Serializable {
//属性和EhBlogTopic一样
//其他省略
}
tb_EhBlogTopic3.hb.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="michael.cache.ehcache.hibernate">
<class name="EhBlogTopic3" table="MY_TB_EH_BLOG_TOPIC3">
<cache usage="read-write" />
<id name="id" type="int" unsaved-value="null">
<generator class="increment" />
</id>
<property name="userId" column="USER_ID" type="string"
not-null="false" length="20" />
<property name="topic" column="TOPIC" type="string"
not-null="false" length="100" />
<property name="site" column="SITE" type="string"
not-null="false" length="100" />
</class>
</hibernate-mapping>
(4). 没有配置cache的bean:EhUserInfo.java
/**
* @blog http://sjsky.iteye.com
* @author Michael
*/
@Entity
@Table(name = "MY_TB_EH_USER_INFO")
public class EhUserInfo implements Serializable {
/**
* serialVersionUID
*/
private static final long serialVersionUID = 930384253681679239L;
private Integer id;
private String userId;
private String userName;
private String otherInfo;
/**
* @return the id
*/
@Id
@GeneratedValue
@Column(name = "ID")
public Integer getId() {
return id;
}
//其他省略。。。
}
4.测试运行代码如下:
package michael.cache.ehcache.hibernate;
import java.util.List;
import michael.hibernate.bigstring.oracle.BigStrBlob;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
*
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class TestEhcacheHibernate {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
testMulitConfigMethod();
}
/**
* 测试多种配置缓存的方法
*/
public static void testMulitConfigMethod() {
SessionFactory sessionFactory = null;
try {
System.out.println("ehcache - hibernate Test ...");
Configuration config = new AnnotationConfiguration()
.configure("michael/cache/ehcache/hibernate/hibernate.cfg.xml");
System.out.println("hibernate config successful :" + config);
sessionFactory = config.buildSessionFactory();
Transaction ta = null;
try {
Session session = sessionFactory.getCurrentSession();
ta = session.beginTransaction();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
}
String[] cacheNames = CacheManager.getInstance().getCacheNames();
System.out.println("缓存的key cacheNames length := "
+ cacheNames.length + " 具体详细列表如下:");
for (String name : cacheNames) {
System.out.println("name := " + name);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("ehcache - hibernate Test end.");
}
}
运行结果如下:
ehcache - hibernate Test ...
hibernate config successfulrg.hibernate.cfg.AnnotationConfiguration@193c0cf
2011-12-15 11:32:36 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic]; using defaults.
2011-12-15 11:32:36 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic2]; using defaults.
2011-12-15 11:32:36 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic3]; using defaults.
2011-12-15 11:32:37 net.sf.ehcache.util.UpdateChecker doCheck
信息: New update(s) found: 2.4.6 [http://www.terracotta.org/confluence/display/release/Release+Notes+Ehcache+Core+2.4]. Please check http://ehcache.org for the latest version.
缓存的key cacheNames length := 7 具体详细列表如下:
name := sampleCache2
name := michael.cache.ehcache.hibernate.EhBlogTopic2
name := org.hibernate.cache.UpdateTimestampsCache
name := sampleCache1
name := michael.cache.ehcache.hibernate.EhBlogTopic
name := org.hibernate.cache.StandardQueryCache
name := michael.cache.ehcache.hibernate.EhBlogTopic3
ehcache - hibernate Test end.
从运行结果可见:三种方式的缓存配置都已经成功。
[二]、Hibernate+EhCache集成demo
1.分别初始化EhUserInfo(没有配置cache的)和EhBlogTopic(配置过cache的)数据如下:
EhUserInfo:
EhBlogTopic:
2.演示代码:
package michael.cache.ehcache.hibernate;
import java.util.List;
import michael.hibernate.bigstring.oracle.BigStrBlob;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
*
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class TestEhcacheHibernate {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
SessionFactory sessionFactory = null;
try {
System.out.println("ehcache - hibernate Test ...");
Configuration config = new AnnotationConfiguration()
.configure("michael/cache/ehcache/hibernate/hibernate.cfg.xml");
sessionFactory = config.buildSessionFactory();
System.out.println("buildSessionFactory===============");
System.out.println("====================================");
System.out.println("session open ....");
System.out.println("同一个session(一级缓存默认的)中,没有配置cache的Bean");
Transaction ta = null;
try {
Session session = sessionFactory.getCurrentSession();
String hsql = "select t from EhUserInfo t ";
ta = session.beginTransaction();
Query query = session.createQuery(hsql);
System.out.println("查询全部query.list().size:"
+ query.list().size());
System.out.println("再根据ID=1查询某记录时不会再去查数据库,故不会打印hsql");
EhUserInfo vo1 = (EhUserInfo) session.get(EhUserInfo.class, 1);
System.out.println("根据ID=1找到的记录:" + vo1);
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
}
System.out.println("session closed.");
System.out.println("session open ....");
try {
Session session = sessionFactory.getCurrentSession();
ta = session.beginTransaction();
System.out.println("第一次根据ID=1查询某记录时,会打印hsql");
EhUserInfo vo1 = (EhUserInfo) session.get(EhUserInfo.class, 1);
System.out.println("根据ID=1找到的记录:" + vo1);
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
}
System.out.println("session closed.");
System.out.println("====================================");
System.out.println("当前同一个sessionFactory,没有配置cache的Bean,");
System.out.println("session open ....");
try {
Session session = sessionFactory.getCurrentSession();
ta = session.beginTransaction();
System.out.println("第一次根据ID=1查询记录时会再去查数据库,故打印hsql如下:");
EhUserInfo vo1 = (EhUserInfo) session.get(EhUserInfo.class, 1);
System.out.println("根据ID=1找到的记录:" + vo1);
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
}
System.out.println("session closed.");
System.out.println("====================================");
String[] cacheNames = CacheManager.getInstance().getCacheNames();
System.out.println("缓存的key cacheNames length := "
+ cacheNames.length);
for (String name : cacheNames) {
System.out.println("name := " + name);
}
System.out.println("====================================");
System.out.println("同一个session(一级缓存默认的)中,配置cache的Bean");
System.out.println("session open ....");
try {
Session session = sessionFactory.getCurrentSession();
String hsql = "select t from EhBlogTopic t ";
ta = session.beginTransaction();
Query query = session.createQuery(hsql);
query.setCacheable(true);
System.out.println("查询全部query.list().size:"
+ query.list().size());
Cache myCache1 = CacheManager.getInstance().getCache(
"michael.cache.ehcache.hibernate.EhBlogTopic");
System.out.println("查询到EhBlogTopic cache size:"
+ myCache1.getKeys().size());
myCache1 = CacheManager.getInstance().getCache(
"michael.cache.ehcache.hibernate.EhBlogTopic");
System.out.println("EhBlogTopic cache size:"
+ myCache1.getKeys().size() + ",详细记录如下:");
for (Object str : myCache1.getKeys()) {
System.out.println(str);
}
System.out
.println("在同一个session,再根据ID=1查询记录时不会再去查数据库,故不会打印hsql");
EhBlogTopic vo1 = (EhBlogTopic) session.get(EhBlogTopic.class,
1);
System.out.println("根据ID=1找到的记录:" + vo1);
EhBlogTopic vo2 = new EhBlogTopic();
vo2.setId(10);
vo2.setUserId("michael");
vo2.setTopic("Myblog:11");
vo2.setSite("http://sjsky.iteye.com");
session.save(vo2);
ta.commit();
System.out.println("新增加一条记录ID=10");
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
}
System.out.println("session closed.");
System.out.println("====================================");
Cache myCache1 = CacheManager.getInstance().getCache(
"michael.cache.ehcache.hibernate.EhBlogTopic");
System.out.println("EhBlogTopic cache size:"
+ myCache1.getKeys().size() + ",详细记录如下:");
for (Object str : myCache1.getKeys()) {
System.out.println(str);
}
System.out.println("====================================");
System.out.println("在当前同一个sessionFactory,配置cache的bean");
System.out.println("session open ....");
try {
Session session = sessionFactory.getCurrentSession();
ta = session.beginTransaction();
System.out
.println("不管是否第一次查询ID=1的记录,如果cache中存在的,则不会再查数据库,故不打印hsql");
EhBlogTopic vo1 = (EhBlogTopic) session.get(EhBlogTopic.class,
1);
System.out.println("根据ID=1找到的记录:" + vo1);
System.out.println("查询之前session保存的数据ID=10,也不会再查数据库,故不打印hsql");
EhBlogTopic vo2 = (EhBlogTopic) session.get(EhBlogTopic.class,
10);
System.out.println("根据之前save的ID=10找到的记录:" + vo2);
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
}
System.out.println("session closed.");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != sessionFactory) {
sessionFactory.close();
}
}
System.out.println("sessionFactory closed.");
}
}
运行结果如下:
ehcache - hibernate Test ...
2011-12-15 13:27:15 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic]; using defaults.
2011-12-15 13:27:15 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic2]; using defaults.
2011-12-15 13:27:15 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic3]; using defaults.
buildSessionFactory===============
====================================
session open ....
同一个session(一级缓存默认的)中,没有配置cache的Bean
Hibernate: select ehuserinfo0_.ID as ID1_, ehuserinfo0_.USER_ID as USER2_1_, ehuserinfo0_.USER_NAME as USER3_1_, ehuserinfo0_.OHTER_INFO as OHTER4_1_ from MY_TB_EH_USER_INFO ehuserinfo0_
查询全部query.list().size:2
再根据ID=1查询某记录时不会再去查数据库,故不会打印hsql
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhUserInfo@63b2e6
session closed.
session open ....
第一次根据ID=1查询某记录时,会打印hsql
Hibernate: select ehuserinfo0_.ID as ID1_0_, ehuserinfo0_.USER_ID as USER2_1_0_, ehuserinfo0_.USER_NAME as USER3_1_0_, ehuserinfo0_.OHTER_INFO as OHTER4_1_0_ from MY_TB_EH_USER_INFO ehuserinfo0_ where ehuserinfo0_.ID=?
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhUserInfo@123a389
session closed.
====================================
当前同一个sessionFactory,没有配置cache的Bean,
session open ....
第一次根据ID=1查询记录时会再去查数据库,故打印hsql如下:
Hibernate: select ehuserinfo0_.ID as ID1_0_, ehuserinfo0_.USER_ID as USER2_1_0_, ehuserinfo0_.USER_NAME as USER3_1_0_, ehuserinfo0_.OHTER_INFO as OHTER4_1_0_ from MY_TB_EH_USER_INFO ehuserinfo0_ where ehuserinfo0_.ID=?
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhUserInfo@429c19
session closed.
====================================
缓存的key cacheNames length := 7
name := sampleCache2
name := michael.cache.ehcache.hibernate.EhBlogTopic2
name := org.hibernate.cache.UpdateTimestampsCache
name := sampleCache1
name := michael.cache.ehcache.hibernate.EhBlogTopic
name := org.hibernate.cache.StandardQueryCache
name := michael.cache.ehcache.hibernate.EhBlogTopic3
====================================
同一个session(一级缓存默认的)中,配置cache的Bean
session open ....
Hibernate: select ehblogtopi0_.ID as ID2_, ehblogtopi0_.USER_ID as USER2_2_, ehblogtopi0_.TOPIC as TOPIC2_, ehblogtopi0_.SITE as SITE2_ from MY_TB_EH_BLOG_TOPIC ehblogtopi0_
查询全部query.list().size:9
查询到EhBlogTopic cache size:9
EhBlogTopic cache size:9,详细记录如下:
michael.cache.ehcache.hibernate.EhBlogTopic#6
michael.cache.ehcache.hibernate.EhBlogTopic#5
michael.cache.ehcache.hibernate.EhBlogTopic#7
michael.cache.ehcache.hibernate.EhBlogTopic#8
michael.cache.ehcache.hibernate.EhBlogTopic#2
michael.cache.ehcache.hibernate.EhBlogTopic#9
michael.cache.ehcache.hibernate.EhBlogTopic#1
michael.cache.ehcache.hibernate.EhBlogTopic#4
michael.cache.ehcache.hibernate.EhBlogTopic#3
在同一个session,再根据ID=1查询记录时不会再去查数据库,故不会打印hsql
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhBlogTopic@11a0d35
Hibernate: insert into MY_TB_EH_BLOG_TOPIC (USER_ID, TOPIC, SITE, ID) values (?, ?, ?, ?)
新增加一条记录ID=10
session closed.
====================================
EhBlogTopic cache size:10,详细记录如下:
michael.cache.ehcache.hibernate.EhBlogTopic#6
michael.cache.ehcache.hibernate.EhBlogTopic#5
michael.cache.ehcache.hibernate.EhBlogTopic#7
michael.cache.ehcache.hibernate.EhBlogTopic#8
michael.cache.ehcache.hibernate.EhBlogTopic#2
michael.cache.ehcache.hibernate.EhBlogTopic#9
michael.cache.ehcache.hibernate.EhBlogTopic#10
michael.cache.ehcache.hibernate.EhBlogTopic#1
michael.cache.ehcache.hibernate.EhBlogTopic#4
michael.cache.ehcache.hibernate.EhBlogTopic#3
====================================
在当前同一个sessionFactory,配置cache的bean
session open ....
不管是否第一次查询ID=1的记录,如果cache中存在的,则不会再查数据库,故不打印hsql
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhBlogTopic@1321f5
查询之前session保存的数据ID=10,也不会再查数据库,故不打印hsql
根据之前save的ID=10找到的记录:michael.cache.ehcache.hibernate.EhBlogTopic@1a6518
session closed.
sessionFactory closed.
我们从上面的详细运行日志中就可以看出cache的效果。
相关推荐
1.通过google ehcache-spring-annotatios.jar自动注解方式实现整合Spring+Ehcache。 2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。...
【Spring Boot 使用 JSP 集成 Hibernate+Shiro+Ehcache 项目详解】 Spring Boot 是一个基于 Spring 框架的高度集成了多种技术的开发工具,它简化了配置过程,使得开发人员能够快速构建可运行的应用程序。在这个项目...
例如,为了使用Ehcache,需要在Spring配置文件中添加Ehcache的相关bean,然后在Hibernate的SessionFactory配置中启用二级缓存。此外,还需要在Struts的Action中调用由Spring管理的业务服务,这些服务通常会利用...
在实际应用中,开发者需要根据项目需求调整这些配置,例如设置数据库连接参数、配置Ehcache缓存策略、定义SpringMVC的拦截器和视图解析器等。此外,还需要确保所有的依赖库已正确导入,以便项目能够顺利运行。这样的...
1. **Spring配置**:Spring的配置文件(如`applicationContext.xml`)会定义bean,包括数据源、SessionFactory(Hibernate)、缓存管理器(Ehcache)以及业务层和服务层的组件。通过依赖注入,Spring将这些组件装配...
这个压缩包"SpringMVC+hibernate+spring+shiro+Ehcache所需jar包"提供了搭建基于SpringMVC、Spring 4.2、Hibernate 4.3、Shiro 1.2.4和Ehcache 2.0的基础组件,这些都是Java开发中非常流行和实用的工具。接下来,...
spring+springmvc+hibernate+ehcache JavaWeb后台框架,不仅提高了开发程序的速度,且其中还是用到hibernate和ehcache缓存的使用,加快了程序运行的数据,该框架亲测好用。值得注意的是该种框架现在还算是用的比较多...
整合EhCache与Spring和Hibernate非常简单,Spring提供了配置支持,使得EhCache的初始化和管理变得自动化。通过设置配置文件,可以指定哪些数据需要被缓存,以及缓存的策略,如过期时间、更新策略等。 总的来说,...
在IT行业中,构建高效、可扩展的Web应用是至关重要的,而"hibernate4+spring4+springmvc+ehcache+自己写的cache系统"是一个常见的技术栈,用于实现这样的目标。这个组合提供了完整的数据持久化、服务层管理、前端...
里面有目前的流行的前台springmvc框架,持久化层采用hibernate,安全方面采用shiro框架,页面风格采用bootstrap,同时里面还集成了javamelody监控,ehcache缓存机制,druid连接池管理,以及spring事务管理,spring...
标题中的"jar包整合:Springmvc+hibernate+Ehcache+shiro+mysql+Oracle+fastjson"指的是一个综合性的Java项目,它集成了多种技术框架,用于构建高效、可扩展的Web应用。让我们逐一解析这些技术的核心知识点: 1. **...
- 配置Maven的pom.xml文件,添加Spring、Hibernate、Shiro和Ehcache等依赖。 - 创建Spring的配置文件,定义Bean和数据源,配置AOP等。 - 配置Hibernate的实体类和映射文件,设置数据源和SessionFactory。 - 使用...
这里我们关注的是一个基于Spring、SpringMVC、Hibernate、CXF、Quartz定时器和Ehcache的整合项目。这个项目结合了这些技术,以实现一个高效、灵活且可扩展的企业级应用。下面将详细介绍每个组件及其在整体架构中的...
7. **Ehcache缓存**:Ehcache是Java的一个流行缓存解决方案,可以用于存储和检索经常访问的数据,提高系统性能。在Web应用中,Ehcache可以缓存数据库查询结果或计算结果,减少对数据库的访问压力。 8. **DOM4J**:...
标题中的“DWZ+springMvc+hibernate+ehcache 简易的后台管理+即时通讯”揭示了这个项目采用的技术栈,包括前端框架、后端框架、持久层框架以及缓存管理工具。让我们逐一深入理解这些技术点: 1. **DWZ**:DWZ全称为...
通过这样的方式,EhCache可以在Spring+Hibernate环境中提供高效的缓存服务,减少对数据库的访问,提高系统性能。在实际应用中,可以根据业务需求调整缓存策略,如缓存更新策略、并发控制等,以达到最佳的性能效果。
以EhCache为例,我们需要在项目中引入ehcache-core或ehcache的依赖,并在Hibernate配置文件(hibernate.cfg.xml或persistence.xml)中启用二级缓存,添加如下配置: ```xml <property name="hibernate.cache.use_...
1. 当用 Hibernate 的方式修改表数据(save,update,delete 等等),这时 EhCache 会自动把缓存中关于此表的所有缓存全部删除掉(这样能达到同步)。但 对于数据经常修改的表来说,可能就失去缓存的意义了(不能减轻数据库...
Ehcache 是一个开源的缓存框架,提供了一个高性能的缓存解决方案,可以与 Hibernate 和 Spring 整合使用,以提高应用程序的性能。 缓存的重要性 在 Web 应用程序中,数据库访问是性能瓶颈之一。数据库访问需要消耗...
[5].ehcache缓存机制(永久缓存/临时缓存) 代码生成器界面: A.动态选择需要生成文件(ServiceI\ServiceImpl\Jsp\Action\Entity\Page) B.动态选择JSP模板(行编辑/单页编辑) ********************************...