hibernate 每次获取 数据都会访问数据库?-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

hibernate 每次获取 数据都会访问数据库?

我使用的是annotation方式映射实体类的。hibernate4.3.4 spring4.0.2 struts2.3.16.1 网上下载整合好的demo。然后在这个基础上开发的。

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
    <session-factory>
 
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="jdbc.batch_size">20</property>
        <property name="connection.autocommit">true</property>
 
        <!-- 显示sql语句 -->
        <property name="show_sql">true</property>
        <property name="connection.useUnicode">true</property>
        <property name="connection.characterEncoding">UTF-8</property>
 
        <!-- 缓存设置 -->
        <property name="cache.provider_configuration_file_resource_path">/ehcache.xml</property>
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
        <property name="cache.use_query_cache">true</property>
 
 
        <!-- 指定自动生成数据表的策略yh -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 报错总是说不能在没有活动的事务中执行
         此session不是Spring托管的session,
         所以获取的session是在spring代理的上下文之外的的当前线程之中,所以此session并非事务管理器代理的那个session,不会自动开启事务
        <property name="current_session_context_class">thread</property>
         -->
    </session-factory>
</hibernate-configuration>
ehcache.xml
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as
  ~ indicated by the @author tags or express copyright attribution
  ~ statements applied by the authors. All third-party contributions are
  ~ distributed under license by Red Hat Middleware LLC.
  ~
  ~ This copyrighted material is made available to anyone wishing to use, modify,
  ~ copy, or redistribute it subject to the terms and conditions of the GNU
  ~ Lesser General Public License, as published by the Free Software Foundation.
  ~
  ~ This program is distributed in the hope that it will be useful,
  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  ~ or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
  ~ for more details.
  ~
  ~ You should have received a copy of the GNU Lesser General Public License
  ~ along with this distribution; if not, write to:
  ~ Free Software Foundation, Inc.
  ~ 51 Franklin Street, Fifth Floor
  ~ Boston, MA  02110-1301  USA
  -->
<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="./target/tmp"/>
 
 
    <!--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.
       中文说明
                    maxElementsInMemory=“10000” //Cache中最多允许保存的数据对象的数量
                    external=“false” //缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期 
                    timeToIdleSeconds=“1000”  //缓存数据钝化时间(设置对象在它过期之前的空闲时间)  
                    timeToLiveSeconds=“1000”  //缓存数据的生存时间(设置对象在它过期之前的生存时间)
                    overflowToDisk=“false”    //内存不足时,是否启用磁盘缓存  
                    memoryStoreEvictionPolicy="LRU" //内存不足时数据对象的清除策略
                    ehcache中缓存的3种清空策略:
                    FIFO(first in first out):先进先出
                    LFU( Less Frequently Used):一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
                    LRU(Least Recently Used):最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
        -->
    <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>

展开
收起
爵霸 2016-03-04 09:16:50 2561 0
1 条回答
写回答
取消 提交回答
  • 爵霸

    需要在在XXmapper.xml上定义

    2019-07-17 18:51:47
    赞同 展开评论 打赏
问答排行榜
最热
最新
相关电子书
更多
数据库2025 V3
立即下载
阿里云数据库案例集下载
立即下载
高可用数据库的搭建与备份恢复策略验证实战
立即下载