hibernate和spring调整配置二级缓存
hibernate和spring整合配置二级缓存
hibernate 二级缓存配置 1.配置hbm.xml 在class节点下面添加 <cache usage="read-write"/>(读写) 或者 <cache usage="read-only"/>(只读) 如: <class name="com.hxy.portal.poststext.JforumPostsText" table="jforum_posts_text" catalog="jforum_bbs"> <cache usage="read-write"/> <id name="postId" type="java.lang.Integer"> <column name="post_id" /> <generator class="identity" /> </id> <property name="postText" type="java.lang.String"> <column name="post_text" length="65535" /> </property> ..... </class> 2.配置spring文件 //打开hibernate二级缓存 hibernate.cache.use_sencond_levl_cache=true //设置缓存加载类 hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider //设置查询缓存 hibernate.cache.use_query_cache=true 具体配置: <bean id="sf_id" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <value> <!-- 配置hibernate属性 如第一个方言等--> <!-- 1.hibernate.dialect Hibernate SQL方言 表示连接那种数据库 2.hibernate.query.substitutions 你可以使用hibernate.query.substitutions定义新的Hibernate查询短语。比如说: hibernate.query.substitutions true 1, false 0 这个配置意思是当你在Hibernate里面输入true的时候,Hibernate会转化为1插入数据库, 当你在Hibernate里面输入false的时候,Hibernate会转化为0插入数据库,后面的Y,N同理。 对于某些数据库,例如Oracle来说,没有boolean数据类型,就是采用1代表true,0代表false, 因此使用这个配置在Hibernate里面直接用true/false会非常直观。 hibernate.query.substitutions toLowercase=LOWER 这可以让你重新命名SQL的LOWER函数。 3.hibernate.jdbc.batch_size 一个非零值,会开启Hibernate使用JDBC2的批量更新功能取值. 建议值在 5 和 30之间。 4.hibernate.cache.provider_class 指定一个自定义的CacheProvider缓存提供者的类名.取值. classname.of.CacheProvider 5.hibernate.cache.provider_configuration_file_resource_path hibernate缓存文件路径 6.hibernate.cache.use_sencond_levl_cache打开hibernate二级缓存 7.update如果数据库中不存在表则生成,存在如果有增加自动增加,开发使用update,生产一般不设置 --> hibernate.hbm2ddl.auto hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect hibernate.query.substitutions=true 1, false 0 hibernate.jdbc.batch_size=20 hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.cache.use_sencond_levl_cache=true hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider hibernate.cache.use_query_cache=true </value> <!-- hibernate.cache.provider_configuration_file_resource_path 指定缓存文件路径和文件名 --> </property> <!-- hibernate配置文件所在地 便于spring加载hibernate配置文件 --> <property name="mappingDirectoryLocations"> <list> <value>classpath:/com/hxy/portal/hibernate</value> </list> </property> </bean> 3.编写ehcache.xml 配置文件 <ehcache> <!-- maxElementsInMemory="10000" 缓存中最大允许创建的对象数 --> <!-- eternal="false" 缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期 --> <!-- timeToIdleSeconds="120" 缓存数据钝化时间(设置对象在它过期之前的空闲时间) --> <!-- timeToLiveSeconds="120" 缓存数据的生存时间(设置对象在它过期之前的生存时间) --> <!-- overflowToDisk="true" 内存不足时,是否启用磁盘缓存 --> <diskStore path="c:\\ehcache\"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> </ehcache> 4.使用查询方法 /**** * 根据hql查询List */ public List findCacheListByHql(final String hql) { // TODO Auto-generated method stub return this.getHibernateTemplate().executeFind(new HibernateCallback(){ public Object doInHibernate(Session session) throws HibernateException, SQLException { // TODO Auto-generated method stub Query queryObject = session.createQuery(hql); //设置是否启用缓存 queryObject.setCacheable(true); if(getHibernateTemplate().getQueryCacheRegion() != null){ queryObject.setCacheRegion(getHibernateTemplate().getQueryCacheRegion()); } return queryObject.list(); } }); } 5.缓存仅仅对hql查询生效,如果使用sql更变数据库数据,缓存数据将不会修改,只用使用hql操作数据库才能同步修改缓存数据。