步骤如下: 1.导入spring,ehcache,hibernate相关的jar文件。 2.在spring的配置文件里填写代码如下:
<!-- 定义数据源,修改成自己的 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/EhcacheSql"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<!-- 在这里添加ehcache的支持 -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>orm/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="UserDAO" class="orm.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- <aop:aspectj-autoproxy proxy-target-class="true" /> -->
<tx:annotation-driven />
<context:component-scan base-package="com.*" /><!-- 定义数据源,修改成自己的 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/EhcacheSql"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<!-- 在这里添加ehcache的支持 -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>orm/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="UserDAO" class="orm.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- <aop:aspectj-autoproxy proxy-target-class="true" /> -->
<tx:annotation-driven />
<context:component-scan base-package="com.*" />
3.配置ehcache.xml文件内容如下: <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="600" overflowToDisk="false">
</defaultCache>
<cache name="levelOneCache" maxElementsInMemory="1000" eternal="false"
timeToIdleSeconds="300" timeToLiveSeconds="1000" overflowToDisk="false" />
</ehcache>
<!-- 这里是ehcache的配置,具体参数请自己查询 --><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="600" overflowToDisk="false">
</defaultCache>
<cache name="levelOneCache" maxElementsInMemory="1000" eternal="false"
timeToIdleSeconds="300" timeToLiveSeconds="1000" overflowToDisk="false" />
</ehcache>
<!-- 这里是ehcache的配置,具体参数请自己查询 -->
4.在User.hbm.xml文件里class下加上<cache usage="read-only/write-read"/> <hibernate-mapping> <!-- catalog="guoang" 请修改成自己的数据库 --> <class name="orm.User" table="user" catalog="EhcacheSql"> <!-- 需要在class下面添加 |<cache usage="read-only"/>| usage的属性可变,具体请自己查询 --> <cache usage="read-only"/> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="increment" /> </id> <property name="username" type="java.lang.String"> <column name="username" length="50" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="50" /> </property> </class> </hibernate-mapping> <hibernate-mapping> <!-- catalog="guoang" 请修改成自己的数据库 --> <class name="orm.User" table="user" catalog="EhcacheSql"> <!-- 需要在class下面添加 |<cache usage="read-only"/>| usage的属性可变,具体请自己查询 --> <cache usage="read-only"/> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="increment" /> </id> <property name="username" type="java.lang.String"> <column name="username" length="50" /> </property> <property name="password" type="java.lang.String"> <column name="password" length="50" /> </property> </class> </hibernate-mapping>
主要是在ehcache.xml里配置相关信息,各个属性如下: name 要缓存的实体类的名称 maxElementsInMemory 设置该缓存实体的最大个数 eternal 对象是否永久有效 timeToIdleSeconds 设置对象在失效前的允许闲置时间(单位:s) timeToLiveSeconds 设置对象在失效前允许存活时间(单位:s) overflowToDisk 当缓存中对象达到maxElementsInMemory时,存入磁盘 diskSpoolBufferSizeMB 设置磁盘缓冲区大小 maxElementsOnDisk 设置磁盘能缓存最大对象个数 diskPersistent 是否缓存虚拟机重启期数 diskExpiryThreadIntervalSeconds 磁盘失效线程运行时间间隔,默认是120秒。 memoryStoreEvictionPolicy 当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 clearOnFlush 内存数量最大时是否清除。 name 要缓存的实体类的名称 maxElementsInMemory 设置该缓存实体的最大个数 eternal 对象是否永久有效 timeToIdleSeconds 设置对象在失效前的允许闲置时间(单位:s) timeToLiveSeconds 设置对象在失效前允许存活时间(单位:s) overflowToDisk 当缓存中对象达到maxElementsInMemory时,存入磁盘 diskSpoolBufferSizeMB 设置磁盘缓冲区大小 maxElementsOnDisk 设置磁盘能缓存最大对象个数 diskPersistent 是否缓存虚拟机重启期数 diskExpiryThreadIntervalSeconds 磁盘失效线程运行时间间隔,默认是120秒。 memoryStoreEvictionPolicy 当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 clearOnFlush 内存数量最大时是否清除。